PowerShell remoting

Welcome Forums General PowerShell Q&A PowerShell remoting

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

 
Participant
2 months, 3 weeks ago.

  • Author
    Posts
  • #108878

    Participant
    Points: 0
    Rank: Member

    Hi, I have to retrieve information about version and parameters of Log Insight Agent application on all machines in the domain. Having written script everything works fine but I am interested in approach I used. Information about application version I retrieve from HKLM whilst parameters are stored in .ini file so I used Get-Content ... | Select-String ... to search for particular parameter names.

    $logInsightAgentVersion = Invoke-Command -Session $session -ScriptBlock {Get-ItemProperty -Path HKLM:\...}

    $logInsightAgentParams = Invoke-Command -Session $session -ScriptBlock {Get-Content ... | Select-String ...}

    I know Get-ItemProperty and Get-Content ... could be put in single ScriptBlock of Invoke-Command -Session $session but then how would I reference to results they produce?

    Is the logic below right way?

    $logInsightAgentVersion,$logInsightAgentParams = Invoke-Command -Session $session -ScriptBlock {

    Get-ItemProperty ...                                                                                                                                      Get-Content ...

    }

  • #108884

    Participant
    Points: 527
    Helping Hand
    Rank: Major Contributor

    You can make use of the powerful PSCUstomObjects(the beauty of PowerShell).

    an example below.

        #Get it like below
        $Output = Invoke-Command -Session $Session -ScriptBlock {
                    $PropertyHash = @{
                        Something      = (Get-Something -SomeParam SomeValue)
                        SomeOtherthing = (Get-SomeOtherthing -SomeOtherParam SomeOtherValue)
                    }
    
                    New-Object -TypeName PSObject -Property $PropertyHash
                  }
    
        #Consume it lke below.
        $Output.Something           
        $Output.SomeOtherthing
    
    • #108928

      Participant
      Points: 22
      Rank: Member

      We could shorten this code a bit, and cast directly as well:

      `

      #Get it like below

      $Output = Invoke-Command -Session $Session -ScriptBlock {

      $Properties = [pscustomobject]@{
      Something      = (Get-Something -SomeParam SomeValue)
      SomeOtherthing = (Get-SomeOtherthing -SomeOtherParam SomeOtherValue)
      }

      }

      #Consume it lke below.

      $Output.Something

      $Output.SomeOtherthing

      `

  • #108931

    Participant
    Points: 527
    Helping Hand
    Rank: Major Contributor

    @Stephen , You could use gist (via github) or the 'pre' tags for posting script in the forum.

    I use New-Object as [PSCustomObject] way doesn't work in V 2.0. I know 2.0 is not recommended , but still there are many environments who can't upgrade to later versions due to various reasons.

  • #108998

    Participant
    Points: 0
    Rank: Member

    Ok guys, thank you very much.

You must be logged in to reply to this topic.