0
Ich versuche, eine bestimmte Gruppe von Diensten zu stoppen, mit .StartsWith, um sie zu finden.Verwenden von Variablen mit 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."
}
}
}
Dies funktioniert gut - Service FooBarBinglyBong und FooBarJingleJangle wird gestoppt. Jedoch wenn ich versuche, und dies tun:
[string] $input = Read-Host -prompt 'Stop services starting with'
Get-Service | ForEach {
$name = $_.Name
if ($name.StartsWith("$input")) {
...
Es stoppt jeden einzelnen Dienst. Was mache ich falsch?