PowerShell remoting

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 …

}

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

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

@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.

Ok guys, thank you very much.