Dav*_*ant 8 powershell scope psobject
以下是我想以某种方式实现的目标.
我有一个定义一些对象的自定义程序集.在我的脚本中,我创建了一个自定义对象,我想将其传递给脚本块,保持该对象的行为.
Add-Type -AssemblyName MyCustomDLL
$global:object = new-object MyCustomDLL.MyCustomObject()
$object | gm
$jobWork = { param ($object) $object | gm } # I'd like to keep my object behavior in that block
$job = Start-Job -ScriptBlock $jobWork -ArgumentList $object
Wait-Job $job
Receive-Job $job
Run Code Online (Sandbox Code Playgroud)
我该怎么做或达到同样的效果?谢谢你的帮助
Rom*_*min 10
取而代之的后台作业您可以使用PowerShell与BeginInvoke,EndInvoke.这是在"作业"中传递活动对象的简单但有效的示例,在那里更改它,获得结果:
# live object to be passed in a job and changed there
$liveObject = @{ data = 42}
# job script
$script = {
param($p1)
$p1.data # some output (42)
$p1.data = 3.14 # change the live object data
}
# create and start the job
$p = [PowerShell]::Create()
$null = $p.AddScript($script).AddArgument($liveObject)
$job = $p.BeginInvoke()
# wait for it to complete
$done = $job.AsyncWaitHandle.WaitOne()
# get the output, this line prints 42
$p.EndInvoke($job)
# show the changed live object (data = 3.14)
$liveObject
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4006 次 |
| 最近记录: |