Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

foreach stopped working after upgrading to powershell 4

32 views
Skip to first unread message

Justin Marshall

unread,
Mar 24, 2015, 11:41:40 AM3/24/15
to
I had built a script that compared the registry of 2 servers and i'm having a very wierd issue with foreach. My script works perfectly fine in powershell 2 (i even tested by running with -version 2) but after uprading to powershell 4 it stopped working. What it does it retrieves the registry for a given path on a remote server and stores that to a variable then iterates on each property to compare. in powershell 4 the iteration variable becomes null yet the ISE tooltip shows there is data.

$_Path = (Invoke-Command -cn $ReferenceServerName -cred $cred {param($KeyPath); Get-Item $KeyPath | get-childitem -recurse} -Args $KeyPath)

# initialize result arrays
$ReferenceServer = @()
$TargetServer= @()
$results = @()

ForEach ($key in $_Path)
{


$key | Get-Member
ForEach ( $Property in $key )
{
................
}
}

The $key | Get-Member returns nothing, the foreach never enters the loop on $Property as it believeles $key is null yet tooltip shows it has data... i'm completely stumped.

any help would be appreciated!

Justin Marshall

unread,
Mar 24, 2015, 11:56:41 AM3/24/15
to
Just noticed a typo above it should be $key.property in the foreach, that is when it stops working

Justin Marshall

unread,
Apr 9, 2015, 6:01:54 PM4/9/15
to
I eventually found a much better way to do this, i bleieve its because the object was retrieved remotely and the sub proeprties were not captured properly - something i've noticed previously.

I solve this by putting all the logic into a function and passing the function to the remote server.

$ReferenceServer = Invoke-Command -ScriptBlock ${function:Get-RegConfiguration} -ArgumentList $KeyPath `
-ComputerName $ReferenceServerName -Credential $cred


hopefully this helps someone in the future as it took a lot of digging to figure out how to pass a locally created function to a remote server without dumping the whole thing as a text script block.
0 new messages