0

I am attempting to pass paramters to a nested job.

$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

With this output showing the command is not actually rendering the parameter in the string:

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      :

I have tried to add this parameters in the script block as well with the same result:

$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      :

Updated:

Tried what was listed in comments. No luck, same result.

$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      :

Update 2 -$using:executepath

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

I have tried with and I get this error:

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
BilliAm
  • 590
  • 2
  • 6
  • 26
  • I believe that for the `Invoke-Command`, you need to reference the argument within the script block as `$using:executepath`. – Jeff Zeitlin May 09 '17 at 19:09
  • `$sb = {param($executepath) Start-Job -Name $NTUpdate -ScriptBlock {param($executepath) & cmd.exe /c $executepath} -ArgumentList $executepath}` – user4003407 May 09 '17 at 19:15
  • @PetSerAl Tried with same output as before. Nogo. – BilliAm May 09 '17 at 19:30
  • Well, let's back up just a little. Are you trying to reference the `ZipFileName` property of the `$patch` object? Or is `$patch` a string? Are you sure that `$executepath` is a valid string? – TheMadTechnician May 09 '17 at 20:55
  • @TheMadTechnician `$patch` object is a hashtable. `$ntupdatefilename` is also a string. The string in `$executepath` renders correctly outside of the script block. – BilliAm May 09 '17 at 21:17
  • Also @TheMadTechnician I see what you're saying with `$executepath` and `$patch` being a hashtable. It was malforming the path, BUT that still doesn't matter in this case as it should still show that malformed path in the command property value. – BilliAm May 09 '17 at 21:35
  • @BilliAm Sorry, but what are you expecting to see in the `Command` property if not the command you pass to `Start-Job`? – user4003407 May 09 '17 at 21:45
  • I am expecting to see `& cmd.exe /c D:\nttools\CoreAutomation\Defect1126511_CD_2017-04-24_1800_A\nttest_genlog.cmd` in the command since that's what $executepath is set to. I am not able to get script blocks to accept parameters and render them as shown. – BilliAm May 09 '17 at 21:55
  • @JeffZeitlin Tried `$using:` – BilliAm May 09 '17 at 22:06
  • 1
    @BilliAm Your expectation are wrong. In command property you should see `$executepath` not the value it set to. – user4003407 May 09 '17 at 22:08

1 Answers1

1

Ok, so if I understand correctly, your main issue here is that when you look at the command in the job you see:

& cmd.exe /c $executepath

When you expect to see:

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

The scriptblock will not interpolate the string variable, so if you want to see it all expanded what you can do is define the scriptblock as a string, and then convert it to a scriptblock.

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

That should get you the results that you desire.

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56