将提示变量与StartsWith一起使用

时间:2016-04-29 12:15:13

标签: powershell

我正在尝试停止一组特定的服务,使用.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")) {
    ...

它会停止每一项服务。我做错了什么?

1 个答案:

答案 0 :(得分:1)

将$ input更改为$ input2。

我猜错了使用保留字。

相关问题