我想要isGooglePlayServicesAvailable
的用户输入,但它不接受我的变量
我想运行以下命令:
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
给了我这个输出:
Status Name DisplayName ------ ---- ----------- Running dhcp DHCP Client
但如果我想要多项服务
$Service_name = "dhcp"
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
答案 0 :(得分:1)
没有名称为dhcp, spooler
的服务。显然,您正在尝试获取服务列表(即dhcp
和spooler
)。为此,您必须将$Service_name
定义为实际列表,而不是将逗号分隔的单词定义为单个字符串:
$Service_name = "dhcp", "spooler"
答案 1 :(得分:0)
如果要检索多个服务,则必须将数组传递给Get-Service
cmdlet。您可以使用-split
将字符串拆分为数组并修剪任何空格:
Get-Service -Name ($Service_name -split ',' | % { $_.Trim() } )
答案 2 :(得分:0)
“dhcp,spooler”是一个文字字符串。因此,当您将其传递给get-service时,Get-Service会查找名为 dhcp,spooler 的服务。相反,传递一个由逗号分隔的字符串“列表”。即'dhcp','假脱机' 然后你可以随心所欲地传递它。 如果您要从文本文件中收集名称,请确保每个服务都在新行