Invoke-Command 期间奇怪的 PowerShell 行为
Strange PowerShell behavior during Invoke-Comand
本来打算在我的脚本中对用户输入进行控制台验证,但遇到了 PS 的奇怪行为,这是脚本:
function Prompt
{
param([string] $prompt, $validationScript)
$rv = $null
do
{
try
{
$input = Read-Host -Prompt $prompt
$rv = Invoke-Command $validationScript -ArgumentList $input
} catch
{
Write-Host "Invalid input, try again"
}
} while (!$rv)
$rv
}
$xxx = Prompt -Prompt "Enter integer" -validationScript {[Convert]::ToInt32($args[0], 10)}
$xxx
它在您输入无效值时工作正常,但当存在有效整数时,它会 returns 值和 PS 挂起。如果我在 ISE 中停止它,则有数千个 "Invalid input, try again",看起来循环永远不会结束但不会调用 Read-Host。什么问题?
现在知道为什么了,但是函数名称 "prompt" 中的问题,如果将其重命名为其他名称则一切正常。
本来打算在我的脚本中对用户输入进行控制台验证,但遇到了 PS 的奇怪行为,这是脚本:
function Prompt
{
param([string] $prompt, $validationScript)
$rv = $null
do
{
try
{
$input = Read-Host -Prompt $prompt
$rv = Invoke-Command $validationScript -ArgumentList $input
} catch
{
Write-Host "Invalid input, try again"
}
} while (!$rv)
$rv
}
$xxx = Prompt -Prompt "Enter integer" -validationScript {[Convert]::ToInt32($args[0], 10)}
$xxx
它在您输入无效值时工作正常,但当存在有效整数时,它会 returns 值和 PS 挂起。如果我在 ISE 中停止它,则有数千个 "Invalid input, try again",看起来循环永远不会结束但不会调用 Read-Host。什么问题?
现在知道为什么了,但是函数名称 "prompt" 中的问题,如果将其重命名为其他名称则一切正常。