0

I have script which start several running script blocks by start-job.

What's the best approach to pass some variables/values to the running background script block?

There are some options like service broker/queue, files, etc. Is there a lighter way?

For example,

$sb = {
    $Value = $args[0] # initial value
    while ($true) 
    {
        # Get more values from caller
        $Value = .....
    }
}
start-job -ScriptBlock $sb -ArgumentList $initValue
# There are more values to send to the script after the script block is started.
while (moreVaulesAvailable)
{
     # $sb.Value = .... newly generated values ? 
}

Start-Job started another PowerShell process. Is there any built-in mechanism to pass values between PS processes?

1 Answers1

4

You can use MSMQ to do this. There is a MSMQ module that comes with PowerShell V3. Here's an example of how to pass messages to a background task using MSMQ:

$sb = {
    param($queueName)
    $q = Get-MsmqQueue $queueName
    while (1) {
        $messages = @(try {Receive-MsmqQueue -InputObject $q -RetrieveBody} catch {})
        foreach ($message in $messages)
        {
            "Job received message: $($message.Body)"

            if ($message.Body -eq '!quit')
            {
                return
            }
        }
        Start-Sleep -Milliseconds 1000
        "Sleeping..."
    }
}

$queueName = 'JobMessages'
$q = Get-MsmqQueue $queueName
if ($q)
{
    "Clearing the queue $($q.QueueName)"
    $q | Clear-MsmqQueue > $null    
}
else
{
    $q = New-MsmqQueue $queueName
    "Created queue $($q.QueueName)"
}

$job = Start-Job -ScriptBlock $sb -ArgumentList $queueName -Name MsgProcessingJob
"Job started"

$msg = New-MsmqMessage "Message1 for job sent at: $(Get-Date)"
Send-MsmqQueue -Name $q.Path -MessageObject $msg > $null

Receive-Job $job

$msg = New-MsmqMessage "Message2 for job sent at: $(Get-Date)"
Send-MsmqQueue -Name $q.Path -MessageObject $msg > $null

$msg = New-MsmqMessage "!quit"
Send-MsmqQueue -Name $q.Path -MessageObject $msg > $null

Wait-Job $job -Timeout 30
Receive-Job $job
Get-Job $job.Name
Remove-Job $job

When I run this script I get the following output:

C:\PS> .\MsmqQueue.ps1
Clearing the queue private$\jobmessages
Job started

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
4      MsgProcessin... BackgroundJob   Completed     True            localhost            ...
Job received message: Message1 for job sent at: 12/15/2012 17:53:39
Sleeping...
Job received message: Message2 for job sent at: 12/15/2012 17:53:39
Sleeping...
Job received message: !quit
4      MsgProcessin... BackgroundJob   Completed     False           localhost            ...
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 1
    We are still using Windows servers 2003 so it's not possible to install Powershell 3.0. –  Dec 16 '12 at 06:19
  • There are other MSMQ solutions available. In fact, the PowerShell Community Extensions has a set of MSMQ cmdlets. – Keith Hill Dec 16 '12 at 17:00