Created
September 30, 2016 02:45
-
-
Save mattmcnabb/a9acb4957a962d40b880ee8be74dbe5c to your computer and use it in GitHub Desktop.
Trouble with array in PoshRSJob
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set up a simple array | |
$Items = @( | |
[PSCustomObject]@{Name = 'Item1'; SomeProperty = "1"}, | |
[PSCustomObject]@{Name = 'Item2'; SomeProperty = "2"}, | |
[PSCustomObject]@{Name = 'Item3'; SomeProperty = "3"} | |
) | |
### enumerate a property of the objects passed in via -ArgumentList | |
# the output here is always '2' | |
$SB = { | |
param ($Items) | |
$Items.SomeProperty | |
} | |
$RSJob = Start-RSJob -ScriptBlock $SB -ArgumentList $Items | |
$RSJob | Wait-rsJob | Receive-RSJob | |
### try the using variable instead | |
# I get an error here: | |
# Start-RSJob : The value of the using variable '$Using:Items.SomeProperty' cannot be retrieved because it has not been set in the local session. | |
$SB = { | |
param ($Items) | |
$Using:Items.SomeProperty | |
} | |
$RSJob = Start-RSJob -ScriptBlock $SB | |
$RSJob | Wait-rsJob | Receive-RSJob | |
<# | |
PS c:\> $PSVersionTable | |
Name Value | |
---- ----- | |
PSVersion 5.0.10586.117 | |
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} | |
BuildVersion 10.0.10586.117 | |
CLRVersion 4.0.30319.42000 | |
WSManStackVersion 3.0 | |
PSRemotingProtocolVersion 2.3 | |
SerializationVersion 1.1.0.1 | |
PS c:\> Get-Module poshrsjob | |
ModuleType Version Name ExportedCommands | |
---------- ------- ---- ---------------- | |
Script 1.7.2.9 PoshRSJob {Get-RSJob, Receive-RSJob, Remove-RSJob, Start-RSJob...} | |
#> | |
<# | |
Strangely enought, in my actual script I'm passing in an array of AD users and another array and comparing properties of each: | |
foreach ($ADUser in $ADUsers) {if ($DBUser.EmployeeID -eq $ADUser.EmployeeID) {<Do something>}} | |
The if block never executes because $ADUser.EmployeeID is always null. | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm seeing a similar issue with PoshRSJob 1.7.4.4
Outside the RSJobs, I'm passing in an ADUSer object with HomeDirectory and Department properties, but inside each RSJob, the two extra properties are gone.