# Invoke-Command to Local Variable

**URL:** <https://forums.powershell.org/t/invoke-command-to-local-variable/6798>\
**Category:** PowerShell Help\
**Created:** [July 12, 2016, 2:22pm UTC](https://forums.powershell.org/t/invoke-command-to-local-variable/6798 "2016-07-12T14:22:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![carl-trevett](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@carl-trevett](https://forums.powershell.org/u/carl-trevett)\
**Post date:** [July 12, 2016, 2:22pm UTC](https://forums.powershell.org/t/invoke-command-to-local-variable/6798/1 "2016-07-12T14:22:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![powershellish](https://avatars.discourse-cdn.com/v4/letter/p/b5ac83/32.png) [@powershellish](https://forums.powershell.org/u/powershellish)\
**Post date:** [July 12, 2016, 2:29pm UTC](https://forums.powershell.org/t/invoke-command-to-local-variable/6798/2 "2016-07-12T14:29:29Z")

</div>

```
$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.

---

<div class="post-metadata">

**Author:** ![carl-trevett](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@carl-trevett](https://forums.powershell.org/u/carl-trevett)\
**Post date:** [July 12, 2016, 2:44pm UTC](https://forums.powershell.org/t/invoke-command-to-local-variable/6798/3 "2016-07-12T14:44:27Z")

</div>

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  
}}}

---

<div class="post-metadata">

**Author:** ![powershellish](https://avatars.discourse-cdn.com/v4/letter/p/b5ac83/32.png) [@powershellish](https://forums.powershell.org/u/powershellish)\
**Post date:** [July 12, 2016, 2:59pm UTC](https://forums.powershell.org/t/invoke-command-to-local-variable/6798/4 "2016-07-12T14:59:30Z")

</div>

```
$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.

---

<div class="post-metadata">

**Author:** ![carl-trevett](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@carl-trevett](https://forums.powershell.org/u/carl-trevett)\
**Post date:** [July 12, 2016, 3:04pm UTC](https://forums.powershell.org/t/invoke-command-to-local-variable/6798/5 "2016-07-12T15:04:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![powershellish](https://avatars.discourse-cdn.com/v4/letter/p/b5ac83/32.png) [@powershellish](https://forums.powershell.org/u/powershellish)\
**Post date:** [July 12, 2016, 3:10pm UTC](https://forums.powershell.org/t/invoke-command-to-local-variable/6798/6 "2016-07-12T15:10:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![carl-trevett](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@carl-trevett](https://forums.powershell.org/u/carl-trevett)\
**Post date:** [July 12, 2016, 3:45pm UTC](https://forums.powershell.org/t/invoke-command-to-local-variable/6798/7 "2016-07-12T15:45:00Z")

</div>

Thank you so much Craig…it worked 🙂

Now to get it into HTML…

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/invoke-command-to-local-variable/6798/8 "2024-05-16T20:40:53Z")

</div>


