locked
Create a CSV file with multiples columns RRS feed

  • Question

  • Hi, 

    I need to export group membership for all machines that I have in my Active Directory. The end goal is to have an CSV file with all my machines as follow, a machine per row and all the groups per columns  

    

    To achieve this I developed the following script. 

    Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host
    
    $list = Get-ADComputer -Filter * -Properties * | select name
    $count = 0 
    foreach($name in $list) {
     $mof = $null
     $name.name
     if (Get-ADComputer -Identity $name.name) {
      $count++
      $report = New-Object psobject 
      $report | Add-Member -MemberType NoteProperty -name Name -Value $name.name
      $data = Get-ADComputer -Identity $name.name
      $guid = ($data.ObjectGUID).Guid
      $mof = Get-ADPrincipalGroupMembership -Identity $guid | Select-Object @{Name = 'GroupName'; Expression = {$_.SamAccountName}}
      ($mof.GroupName).count
      $mof.GroupName
      ""
      if (($mof.GroupName).count -gt 1) {
       $i = 1   
       foreach ($line in $mof.GroupName) {
        $report | Add-Member -MemberType NoteProperty -name ('MemberOf'+$i) -Value $line -Force; $i++
       }
      }
      else {
       $report | Add-Member -MemberType NoteProperty -name 'MemberOf1' -Value $mof.GroupName
       $report | Add-Member -MemberType NoteProperty -name 'MemberOf2' -Value $null
      }
     $report | export-csv "C:\testx.csv" -NoTypeInformation -Append -Force -Confirm:$false -NoClobber
     } 
    }
    
    

    The issue i have here is the fact that i am not able to create more than an CSV file with more than 2 columns. 

    If anyone available for an advise, much appreciated. 

    Thank you, 

    Aurelian


    • Edited by CalimanA Wednesday, December 4, 2019 9:15 AM
    Wednesday, December 4, 2019 7:39 AM

Answers

  • Thank you.

    The following will do what you are trying to do:

    Get-ADComputer -Filter * |
        ForEach-Object{
        
            $hash = [ordered]@{
                ComputerName = $_.Name
            }
            
            $groups = @(Get-ADPrincipalGroupMembership $_.SamAccountName)
            for($i = 0;$i -lt $($groups.Count);$i++){
                $hash.Add("Group$i",$groups[$i].Name)
            }
            [pscustomobject]$hash
        }



    \_(ツ)_/


    • Edited by jrv Wednesday, December 4, 2019 9:21 AM
    • Marked as answer by CalimanA Wednesday, December 4, 2019 12:30 PM
    Wednesday, December 4, 2019 9:18 AM

All replies

  • That cannot be done with a CSV except by setting a specific number of groups per machine.

    First you need to post your code correctly.

    Once you have fixed you original post I will show you the only way this can be done.


    \_(ツ)_/

    Wednesday, December 4, 2019 7:47 AM
  • Thank you for your answer and advice JRV. Is there any other way to accomplish this?
    Wednesday, December 4, 2019 9:17 AM
  • Thank you.

    The following will do what you are trying to do:

    Get-ADComputer -Filter * |
        ForEach-Object{
        
            $hash = [ordered]@{
                ComputerName = $_.Name
            }
            
            $groups = @(Get-ADPrincipalGroupMembership $_.SamAccountName)
            for($i = 0;$i -lt $($groups.Count);$i++){
                $hash.Add("Group$i",$groups[$i].Name)
            }
            [pscustomobject]$hash
        }



    \_(ツ)_/


    • Edited by jrv Wednesday, December 4, 2019 9:21 AM
    • Marked as answer by CalimanA Wednesday, December 4, 2019 12:30 PM
    Wednesday, December 4, 2019 9:18 AM