[ad_1]
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
[ad_2]