0

I am new to Powershell. I am trying to find out all local/domain users under all the local groups from different Windows machines.

I found below script for this purpose. When I run this script in Windows 7 Ultimate, it does not get any member properties (e.g. Name, Path) for local groups found in this machine.

 Invoke-Command -ScriptBlock {
[ADSI]$S = "WinNT://$($env:computername)"
$S.children.where({$_.class -eq 'group'}) |

Select @{Name="Members";Expression={
[ADSI]$group = "$($_.Parent)/$($_.Name),group"
$members = $Group.psbase.Invoke("Members")
($members | ForEach-Object {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}) -join ";"}
} 
} | Select Members 

It seems that below line of code does not work(does not fetch properties (e.g. name ) of the user)

ForEach-Object {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}) -join ";"}

Same script works well in Windows 10 and Windows 2012.

I thank you in advance for your help.

Rana9999
  • 1
  • 1

1 Answers1

0

This question was answered here.Copying answer from the link.

There are many issues with your script that will cause bad behavior on different systems and versions.

Here is a general method that works anywhere. DO NOT use Invoke-Command. ADSI works remotely just fine.

$computer = $env:computername
$groups = ([ADSI]"WinNT://$computername").Children | Where{$_.class -eq 'group'}
foreach($group in $groups){
    $group.Invoke("Members") |
        ForEach-Object {
            $type = $_.GetType()
            $name = $type.InvokeMember('Name', 'GetProperty', $null, $_, $null)
            [pscustomobject]@{Group=$group.Name[0];Account=$name}
        }
}
Rana9999
  • 1
  • 1