0

I am working on a PowerShell script that will output a list od system admins to a CSV file using the Export-Csv command. The portion of the script that gets the data is:

Foreach ($Computer in $Computers){
  $Online = Test-Connection -ComputerName $Computer -Quiet
  if ($Online -eq "True"){
    $GroupName = Get-WmiObject win32_group -ComputerName $Computer | ? {$_.SID -eq 'S-1-5-32-544'} | Select-Object name -ExpandProperty name
    $LocalGroup =[ADSI]"WinNT://$Computer/$GroupName"
    $GroupMembers = @($LocalGroup.psbase.Invoke("Members"))
    $Members = $GroupMembers | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
    foreach ($Member in $Members){
      $obj = New-Object System.Object
      $obj | Add-Member -MemberType NoteProperty -Name "Computer" -Value $Computer
      $obj | Add-Member -MemberType NoteProperty -Name "AdminGroupMembers" -Value $Member
      $obj
    }
  }
}

}

Get-Admins | Export-Csv -NoTypeInformation c:\scripts\adm.csv -Encoding UTF8

The current output is formatted looks like this:

"Computer1", "Admin1"
"Computer1", "Admin2"
"Computer1", "Admin3"
"Computer1", "Admin4"
"Computer2", "Admin1"
"Computer2", "Admin2"
"Computer3", "Admin1"

I am trying to get the output to look like this:

"Computer1", "Admin1" , "Admin2" , "Admin3" , "Admin4"
"Computer2", "Admin1" , "Admin2"  
"Computer3", "Admin1" , "Admin2" , "Admin3"

Any Ideas?

1 Answer 1

1

Your output format is not CSV, so Export-Csv is not a suitable tool for you. Try this instead:

Get-Admins | group { $_.Computer } | % {
  '{0},{1}' -f @($_.Group.Computer)[0], ($_.Group.AdminGroupMembers -join ',')
} | Out-File 'output.csv'

For PowerShell v2 you'll need to manually expand the group properties:

Get-Admins | group { $_.Computer } | % {
  $computer = @($_.Group | select -Expand Computer)[0]
  $admins   = ($_.Group | select -Expand AdminGroupMembers) -join ','
  '{0},{1}' -f $computer, $admins
} | Out-File 'output.csv'
Sign up to request clarification or add additional context in comments.

6 Comments

I appreciate your suggestion however The file does output to a csv file, just not in the format I would like it. You suggestion above runs but provides no CSV output which is a requirement.
@user2654059 Now it does.
the output file is 9 lines ling with a comma in each line, no other data. #Export data out and give feedback #--------------------------------------------------------------------------------------------------------- Get-Admins | group { $_.Computer } | % { '{0},{1}' -f @($_.Group.Computer)[0], ($_.Group.AdminGroupMembers -join ',') } | Out-File 'c:\scripts\adm.csv' Write-Host "`n Script completed for online systems"
It seems you're still using PowerShell v2. See updated answer.
Updated the powershell to ver 3. Pre last edit change produced a 9 line list with the computer name and a comma but no admin names. Incorporating the latest changes I get this error 43 times: At C:\Scripts\test.ps1:44 char:27 + $admins = ($_.Group | select -Expand AdminGroupMembers) -join ',' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (System.Object:PSObject) [Select-Object], PSArgumentException + FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.