我想爲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
給了我這樣的輸出:
Status Name DisplayName ------ ---- ----------- Running dhcp DHCP Client
但是如果我想多業務
$Service_name = "dhcp, spooler"
Get-Service -Name $service_name
我收到此錯誤:
Get-Service : Cannot find any service with service name 'dhcp, spooler'. At line:5 char:1 + Get-Service -Name $service_name + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (dhcp, spooler:String) [Get-Service], ServiceCommandException + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand