I’ve got a script that has several interactive prompts and ends with a single line of code that uses variables from the interactive responses. When it runs, it could take up to several hours (moving GB of data on a NAS). I want to continue working while that’s running by placing it in a Start-Job block.
If I run this script (for testing the functionality to see results), I start the job, get-job, it’s blocked (waiting for the input). I then receive-job, and then add my text at the prompt, and runs using the $InJob, and then I re-run receive-job, and enter the next part of text. And finally receive-job a third time to see the output.
Start-Job -name InJob -ScriptBlock {
$i = 1
$InJob = Read-Host "Enter World"
$InJob2 = Read-Host "Enter World2"
while [$i -lt 1000]{
Write-Output "Hello $InJob"
Write-Output "Hello $InJob2"
$i++}
}
Id Name PSJobTypeName State HasMoreData Location Command
24 InJob BackgroundJob Running True localhost …
PS C:\Users\grant.harrington\Documents\SCRIPTS\POSH> get-job 24
Id Name PSJobTypeName State HasMoreData Location Command
24 InJob BackgroundJob Blocked True localhost …
PS C:\Users\grant.harrington\Documents\SCRIPTS\POSH> Receive-Job 24
Enter World: job1
PS C:\Users\grant.harrington\Documents\SCRIPTS\POSH> Receive-Job 24
Enter World2: job2
PS C:\Users\grant.harrington\Documents\SCRIPTS\POSH> Receive-Job 24
Hello job1
Hello job2
Hello job1
Is there a way to receive-job, and enter all the prompts at once?
Or place the interactive portion outside the Start-Job and feed the variables to the -scriptblock, like this? (I can’t get this to work)
cls
$InJob3 = Read-Host "Enter World"
$InJob4 = Read-Host "Enter World2"
Start-Job -name InJob2 -ScriptBlock {
$i = 1
while [$i -lt 11]{
Write-Output "Hello $InJob3"
Write-Output "Hello $InJob4"
$i++}
}
Enter World: Job3
Enter World2: job4
Id Name PSJobTypeName State HasMoreData Location Command
28 InJob2 BackgroundJob Running True localhost …
PS C:\Users\grant.harrington\Documents\SCRIPTS\POSH> get-job 28
Id Name PSJobTypeName State HasMoreData Location Command
28 InJob2 BackgroundJob Completed True localhost …
PS C:\Users\grant.harrington\Documents\SCRIPTS\POSH> Receive-Job 28
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
PS C:\Users\grant.harrington\Documents\SCRIPTS\POSH>