Export-csv not getting my multi-domain variable

Welcome Forums General PowerShell Q&A Export-csv not getting my multi-domain variable

This topic contains 11 replies, has 3 voices, and was last updated by

 
Participant
1 year, 11 months ago.

  • Author
    Posts
  • #60025

    Participant
    Points: 58
    Rank: Member

    I see successful output for all domains with this query:

    ForEach ($domain in ((Get-ADForest).domains)) { Get-ADGroupMember "Domain Admins" -Server $domain -Credential $creds }

    ...but when I try to pipe it to export-csv, I only get the last domain in the output:

    ForEach ($domain in ((Get-ADForest).domains)) {Get-ADGroupMember "Domain Admins" -Server $domain -Credential $creds | Export-Csv C:\temp\compliance\DomainAdmins.csv -NoTypeInformation }

    What am I not understanding about my pipeline and how to correct to capture all to a csv?

    thanks

  • #60028

    Participant
    Points: 21
    Rank: Member

    export-csv goes outside the loop.

  • #60030

    Participant
    Points: 21
    Rank: Member

    Simplified.

    $domains = (Get-ADForest).domains)

    $domainadmins = ForEach ($domain in $domains) {Get-ADGroupMember "Domain Admins" -Server $domain -Credential $creds }

    $domainadmins | Export-Csv C:\temp\compliance\DomainAdmins_gap.csv -NoTypeInformation

    • #60049

      Participant
      Points: 58
      Rank: Member

      Yes this works thank you....looking into a one-liner with -Append per below

  • #60031

    Participant
    Points: 4
    Rank: Member

    Each time your loop runs, a new CSV is created. You can demonstrate this with a simple bit of code which changes the name of the CSV file each time the loop runs.

    $domains = 'domain1','domain2'
    
    foreach ($domain in $domains) {
        
        $domain | Export-Csv "E:\__temp\csv-$domain.csv"
    
    }
    

    In your case, because the name isn't changed, Export-CSV overwrites file each time the loop runs.

    Version 3 added the -append parameter to Export-CSV which will add the data to the file rather than overwriting it.

    • #60048

      Participant
      Points: 58
      Rank: Member

      -Append sounds intriguing but when I try this:

      ForEach ($domain in ((Get-ADForest).domains)) {Get-ADGroupMember "Domain Admins" -Server $domain -Credential $creds | Export-Csv C:\temp\compliance\DomainAdmins.csv -NoTypeInformation -Append }

      I get error:

      Export-Csv : Cannot append CSV content to the following file: C:\temp\compliance\DomainAdmins.csv. The appended object does not have a property that corresponds to the following column: PSShowComputerName 
    • #60064

      Participant
      Points: 4
      Rank: Member

      You can use the -Force parameter (doesn't the full error message say that?) to try and export everything regardless of mismatches. For your scenario, where you're exporting the same object type each time that should be fine.

    • #60145

      Participant
      Points: 58
      Rank: Member

      Thank you for the demo script Matt..I see that now.

  • #60055

    Participant
    Points: 21
    Rank: Member

    Even so.

    (Get-ADForest).domains) | %{Get-ADGroupMember "Domain Admins" -Server $_ -Credential $creds }| Export-Csv C:\temp\compliance\DomainAdmins_gap.csv -NoTypeInformation

    • #60066

      Participant
      Points: 58
      Rank: Member

      have to lose the domains) < ———- yeah?

    • #60079

      Participant
      Points: 21
      Rank: Member

      Yep, typo in mashing your code together. remove the ) after domains. Understand the pipeline before starting with code golf.

      Untitledimg

    • #60133

      Participant
      Points: 58
      Rank: Member

      Thanks Dan

The topic ‘Export-csv not getting my multi-domain variable’ is closed to new replies.