0
我试图停止一组特定的服务,使用.StartsWith来查找它们。在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")) {
...
它停止每一项服务。我究竟做错了什么?