Job with interactive input

This topic contains 2 replies, has 2 voices, and was last updated by  Grant Harrington 2 years, 5 months ago.

  • Author
    Posts
  • #24371

    Grant Harrington
    Participant

    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>

  • #24379

    Daniel Krebs
    Moderator

    Hi Grant,

    It is not a good practice to have jobs ask for interactive input. Jobs are supposed to run in the background do their "job" (while you or your co-workers are sleeping or doing other important things).

    If your jobs need input please collect the inputs required before starting the jobs, pass the information into the job at start time or use an external sources like a databases or web services to feed the jobs with input.

    Regards
    Daniel

  • #24427

    Grant Harrington
    Participant

    Thanks.

    I did some more research and came up with a solution.

    I created a function that prompted me interactively, and stores the results as global variables. I then passed those to the job using ArgumentList.

    In fact, I'm making my original script less interactive, meaning it's dynamically creating variables, rather than me feeding them line by line, so when I get that completed, the entire thing may be able to fit in the job without the need for the ArgumentList at all.

    I was building a network path with users and Unit directories by entering variables and Switch arrays. I'm now using GCI to recursively search and when it finds the correct path, it adds that to a New-Variable that feeds the portion of the script I wanted in a job.

You must be logged in to reply to this topic.