Export-CSV forEach psitem?

This topic contains 6 replies, has 4 voices, and was last updated by  Max Kozlov 4 months, 3 weeks ago.

  • Author
    Posts
  • #70703

    Jeff Taylor
    Participant

    How would I export this one-liner?

    foreach ($domain in (Get-ADForest).domains) { 
      Get-ADDomainController -filter * -server $domain | 
      sort hostname  |
      foreach { 
        Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $psitem.Hostname |
        select @{name="DomainController";Expression={$_.PSComputerName}}, Manufacturer, Model,@{Name="TotalPhysicalMemory(GB)";Expression={ "{0:N0}" -f  ($_.TotalPhysicalMemory / 1Gb) }}
        }
    }
  • #70739

    Don Jones
    Keymaster

    You want the morally correct way or the quick and dirty way?

  • #70955

    random commandline
    Participant
    $domain = (Get-ADForest).domains
    $host = foreach ($d in $domain){(Get-ADDomainController -Server $d).hostname}
    
    Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $host |
        Select-Object @{name="DomainController";Expression={$_.PSComputerName}},
        Manufacturer,Model,@{Name="TotalPhysicalMemory(GB)";
        Expression={"{0:N0}" -f  ($_.TotalPhysicalMemory / 1Gb)}} |
        Export-Csv \\path\to\export.csv -NoTypeInformation
    
    • #71057

      Jeff Taylor
      Participant

      Thanks random

    • #71470

      Jeff Taylor
      Participant

      RCL,

      Apparently, this script only outputs (1) DC per domain. I'd like output for all DC's in each domain.

  • #71474

    Max Kozlov
    Participant
    [...]
    $hosts = foreach ($d in $domain){(Get-ADDomainController -Server $d -Filter *).hostname}
    [...]
    

You must be logged in to reply to this topic.