Get DisplayName of domain users who are members of local groups

Y.Ziya Güleç 1 Reputation point
2021-09-01T13:19:21.823+00:00

I have a script which lists all local groups and their members and give CSV output.

I modified script to get displayName property of domain user members of local groups (Not domain groups not local groups and not local users. Just the displayName of domain users)

But the script does not work stable. It shows the same displayname for every member without checking if it is a domain object or local object. Or without checking if it is a group or user.

Is there any suggestion to achieve my goal?

$complist = get-content C:\temp\comps2.txt
foreach ($comp in $complist){

$strComputer = $comp #Enter the name of the target computer, localhost is used by default
Write-Host "Computer: $strComputer"
$computer = [ADSI]"WinNT://$strComputer"
$objCount = ($computer.psbase.children | measure-object).count
Write-Host "Q-ty objects for computer '$strComputer' = $objCount"
$Counter = 1
$result = @()
foreach($adsiObj in $computer.psbase.children)
{
switch -regex($adsiObj.psbase.SchemaClassName)
  {
    "group"
    {
      $group = $adsiObj.name
      $LocalGroup = [ADSI]"WinNT://$strComputer/$group,group"
      $Members = @($LocalGroup.psbase.Invoke("Members"))
      $objCount = ($Members | measure-object).count
      Write-Host "Q-ty objects for group '$group' = $objCount"
      $GName = $group.tostring()

      ForEach ($Member In $Members) {


        $Name = $Member.GetType().InvokeMember("Name", "GetProperty", $Null, $Member, $Null)
        $Path = $Member.GetType().InvokeMember("ADsPath", "GetProperty", $Null, $Member, $Null)
        $membername= Get-ADUser $name -Properties displayname | Select-Object -ExpandProperty displayname

        Write-Host " Object = $Path"

                 $isGroup = ($Member.GetType().InvokeMember("Class", "GetProperty", $Null, $Member, $Null) -eq "group")




        If (($Path -like "*/$strComputer/*") -Or ($Path -like "WinNT://NT*")) { $Type = "Local"
        } Else 

        {$Type = "Domain"}





        $result += New-Object PSObject -Property @{
          Computername = $strComputer
          NameMember = $Name
          PathMember = $Path
          TypeMemeber = $Type
          ParentGroup = $GName
          isGroupMemeber = $isGroup
          Depth = $Counter
          Membername = $membername

        }
      }
    }
  } #end switch

} #end foreach

Write-Host "Total objects = " ($result | measure-object).count
$result = $result | select-object Computername, ParentGroup, NameMember, TypeMemeber, PathMember, isGroupMemeber, membername
$result | Export-Csv -append -path ("C:\temp\LocalGroups({0})-{1:yyyyMMddHHmm}.csv" -f
$env:COMPUTERNAME,(Get-Date)) -Delimiter ";" -Encoding "UTF8" -force -NoTypeInformation}
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,459 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,435 questions
0 comments No comments
{count} votes