我正在尝试停止一组特定的服务,使用.StartsWith查找它们。
Get-Service | ForEach {
$name = $_.Name
if ($name.StartsWith("FooBar")) {
# stop service
if ($_.Status -ne "Stopped") {
Stop-Service "$name" -Force
$_.WaitForStatus('Stopped', '00:01:00')
Write-Host "Service $name stopped."
}
}
}
这很好——FooBarBinglyBong和FooBarJingleJangle的服务将停止。然而,当我尝试这样做时:
[string] $input = Read-Host -prompt 'Stop services starting with'
Get-Service | ForEach {
$name = $_.Name
if ($name.StartsWith("$input")) {
...
它停止了每一项服务。我做错了什么?
将$input更改为$input2有效。
我想是我用了一个保留词的错。