Getting all local groups and members RRS feed

  • Question

  • Hello,

    I have the following code from https://gallery.technet.microsoft.com/scriptcenter/List-all-local-groups-and-f64e63d1

    I have modified it slightly for my needs but I cant figure out why I cant get it to export out apart from just a length!

    Trap {"Error: $_"; Break;} 
    Function EnumLocalGroup($LocalGroup) 
        $Group = [ADSI]"WinNT://$strComputer/$LocalGroup,group" 
        "Group: $LocalGroup" 
        # Invoke the Members method and convert to an array of member objects. 
        $Members= @($Group.psbase.Invoke("Members")) 
        ForEach ($Member In $Members) 
            $Name = $Member.GetType().InvokeMember("Name", 'GetProperty', $Null, $Member, $Null) 
    # Specify the computers. 
    Import-Csv "C:\Temp_Storage\List_of_Servers_in_AD.csv" | 
    ForEach-Object {
    $strComputer = $_.Server
    Write-Host "Computer: $strComputer" 
    $computer = [adsi]"WinNT://$strComputer" 
    $objCount = ($computer.psbase.children | measure-object).count 
    foreach($adsiObj in $computer.psbase.children) 
      switch -regex($adsiObj.psbase.SchemaClassName) 
           { $group = $adsiObj.name 
           EnumLocalGroup $group } 
       } #end switch 
    	} | Out-String | Export-Csv "C:\Temp_Storage\LocalGroups.csv" -NoTypeInformation

    If I run it then it works fine and prints out to the screen however if I leave in the export-csv I just get a length in the CSV file.

    Any help on where I am going wrong would be appreciated.



    • Moved by Bill_Stewart Tuesday, November 7, 2017 9:41 PM This is not "fix/debug/rewrite gallery script for me" forum
    Tuesday, September 5, 2017 12:27 PM

All replies