如何修复错误 Wait-Job cmdlet 无法完成工作,因为一个或多个作业被阻止等待用户交互
how to fix the error Wait-Job cmdlet cannot finish working, because one or more jobs are blocked waiting for user interaction
我有一个功能需要用户输入 Y 或 N 来删除文件,我正在尝试 运行 将整个功能作为开始工作功能。我的代码是:
$JobFunction2 = {
function init {
try {
$output = terraform init
$path = Get-Item -Path ".\"
$in = $output | Select-String "Terraform has been successfully initialized!"
if ($in) {
Write-Host -ForegroundColor GREEN 'Initialization successfull'
}
else {
Write-Host -ForegroundColor YELLOW 'Intiialization failed Please enter Y to delete the .terraform folder'
$fail = del $path/.terraform -Force
$output = terraform init
return $fail, $output
}
}
catch {
Write-Warning "Error Occurred while Initializing the folder and the path is: $path"
}
}
}
Start-Job -InitializationScript $JobFunction2 -ScriptBlock { init } | Wait-Job | Receive-Job
当我运行开始作业时,它显示“Wait-Job cmdlet 无法完成工作,因为一个或多个作业被阻止等待用户交互”。但是,如果我只调用函数名称 'init' 而没有开始工作,它就可以完美运行。有什么方法可以提示用户输入 Y 或 No,以便该功能可以在 start-job 中运行?
不确定是什么将您的工作 State
设置为 Blocked
,但如果问题是:
有什么方法可以提示用户输入 Y
或 N
以便函数可以在 Start-Job
?
中运行
答案是肯定的,下面你可以看到一个如何实现的例子,注意我使用的是 while
循环而不是 Wait-Job
.
根据文档,该 cmdlet 有一个带有 <JobState>
参数的 -Sate
参数,但我无法让它工作。甚至尝试过:
Wait-Job -Any -Force -State Blocked
和你一样的错误,所以这里是你可以使用的暴力解决方案:
function init {
'Initializing Function {0}' -f $MyInvocation.MyCommand.Name
do
{
$choice = Read-Host 'Waiting for user input. Continue? [Y/N]'
if($choice -notmatch '^y|n$')
{
'Invalid input.'
}
}
until ($choice -match '^y|n$')
if($choice -eq 'y')
{
'Continue script...'
}
else
{
'Abort script...'
}
}
$funcDef = [scriptblock]::Create("function init { $function:init }")
$job = Start-Job -InitializationScript $funcDef -ScriptBlock { init }
$desiredState = 'Stopped', 'Completed', 'Failed'
while($job.State -notin $desiredState)
{
if($job.State -eq 'Blocked')
{
Receive-Job $job
}
Start-Sleep -Milliseconds 500
}
$job | Receive-Job -Wait -AutoRemoveJob
$fail = Remove-Item $path/.terraform -Force -Recurse -Confirm:$false
这帮助我解决了问题
我有一个功能需要用户输入 Y 或 N 来删除文件,我正在尝试 运行 将整个功能作为开始工作功能。我的代码是:
$JobFunction2 = {
function init {
try {
$output = terraform init
$path = Get-Item -Path ".\"
$in = $output | Select-String "Terraform has been successfully initialized!"
if ($in) {
Write-Host -ForegroundColor GREEN 'Initialization successfull'
}
else {
Write-Host -ForegroundColor YELLOW 'Intiialization failed Please enter Y to delete the .terraform folder'
$fail = del $path/.terraform -Force
$output = terraform init
return $fail, $output
}
}
catch {
Write-Warning "Error Occurred while Initializing the folder and the path is: $path"
}
}
}
Start-Job -InitializationScript $JobFunction2 -ScriptBlock { init } | Wait-Job | Receive-Job
当我运行开始作业时,它显示“Wait-Job cmdlet 无法完成工作,因为一个或多个作业被阻止等待用户交互”。但是,如果我只调用函数名称 'init' 而没有开始工作,它就可以完美运行。有什么方法可以提示用户输入 Y 或 No,以便该功能可以在 start-job 中运行?
不确定是什么将您的工作 State
设置为 Blocked
,但如果问题是:
有什么方法可以提示用户输入 Y
或 N
以便函数可以在 Start-Job
?
中运行
答案是肯定的,下面你可以看到一个如何实现的例子,注意我使用的是 while
循环而不是 Wait-Job
.
根据文档,该 cmdlet 有一个带有 <JobState>
参数的 -Sate
参数,但我无法让它工作。甚至尝试过:
Wait-Job -Any -Force -State Blocked
和你一样的错误,所以这里是你可以使用的暴力解决方案:
function init {
'Initializing Function {0}' -f $MyInvocation.MyCommand.Name
do
{
$choice = Read-Host 'Waiting for user input. Continue? [Y/N]'
if($choice -notmatch '^y|n$')
{
'Invalid input.'
}
}
until ($choice -match '^y|n$')
if($choice -eq 'y')
{
'Continue script...'
}
else
{
'Abort script...'
}
}
$funcDef = [scriptblock]::Create("function init { $function:init }")
$job = Start-Job -InitializationScript $funcDef -ScriptBlock { init }
$desiredState = 'Stopped', 'Completed', 'Failed'
while($job.State -notin $desiredState)
{
if($job.State -eq 'Blocked')
{
Receive-Job $job
}
Start-Sleep -Milliseconds 500
}
$job | Receive-Job -Wait -AutoRemoveJob
$fail = Remove-Item $path/.terraform -Force -Recurse -Confirm:$false
这帮助我解决了问题