none
PowerShell Script to fetch local admin group membership from multiple servers RRS feed

  • General discussion

  • #Provide the lists of server input and script gives output csv listing members of local admin group

    #Its working script however please first try on test machine and then execute on multiple machines

    #below is script

    #######################################################################

    $servers = Get-Content C:\temp\servers.txt #Enter the name of the target computer, localhost is used by default
    $Counter = 1
    $result = @()
    ForEach($server in $servers){
    Write-Host "Computer: $server"
    $computer = [ADSI]"WinNT://$server"
    $objCount = ($computer.psbase.children | measure-object).count
    Write-Host "Q-ty objects for computer '$server' = $objCount"

    foreach($adsiObj in $computer.psbase.children)
    {
      switch -regex($adsiObj.psbase.SchemaClassName)
        {
          "group"
          {
            $group = $adsiObj.name
            $LocalGroup = [ADSI]"WinNT://$server/$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)
              Write-Host " Object = $Path"

                       $isGroup = ($Member.GetType().InvokeMember("Class", "GetProperty", $Null, $Member, $Null) -eq "group")
              If (($Path -like "*/$server/*") -Or ($Path -like "WinNT://NT*")) { $Type = "Local"
              } Else {$Type = "Domain"}
              $result += New-Object PSObject -Property @{
                Computername = $server
                Members = $Name
                ParentGroup = $GName
                PathMember = $Path
                TypeMemeber = $Type
               
               
              }
            }
          }
        } #end switch
    } #end foreach
    }
    Write-Host "Total objects = " ($result | measure-object).count
    $result = $result | select-object Computername, ParentGroup, Members, TypeMemeber, PathMember
    $result | Export-Csv -path C:\Temp\localgroups.csv -NoTypeInformation

    Tuesday, June 30, 2020 11:10 AM

All replies

  • Please stop posting random scripts.  This is not a script sharing forum.  If you do not have a question then post your scripts in a scripts sharing forum.

    Most of the scripts you are posting are wrong and do not work as you claim.


    \_(ツ)_/

    Tuesday, June 30, 2020 2:20 PM