squareborg
squareborg

Reputation: 1621

Powershell V3 Variables in powershell start-job script block not getting expanded

I'm having a problem with a powershell script. I'm trying to use start-job to run a bunch of test-connection against a list of computers.

I have trimmed the script to the basics this is it,

$cnamesAll=@("localhost","dc01","ex01","dd01")
$cnamesAll | ForEach-Object { start-job { Test-Connection $args[0]}  -ArgumentList "$_"}
Get-Job | % { $_.Command }
Get-Job | Wait-Job 
Remove-Job * 

This is the output I get when running:

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
2916   Job2916         BackgroundJob   Running       True            localhost             Test-Connection $args[0]
2918   Job2918         BackgroundJob   Running       True            localhost             Test-Connection $args[0]
2920   Job2920         BackgroundJob   Running       True            localhost             Test-Connection $args[0]
2922   Job2922         BackgroundJob   Running       True            localhost             Test-Connection $args[0]
 Test-Connection $args[0]
 Test-Connection $args[0]
 Test-Connection $args[0]
 Test-Connection $args[0]
2916   Job2916         BackgroundJob   Completed     True            localhost             Test-Connection $args[0]
2918   Job2918         BackgroundJob   Completed     True            localhost             Test-Connection $args[0]
2920   Job2920         BackgroundJob   Completed     True            localhost             Test-Connection $args[0]
2922   Job2922         BackgroundJob   Completed     True            localhost             Test-Connection $args[0]

As you can see the the $args[0] inside the start-job script block is not getting expanded.

Im running this on Server 2012 and output of $PSVersionTable

S C:\Users\administrator\Documents> $PSVersionTable

Name                           Value                                                                                        
----                           -----                                                                                        
PSVersion                      3.0                                                                                          
WSManStackVersion              3.0                                                                                          
SerializationVersion           1.1.0.1                                                                                      
CLRVersion                     4.0.30319.18408                                                                              
BuildVersion                   6.2.9200.16628                                                                               
PSCompatibleVersions           {1.0, 2.0, 3.0}                                                                              
PSRemotingProtocolVersion      2.2 

Can anybody help I have been searching for over an hour now. Thanks.

Upvotes: 1

Views: 706

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200313

Get-Job | % { $_.Command } shows the commands you're running in the jobs. The command in the script block is Test-Connection $args[0], so that's what the Command property returns. The argument is passed into the job just fine, which you'd see if you retrieved the output from the job:

Get-Job | Wait-Job | Receive-Job

Demonstration:

PS C:\> 'localhost' | % {Start-Job {Test-Connection $args[0]} -ArgumentList $_}

Id  Name   PSJobTypeName  State   HasMoreData  Location  Command
--  ----   -------------  -----   -----------  --------  -------
30  Job30  BackgroundJob  Running True         localhost Test-Connection $args...


PS C:\> Get-Job | % { $_.Command }
 Test-Connection $args[0]
PS C:\> Get-Job | Wait-Job | Receive-Job

Source  Destination  IPV4Address  IPV6Address  Bytes  Time(ms)
------  -----------  -----------  -----------  -----  --------
VM01    localhost    127.0.0.1    ::1          32     0
VM01    localhost    127.0.0.1    ::1          32     0
VM01    localhost    127.0.0.1    ::1          32     0
VM01    localhost    127.0.0.1    ::1          32     0


PS C:\> Remove-Job *

Upvotes: 3

Related Questions