1

今天刚开始写一个脚本,但是在我进入它的主要核心之前,当我运行下面并为多个选择“M”时 - IF 语句转到“M”但是,当它然后弹出一个窗口时要求一条路径,我故意关闭它出错的窗口并直接进入 else 语句......

如何让脚本结束,以便如果我选择$answer -eq "M"then 当我退出脚本时,它会在那里关闭,然后在该 IF 语句中关闭,而不是继续执行 ELSE 语句。

我希望以上是有道理的 - 想不出更好的方法来解释它..

另外...我对以下声明不满意:

while("S","M" -notcontains $answer)

问题是,如果一个人输入其他内容,它只会继续弹出问题。如果有人输入了错误的值,然后询问用户“请输入“S”或“M”,我该如何出错。

代码:

Write-Host ""
Write-Host "Backup Troubleshooting Script" -ForegroundColor Cyan
Write-Host ""

$answer = Read-Host "Do you want to check for multiple (M) servers or a single (S) server? Please enter "S" or "M""

while("S","M" -notcontains $answer)
{
    $answer = Read-Host "Do you want to check for multiple (M) servers or a single (S) server? Please enter "S" or "M""
}

If ($answer -eq "M")
{
    $serverlist = Read-Host "Please enter path to server list: "
    $computer = Get-Content -path $serverlist

        foreach ($computer1 in $computer)
        {
            Write-Host $computer1
            (Get-WmiObject -computerName $computer1 Win32_Service -Filter "Name='Alerter'").StopService()
            sleep 3
            (Get-WmiObject -computerName $computer1 Win32_Service -Filter "Name='Alerter'").StartService() 
            Write-Host `
        }

else
{
    Write-Host "You have chosen "S""
}
}

错误:

> Backup Troubleshooting Script
> 
> Read-Host : An error of type
> "System.Management.Automation.Host.PromptingException" has occurred.
> At
> C:\Users\gaachm5\AppData\Local\Temp\769b7304-5529-4f5c-aba7-2cd3dcc2cec9.ps1:15
> char:16
> +     $serverlist = Read-Host "Please enter path to server list: "
> +                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>     + CategoryInfo          : ResourceUnavailable: (:) [Read-Host], PromptingException
>     + FullyQualifiedErrorId : System.Management.Automation.Host.PromptingException,Microsoft.PowerShell.Commands.ReadHostC
> ommand   Get-Content : Cannot bind argument to parameter 'Path'
> because it is null. At
> C:\Users\gaachm5\AppData\Local\Temp\769b7304-5529-4f5c-aba7-2cd3dcc2cec9.ps1:16
> char:32
> +     $computer = Get-Content -path $serverlist
> +                                   ~~~~~~~~~~~
>     + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
>     + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentComma
> nd   else : The term 'else' is not recognized as the name of a cmdlet,
> function, script file, or operable program. Check the  spelling of the
> name, or if a path was included, verify that the path is correct and
> try again. At
> C:\Users\gaachm5\AppData\Local\Temp\769b7304-5529-4f5c-aba7-2cd3dcc2cec9.ps1:27
> char:1
> + else
> + ~~~~
>     + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoundException
>     + FullyQualifiedErrorId : CommandNotFoundException
>  
> 
>     Write-Host "You have chosen "S""
4

1 回答 1

4

你的花括号放错了位置。代码应为:

            (Get-WmiObject -computerName $computer1 Win32_Service -Filter "Name='Alerter'").StartService() 
            Write-Host `
        }
}
else
{
    Write-Host "You have chosen "S""
}
于 2013-03-13T18:07:16.170 回答