Powershell将参数传递给嵌套的Start-Job

时间:2017-05-09 19:06:41

标签: powershell parameter-passing start-job scriptblock

我试图将参数传递给嵌套作业。

$executepath = "D:\nttools\CoreAutomation\$patch.zipfilename\$ntupdatefilename"

$sb = {Start-Job -Name $NTUpdate -ScriptBlock {& cmd.exe /c $executepath}}

Invoke-Command -Session $NTSession -ScriptBlock $sb -ArgumentList $executepath

使用此输出显示命令实际上不在字符串中呈现参数:

State          : Running
PSComputerName : LKS.nt.isg.local
RunspaceId     : 8a1c9cc2-1cd0-42a6-a1d0-89d977aabf04
HasMoreData    : True
StatusMessage  :
Location       : localhost
Command        : & cmd.exe /c $executepath
JobStateInfo   : Running
InstanceId     : 9e95c0d8-d177-4a1a-9283-56f07ff5f0a8
Id             : 1
Name           : Job1
ChildJobs      : {System.Management.Automation.PSRemotingChildJob}
PSJobTypeName  : BackgroundJob
PSBeginTime    : 5/9/2017 2:00:49 PM
PSEndTime      :

我试图在脚本块中添加这些参数,但结果相同:

$executepath = "D:\nttools\CoreAutomation\$patch.zipfilename\$ntupdatefilename"

$sb = {Start-Job -Name $NTUpdate -ScriptBlock {param($executepath) & cmd.exe /c $executepath} -ArgumentList $executepath}

Invoke-Command -Session $NTSession -ScriptBlock $sb -ArgumentList $executepath


State          : Running
PSComputerName : LKS.nt.isg.local
RunspaceId     : 7e039382-d8a6-4298-9983-8f3f6fd2a6c3
HasMoreData    : True
StatusMessage  :
Location       : localhost
Command        : param($executepath) & cmd.exe /c $executepath
JobStateInfo   : Running
InstanceId     : 77e80c13-2801-4726-81f4-8c960319cd0b
Id             : 1
Name           : Job1
ChildJobs      : {System.Management.Automation.PSRemotingChildJob}
PSJobTypeName  : BackgroundJob
PSBeginTime    : 5/9/2017 2:08:38 PM
PSEndTime      :

更新

尝试了评论中列出的内容。没有运气,结果相同

$executepath = "D:\nttools\CoreAutomation\$patch.zipfilename\$ntupdatefilename"

$sb = {param($executepath) Start-Job -Name NTUpdate -ScriptBlock {param($executepath) & cmd.exe /c $executepath} -ArgumentList $executepath}

Invoke-Command -Session $NTSession -ScriptBlock  $sb -ArgumentList $executepath


State          : Running
PSComputerName : LKS.nt.isg.local
RunspaceId     : 7b7b86df-a216-494e-b7e7-2336e6994a06
HasMoreData    : True
StatusMessage  :
Location       : localhost
Command        : param($executepath) & cmd.exe /c $executepath
JobStateInfo   : Running
InstanceId     : 4d5dd307-2158-4b38-b118-987f1e56cb12
Id             : 1
Name           : NTUpdate
ChildJobs      : {System.Management.Automation.PSRemotingChildJob}
PSJobTypeName  : BackgroundJob
PSBeginTime    : 5/9/2017 2:25:19 PM
PSEndTime      :

更新2 - $ using:executepath

Invoke-Command -Session $NTSession -ScriptBlock {Start-Job -Name NTUpdate -ScriptBlock {& cmd.exe /c $Using:executepath}}

我试过了,我收到了这个错误:

The value of the using variable '$using:executepath' cannot be retrieved because it has not been set in the local
session.
    + CategoryInfo          : InvalidOperation: (:) [Start-Job], RuntimeException
    + FullyQualifiedErrorId : UsingVariableIsUndefined,Microsoft.PowerShell.Commands.StartJobCommand
    + PSComputerName        : LKSNTADM01.nt.isg.local

1 个答案:

答案 0 :(得分:0)

好的,所以如果我理解正确的话,你的主要问题是,当你看到作业中的命令时,你会看到:

& cmd.exe /c $executepath

如果您希望看到:

& cmd.exe /c D:\nttools\CoreAutomation\Defect1126511_CD_2017-04-24_1800_A‌​\nttest_genlog.cmd

scriptblock不会插入字符串变量,所以如果你想看到它全部扩展了你可以做的是将scriptblock定义为一个字符串,然后将其转换为一个scriptblock。

$sbtext = "Start-Job -Name $NTUpdate -ScriptBlock {& cmd.exe /c $executepath}"
$sb = [scriptblock]::Create($sbtext)

那应该能得到你想要的结果。