Invoke-Command to Local Variable

Hi

New to PowerShell, I was wondering when using Invoke-command against a number of machines is there away to return the data back to a variable on the local session? I get when a need to bring a variable from the local machine to the remote session you need to use param with -argumentlist but don’t get it vice versa.

Thanks
Carl

$data = Invoke-Command -ComputerName compy -ScriptBlock {Get-ChildItem c:\}

Just a simple assignment, excepting that the $data will have extra properties that invoke-command adds in. One of those properties is the computer name, so that is what you can use to filter out the results per the computer that returned them. $data will hold the results of all computers it is ran against.

this works local for one machine


$Array = @()

$Groups = get-pvsauthgroup

Foreach ($Group in $Groups.AuthGroupName) {

$PVSGroup = get-pvsauthgroup -AuthGroupName $Group | Select-Object AuthGroupName, Description

$PVSGroupUsage = Get-PvsAuthGroup -AuthGroupName $Group | Get-PvsAuthGroupUsage | Select-Object Name, Role

$PVSSite = Get-PVSSite | Select-Object SiteName

$Props = [ordered]@{‘PVS Site’=$PVSSite.SiteName;
‘AuthGroupName’=$PVSGroup.AuthGroupName;
‘Description’=$PVSGroup.Description;
‘Permission’=$PVSGroupUsage.Name;
‘Role’=$PVSGroupUsage.Role

}
$obj = New-Object -TypeName PSObject -Property $Props
$array += $obj
}
$array


this is what I am trying remotely for a few machines

$Array = @()
Foreach ($PVServer in $PVServers.name) { Invoke-Command -Computername $PVSServer -ScriptBlock {
Import-Module ‘C:\Program Files\Citrix\Provisioning Services Console\Citrix.PVS.SnapIn.dll’

$Groups = get-pvsauthgroup
Foreach ($Group in $Groups.AuthGroupName) {
$PVSGroup = get-pvsauthgroup -AuthGroupName $Group | Select-Object AuthGroupName, Description
$PVSGroupUsage = Get-PvsAuthGroup -AuthGroupName $Group | Get-PvsAuthGroupUsage | Select-Object Name, Role
$Props = [ordered]@{‘PVS Site’=$PVSSite.SiteName;
‘AuthGroupName’=$PVSGroup.AuthGroupName;
‘Description’=$PVSGroup.Description;
‘Permission’=$PVSGroupUsage.Name;
‘Role’=$PVSGroupUsage.Role
}
$obj = New-Object -TypeName PSObject -Property $Props
$array += $obj
}}}

$Array =  Invoke-Command -Computername $PVSServers.name -ScriptBlock {
#...
New-Object -TypeName PSObject -Property $Props
}#...

You are not going to be able to reference the memory space on the local computer from the remote, you’ll have to catch the returned values.

sorry…not sure I understand, have you given me something to try or are you saying its not possible? sorry little confused.

Its not possible the way you are trying it, because $array is in the memory of the local computer and the remote computer can’t access $array. So,

$obj = New-Object #...
$array += $obj

cannot work.

You have to modify the script block to return a value, hence my examples change of not assigning the result of New-Object to $obj and just having New-Object by itself. Then on the local computer side, assigning the results to a variable. My example was something to try with everything I didn’t change commented out so to highlight my suggested changes.

Thank you so much Craig…it worked :slight_smile:

Now to get it into HTML…