如何执行一个PowerShell功能并行几次 [英] How to execute a Powershell function several times in parallel

查看:1008
本文介绍了如何执行一个PowerShell功能并行几次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道需要多线程,工作为主,或是否异步调用此,但基本上我有一个PowerShell脚本函数,它接受几个参数,我需要使用不同的参数,并多次调用有这些并行运行。

目前,我这样调用该函数:

 执行参数1参数2参数3,param4

我怎么能称之为多次,而无需等待每次调用执行返回给调用者?

目前我跑2.0版,但如果需要的话我可以更新

编辑:这里是我迄今为止,它不工作:

  $ CMD = {
    param($vmxFilePath,$machineName,$username,$password,$scriptTpath,$scriptFile,$uacDismissScript,$snapshotName)
    执行$ vmxFilePath $ MACHINENAME $ $的用户名密码$ scriptTpath $脚本文件$ uacDismissScript $ snapshotName
}启动工作-ScriptBlock $ CMD -ArgumentList $ vmxFilePath,$计算机名,用户名$密码$,$ scriptTpath,$脚本文件,$ uacDismissScript,$ snapshotName

我得到一个错误:

 不能'System.Object的[]'转换为'initializationscript'参数所需的类型system.management.automation.scriptblock。不支持指定的方法

EDIT2:我修改我的剧本,但我仍然得到上述错误。这里是我的MOD:

  $ CMD = {
    param($vmxFilePath,$machineName,$username,$password,$scriptTpath,$scriptFile,$uacDismissScript,$snapshotName)
    执行$ vmxFilePath $ MACHINENAME $ $的用户名密码$ scriptTpath $脚本文件$ uacDismissScript $ snapshotName
}启动工作-ScriptBlock $ CMD -ArgumentList $ vmxFilePath,$计算机名,用户名$密码$,$ scriptTpath,$脚本文件,$ uacDismissScript,$ snapshotName


解决方案

没有必要此更新。定义一个脚本块,并使用启动作业来根据需要运行该脚本块多次。例如:

  $ CMD = {
  参数($ A,$ B)
  写主机$ A $ B
}$富=富1..5 |的foreach对象{
  启动工作-ScriptBlock $ CMD -ArgumentList $ _,$ foo的
}

该脚本块需要两个参数 $ A $ B 这是由 -ArgumentList 选项。在上面的例子中,分配是 $ _ → $ A $ foo的→ $ B $ foo的仅仅是一个例子的配置的,但静态参数。

运行 GET-招聘|删除 - 作业在某些时候从队列中删除已完成作业(或 GET-招聘|%{接收在职$ _标识;删除在职$ _标识} 如果要检索输出)。

I'm not sure whether to call this a need for multi-threading, job-based, or async, but basically I have a Powershell script function that takes several parameters and I need to call it several times with different parameters and have these run in parallel.

Currently, I call the function like this:

Execute "param1" "param2" "param3" "param4"

How can I call this multiple times without waiting for each call to Execute return to the caller?

Currently I'm running v2.0 but I can update if necessary

EDIT: here's what I have so far, which doesn't work:

$cmd = {
    param($vmxFilePath,$machineName,$username,$password,$scriptTpath,$scriptFile,$uacDismissScript,$snapshotName)
    Execute $vmxFilePath $machineName $username $password $scriptTpath $scriptFile $uacDismissScript $snapshotName
}

Start-Job -ScriptBlock $cmd -ArgumentList $vmxFilePath, $machineName, $username $password, $scriptTpath, $scriptFile, $uacDismissScript, $snapshotName

I get an error:

cannot convert 'system.object[]' to the type 'system.management.automation.scriptblock' required by parameter 'initializationscript'. specified method is not supported

EDIT2: I've modified my script but I still get the error mentioned above. Here's my mod:

    $cmd = {
    param($vmxFilePath,$machineName,$username,$password,$scriptTpath,$scriptFile,$uacDismissScript,$snapshotName)
    Execute $vmxFilePath $machineName $username $password $scriptTpath $scriptFile $uacDismissScript $snapshotName
}

Start-Job -ScriptBlock $cmd -ArgumentList $vmxFilePath, $machineName, $username $password, $scriptTpath, $scriptFile, $uacDismissScript, $snapshotName

解决方案

No update necessary for this. Define a script block and use Start-Job to run the script block as many times as necessary. Example:

$cmd = {
  param($a, $b)
  Write-Host $a $b
}

$foo = "foo"

1..5 | ForEach-Object {
  Start-Job -ScriptBlock $cmd -ArgumentList $_, $foo
}

The script block takes 2 parameters $a and $b which are passed by the -ArgumentList option. In the example above, the assignments are $_$a and $foo$b. $foo is just an example for a configurable, but static parameter.

Run Get-Job | Remove-Job at some point to remove the finished jobs from the queue (or Get-Job | % { Receive-Job $_.Id; Remove-Job $_.Id } if you want to retrieve the output).

这篇关于如何执行一个PowerShell功能并行几次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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