Export-csv not getting my multi-domain variable

Tagged: 

This topic contains 11 replies, has 3 voices, and was last updated by Profile photo of Jeff Taylor Jeff Taylor 5 months, 1 week ago.

  • Author
    Posts
  • #60025
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    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
    Profile photo of Dan Potter
    Dan Potter
    Participant

    export-csv goes outside the loop.

  • #60030
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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
      Profile photo of Jeff Taylor
      Jeff Taylor
      Participant

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

  • #60031
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    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
      Profile photo of Jeff Taylor
      Jeff Taylor
      Participant

      -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
      Profile photo of Matt Bloomfield
      Matt Bloomfield
      Participant

      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
      Profile photo of Jeff Taylor
      Jeff Taylor
      Participant

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

  • #60055
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Even so.

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

    • #60066
      Profile photo of Jeff Taylor
      Jeff Taylor
      Participant

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

    • #60079
      Profile photo of Dan Potter
      Dan Potter
      Participant

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

      Untitledimg

    • #60133
      Profile photo of Jeff Taylor
      Jeff Taylor
      Participant

      Thanks Dan

You must be logged in to reply to this topic.