将值传递给运行/后台脚本块的最佳方法是什么? [英] What's the best way to pass values to a running/background script block?

查看:39
本文介绍了将值传递给运行/后台脚本块的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过 start-job 启动几个正在运行的脚本块的脚本.

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?

例如,

$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 启动了另一个 PowerShell 进程.是否有任何内置机制可以在 PS 进程之间传递值?

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

推荐答案

您可以使用 MSMQ 来执行此操作.PowerShell V3 附带了一个 MSMQ 模块.下面是如何使用 MSMQ 将消息传递给后台任务的示例:

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            ...

这篇关于将值传递给运行/后台脚本块的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆