Invoke-Command to Local Variable

This topic contains 6 replies, has 2 voices, and was last updated by Profile photo of carl trevett carl trevett 4 months, 3 weeks ago.

  • Author
    Posts
  • #46633
    Profile photo of carl trevett
    carl trevett
    Participant

    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

  • #46637
    Profile photo of Craig Duff
    Craig Duff
    Participant
    $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 reply was modified 4 months, 3 weeks ago by Profile photo of Craig Duff Craig Duff.
    • This reply was modified 4 months, 3 weeks ago by Profile photo of Craig Duff Craig Duff.
  • #46641
    Profile photo of carl trevett
    carl trevett
    Participant

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

    • #46643
      Profile photo of Craig Duff
      Craig Duff
      Participant
      $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.

  • #46645
    Profile photo of carl trevett
    carl trevett
    Participant

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

    • #46647
      Profile photo of Craig Duff
      Craig Duff
      Participant

      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.

      • This reply was modified 4 months, 3 weeks ago by Profile photo of Craig Duff Craig Duff.
      • This reply was modified 4 months, 3 weeks ago by Profile photo of Craig Duff Craig Duff.
  • #46652
    Profile photo of carl trevett
    carl trevett
    Participant

    Thank you so much Craig..it worked 🙂

    Now to get it into HTML...

You must be logged in to reply to this topic.