【发布时间】:2016-01-14 08:36:17
【问题描述】:
我想要Get-Service 的用户输入,但它不接受我的变量
我想运行以下命令:
#### --- Content -- ###
# - Location ##
$Service_text = "$env:userprofile\documents\PS-Script-Services\Services.txt"
#- Test path
$service_test = Test-Path $Service_text
#-- Get-content ##-
$Get_the_service = Get-Content $service_text
#-loop-# -- Get-Service -- ##
if ($service_test -eq $false) {
Write-Host "To view your Services go to Start --> en typ services, or go to Powershell and type get-service"
Write-Host "Which Services do you want to monitor? (Example: Teamviewer, BITS, DHCP, Eventlog, Spooler." -BackgroundColor Black -ForegroundColor Yellow
Write-Host "This script only works when a service has been stopped" -BackgroundColor Black -ForegroundColor Yellow
Read-Host "Enter services" | Out-File $service_text
$ask_service = Get-Service -Name $Get_the_service
问题是它无法获取服务。
$Service_name = "dhcp"
Get-Service -Name $service_name
给我这个输出:
状态名称显示名称 ------ ---- ------------ 运行 dhcp DHCP 客户端
但如果我想要多项服务
$Service_name = "dhcp, spooler"
Get-Service -Name $service_name
我收到此错误:
Get-Service : 找不到任何服务名称为“dhcp, spooler”的服务。
在行:5 字符:1
+ 获取服务-名称 $service_name
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (dhcp, spooler:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
【问题讨论】:
标签: powershell variables