Asked by:
Getting all local groups and members

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) $Name } } # 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 $i=0 foreach($adsiObj in $computer.psbase.children) { switch -regex($adsiObj.psbase.SchemaClassName) { "group" { $group = $adsiObj.name EnumLocalGroup $group } } #end switch $i++ } } | 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.
Thanks
James
- 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
-
Remove "Out-String"
\_(ツ)_/
Tuesday, September 5, 2017 4:40 PM -
Hi Thanks for the response however when I remove the Out-String I get the following in the CSV:
Length 42 21 8 33 13 31 13 14 26 23 38 30 28 24 13 5 Thanks
James
Friday, September 8, 2017 9:10 AM -
Please post here for assistance with your Gallery script: https://gallery.technet.microsoft.com/scriptcenter/List-all-local-groups-and-f64e63d1/view/Discussions#content
\_(ツ)_/
Friday, September 8, 2017 9:26 AM -
The code you have chosen from the Gallery cannot be used to output to a CSV. There are other, better, scripts in the Gallery which an output to a Csv.
\_(ツ)_/
Friday, September 8, 2017 9:37 AM -
Hi Jrv,
Sorry I must be really dumb here but I cant seem to find any which are doing what I am after. Could you point me in the right direction?
Thanks
James
Wednesday, September 13, 2017 7:18 PM