Skip to content

Instantly share code, notes, and snippets.

@mattmcnabb
Created September 30, 2016 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmcnabb/a9acb4957a962d40b880ee8be74dbe5c to your computer and use it in GitHub Desktop.
Save mattmcnabb/a9acb4957a962d40b880ee8be74dbe5c to your computer and use it in GitHub Desktop.
Trouble with array in PoshRSJob
# 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.
#>
@AspenForester
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment