powershell - 进入IF但退出到ELSE

标签 powershell powershell-2.0 powershell-3.0

今天才刚刚开始编写脚本,但是在进入下面的脚本并选择多个“M”之前,我还未进入脚本的核心-IF语句转到“M”,但随后弹出一个窗口以显示该脚本。询问路径,我故意关闭错误窗口,直接进入else语句...

我如何让脚本结束,以便如果我选择$answer -eq "M",那么当我退出脚本时,它将在那儿关闭,然后在该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""

最佳答案

大括号放错了位置。该代码应显示为:

            (Get-WmiObject -computerName $computer1 Win32_Service -Filter "Name='Alerter'").StartService() 
            Write-Host `
        }
}
else
{
    Write-Host "You have chosen "S""
}

关于powershell - 进入IF但退出到ELSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15393095/

相关文章:

html - Powershell:下载或保存整个 ie 页面的源代码

powershell - 为什么此代码将开关参数传递给 PowerShell 函数会失败

powershell - 如何设置 MSMQ 集群队列的权限?

PowerShell 函数参数

.net - 用于返回计算机上 .NET Framework 版本的 PowerShell 脚本?

xml - 确定 XML 节点存在

powershell - PowerShell,Exchange 2010通讯簿

vba - Excel 工作簿连接使文件大小变大

azure - 如何获取 Azure 资源组中存在的磁盘快照列表的 VM 名称

powershell - 显示远程运行的Powershell脚本的实时输出