gpt4 book ai didi

powershell - 进入IF但退出到ELSE

转载 作者:行者123 更新时间:2023-12-03 00:53:34 24 4
gpt4 key购买 nike

今天才刚刚开始编写脚本,但是在进入下面的脚本并选择多个“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com