How to retrieve Output from Remote Job

Welcome Forums General PowerShell Q&A How to retrieve Output from Remote Job

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
2 years, 2 months ago.

  • Author
    Posts
  • #56101

    Participant
    Points: 1
    Rank: Member

    Howdy...

    I am trying to query a registry on several 2008 R2 machines using ICM and -asjob parameter. I use the following code

    icm -computername $_ { $x = "hklm:\software\policies\citrix\ima"; write-output "$($env:computername), $($x.loadevaluator)" }

    I then use, get-job | wait-job | receive-job

    Is there a better way to do this.

  • #56102

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    That's what I'd probably do. What do you feel is wrong with it?

    • #56138

      Participant
      Points: 1
      Rank: Member

      Is there a way that each icm session can return an object, so that I can use export-csv to format the output in a more cleaner way.

      Current approach looks very bland.

  • #56162

    Participant
    Points: 0
    Rank: Member

    you don't try to return object and you doesn't get object 🙂
    tro to use [PSCustomObject] or New-Object instead of just write-output $somearray
    the examples everywhere...

    btw, Invoke-Command already return Computername as PSComputerName

  • #56170

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    We don't worry about "bland" in PowerShell :).

    Each job that you create using Invoke-Command actually has one child job per computer. There's a parameter of Show-Job that will let you see the child jobs, in fact. So you can retrieve one "session" (child job) at a time.

    The results of each job are whatever your code outputs.

    icm -computername $_ { $x = "hklm:\software\policies\citrix\ima"; write-output "$($env:computername), $($x.loadevaluator)" }
    

    Right now, you're outputting a string object, so that's what the job's results will be. If you want something "cleaner" in terms of PowerShell:

    icm -computername $_ { $x = "hklm:\software\policies\citrix\ima"
     New-Object -Type PSObject -Prop @{'ComputerName'=($env:computername) ; 'LoadEvaluator' = $x.loadevaluator }
    }
    

    That'll produce an object having two properties.

  • #56207

    Participant
    Points: 21
    Rank: Member

    Start-job with a name would be easier. When you get into complex stuff like job monitoring, tracking, updating grids it'll come in handy.

    • #56218

      Participant
      Points: 1
      Rank: Member

      Thank you very much, Don.

The topic ‘How to retrieve Output from Remote Job’ is closed to new replies.