在 powershell 中使用 start-job 启动作业时,会返回一个对象 psremotingjob。PsRemotingJob 上的 get-member 为我们提供:
TypeName: System.Management.Automation.PSRemotingJob
Name MemberType Definition
---- ---------- ----------
[...]
Progress Property System.Management.Automation.PSDataCollection`...
StatusMessage Property System.String StatusMessage {get;}
Verbose Property System.Management.Automation.PSDataCollection`...
Warning Property System.Management.Automation.PSDataCollection`...
State ScriptProperty System.Object State {get=$this.JobStateInfo.St...
所以我想知道我是否可以从工作本身更新属性“进度”?我建立了 progressRecord 集合,但我不知道如何从内部获取作业的属性。
$VMlist = @("VM1","VM2")
foreach($VM in $VMlist)
{
$j = start-job -name $VM -argumentlist @($path,$VM) -ScriptBlock {
$psdatacollectionExample = New-Object 'System.Management.Automation.PSDataCollection`1[System.Management.Automation.ProgressRecord]'
$progressRecord = New-Object System.Management.Automation.ProgressRecord(1,"Task1","Installing")
for($i=0;$i -lt 5; $i++)
{
$progressRecord.PercentComplete = $i * 20
$psdatacollectionExample.Add($progressRecord)
#something like super.Progess = $psdatacollectionExample
}
}
}