Home > Software engineering >  Invoke-Command doesn't return objects based on PSObject properties
Invoke-Command doesn't return objects based on PSObject properties

Time:05-31

Working with the script below whose job it is to reach out to remote machines and pulls the membership of the local group and then create custom PS objects to sort the data as I need. The code works fine when pulling from the local machine and the PSCustomObject is honored. However, on a remote call, the objects do not get stored properly in the PScustomObjects.

Looking for some insight/help to return these in the proper format

$params = $args
$Target = $args[0]
$PrivUser = "$($params[1])\$($params[2])"
$PrivPwd = ConvertTo-SecureString -String $params[3] -AsPlainText -Force
$cred = [pscredential]::new($PrivUser,$PrivPwd)
$Groups =  @('Administrators','Remote Desktop Users','Power Users')
$results = @()
try {
    Invoke-Command -ComputerName $Target -Credential $cred -HideComputerName -ScriptBlock {
          #Change $using:groups to $groups to pull local objects
        foreach($group in $using:Groups) 
        {
        foreach($member in Get-LocalGroupMember -Name $group) {
            [pscustomobject]@{
                Machine     = $env:COMPUTERNAME
                Group       = $group
                Member      = $member.Name
            }
        }
            $results  = $output
        }
        return $results 
        
    }
 

} catch {
    throw "Unable to connect to target: $($args[0]) `n$_"
}

Output from local machine

Machine Group                Member               
------- -----                ------               
BLT-SRV Administrators       BLT\Domain Admins    
BLT-SRV Administrators       BLT\svr.blt.ad.ss    
BLT-SRV Administrators       BLT\svr.blt.ss.ps    
BLT-SRV Administrators       BLT\svr.kiwi         
BLT-SRV Administrators       BLT-SRV\Administrator
BLT-SRV Administrators       BLT-SRV\BLT_Admin    
BLT-SRV Remote Desktop Users BLT\cpopopo  

Output from remote machine

Machine    : BLT-SRV
Group      : Administrators
Member     : BLT\Domain Admins
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

Machine    : BLT-SRV
Group      : Administrators
Member     : BLT\svr.blt.ad.ss
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

Machine    : BLT-SRV
Group      : Administrators
Member     : BLT\svr.blt.ss.ps
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

Machine    : BLT-SRV
Group      : Administrators
Member     : BLT\svr.kiwi
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

Machine    : BLT-SRV
Group      : Administrators
Member     : BLT-SRV\Administrator
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

Machine    : BLT-SRV
Group      : Administrators
Member     : BLT-SRV\BLT_Admin
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

Machine    : BLT-SRV
Group      : Remote Desktop Users
Member     : BLT\hgdghdh
RunspaceId : 81840b66-4a15-467f-956f-1552b068847d

CodePudding user response:

Excluding the RunspaceId property should, by default, display the object as a Table:

Invoke-Command -ComputerName $Target -Credential $cred -HideComputerName -ScriptBlock {
    foreach($group in $using:Groups) {
        foreach($member in Get-LocalGroupMember -Name $group) {
            [pscustomobject]@{
                Machine     = $env:COMPUTERNAME
                Group       = $group
                Member      = $member.Name
            }
        }
    }
} | Select-Object * -ExcludeProperty RunspaceId

Alternatively, you can pipe the result of Invoke-Command to Format-Table to force the object to be displayed as table:

Invoke-Command -ComputerName $Target -Credential $cred -HideComputerName -ScriptBlock {
    # code here
} | Format-Table -AutoSize

However, do note, Format-Table is meant exclusively for console display, do not use it if you want to export the output.

CodePudding user response:

You're running up against serialization. See this post to understand what is happening.

CodePudding user response:

I'm not sure I understood your problem, but I think you're getting the same result, but it's being shown differently. You can force table view (your first output example) using Format-Table (or simply ft). To get a list view (the second one), use Format-List (or fl). Both commands below return the same result, but shows it in a different way:

Get-ChildItem -Path c:\ | Format-Table

Get-ChildItem -Path c:\ | Format-List

  • Related