Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 进入IF但退出到ELSE_Powershell_Powershell 2.0_Powershell 3.0 - Fatal编程技术网

Powershell 进入IF但退出到ELSE

Powershell 进入IF但退出到ELSE,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,今天刚开始写一个脚本,但在我进入它的主要核心之前,当我运行下面的并选择M作为multiple时-IF语句转到M,然而,当它弹出一个窗口来请求路径时,我故意关闭窗口,它会出错并直接转到else语句 如何让脚本结束,如果我选择$answer-eq M,那么当我退出脚本时,它将在那里关闭,然后在if语句中关闭,而不是继续执行ELSE语句 我希望以上所说的有道理——想不出更好的解释方法 此外,我对以下陈述不满意: while("S","M" -notcontains $answer) 问题是,如果一个

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

如何让脚本结束,如果我选择$answer-eq M,那么当我退出脚本时,它将在那里关闭,然后在if语句中关闭,而不是继续执行ELSE语句

我希望以上所说的有道理——想不出更好的解释方法

此外,我对以下陈述不满意:

while("S","M" -notcontains $answer)
问题是,如果一个人输入了其他内容,他只会继续弹出问题。如果有人输入了不正确的值,然后要求用户输入S或M,我如何出错

代码:

错误:

> 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""
}

你的花括号放错地方了。守则应改为:

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

@不,我是说它放错地方了。在OP的代码中,大括号是平衡的。@AthomSfere:不,我的意思是它放错地方了。在OP的代码中,大括号是平衡的。