停止 PowerShell 脚本或重新询问 Right-Host 的最佳方法
Best way to Stop a PowerShell Script or to re-ask the Right-Host
这是我的代码。感觉真的很脏,我有一长串我要问的 Right-Host 问题。
如果添加了 Null、空白或其他空条目(包括仅添加 space),我想停止脚本。现在我已经让它停止了,但希望有一种更简洁的方法来对 "If" 语句进行分组,并且如果上述任何条目发生,也许会重新提出问题...
$Parent = Read-Host -prompt "Enter full parent path that will contain the new folder"
if ( $Parent -eq $null)
{
Write-Host "You entered a blank value: This Script is now Exiting."
Exit
}
if ( $Parent -eq "")
{
Write-Host "You entered a blank value: This Script is now Exiting."
Exit
}
if ( $Parent -eq " ")
{
Write-Host "You entered a blank value: This Script is now Exiting."
Exit
}
else
{
Write-Host "You Entered $Parent"
}
$Name = Read-Host -prompt "Enter Folder Name"
if ( $Name -eq $null)
{
Write-Host "You entered a blank value: This Script is now Exiting."
Exit
}
if ( $Name -eq "")
{
Write-Host "You entered a blank value: This Script is now Exiting."
Exit
}
if ( $Name -eq " ")
{
Write-Host "You entered a blank value: This Script is now Exiting."
Exit
}
else
{
Write-Host "You Entered $Name"
}
您可以像这样将验证条件放在一起:
$Parent = Read-Host -prompt "Enter full parent path that will contain the new folder"
if ( ($Parent -eq $null) -or ($Parent -eq "") -or ($Parent -eq " "))
{
Write-Host "You entered a blank value: This Script is now Exiting."
}
else
{
Write-Host "You Entered $Parent"
}
您可以使该函数成为一个带有强制性非空参数的函数(正如 bill 在长 运行 上所说的很多 better/easier),或者进行 while 循环直到输入正确。
这是我的代码。感觉真的很脏,我有一长串我要问的 Right-Host 问题。
如果添加了 Null、空白或其他空条目(包括仅添加 space),我想停止脚本。现在我已经让它停止了,但希望有一种更简洁的方法来对 "If" 语句进行分组,并且如果上述任何条目发生,也许会重新提出问题...
$Parent = Read-Host -prompt "Enter full parent path that will contain the new folder"
if ( $Parent -eq $null)
{
Write-Host "You entered a blank value: This Script is now Exiting."
Exit
}
if ( $Parent -eq "")
{
Write-Host "You entered a blank value: This Script is now Exiting."
Exit
}
if ( $Parent -eq " ")
{
Write-Host "You entered a blank value: This Script is now Exiting."
Exit
}
else
{
Write-Host "You Entered $Parent"
}
$Name = Read-Host -prompt "Enter Folder Name"
if ( $Name -eq $null)
{
Write-Host "You entered a blank value: This Script is now Exiting."
Exit
}
if ( $Name -eq "")
{
Write-Host "You entered a blank value: This Script is now Exiting."
Exit
}
if ( $Name -eq " ")
{
Write-Host "You entered a blank value: This Script is now Exiting."
Exit
}
else
{
Write-Host "You Entered $Name"
}
您可以像这样将验证条件放在一起:
$Parent = Read-Host -prompt "Enter full parent path that will contain the new folder"
if ( ($Parent -eq $null) -or ($Parent -eq "") -or ($Parent -eq " "))
{
Write-Host "You entered a blank value: This Script is now Exiting."
}
else
{
Write-Host "You Entered $Parent"
}
您可以使该函数成为一个带有强制性非空参数的函数(正如 bill 在长 运行 上所说的很多 better/easier),或者进行 while 循环直到输入正确。