Getting VM or physical from foreach

I use this to retrieve all my DC’s:

foreach ($domain in (get-adforest).domains) { get-addomaincontroller -filter * -server $domain `
| sort hostname | select -Property hostname }

…but how would I then pipe that into a foreach to glean their being physical or VM, presumably using Get-CIMInstance?

first off you don’t need the back tick.

foreach ($domain in (get-adforest).domains) { get-addomaincontroller -filter * -server $domain `
| sort hostname  | select -Property hostname }

The pipe symbol works as a line continuation marker if its the end of a line - so this is better

foreach ($domain in (get-adforest).domains) { get-addomaincontroller -filter * -server $domain | 
sort hostname  | select -Property hostname }

To answer your actual question - this should do what you want

foreach ($domain in (Get-ADForest).domains) { 
  Get-ADDomainController -filter * -server $domain | 
  sort hostname  |
  foreach { 
    Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $psitem.Hostname |
    select PSComputerName, Manufacturer, Model
  }
}

Thanks, Richard.

You could do the following as well. Either using the foreach statement or the Foreach-Object cmdlet.

Example - foreach statement:

foreach ($domain in (Get-ADForest).Domains) { 
    $hostNameList = Get-ADDomainController -Filter * -Server $domain |
        Sort-Object -Property HostName | 
            Select-Object -ExpandProperty HostName

    Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $hostNameList
}

Example - ForEach-Object cmdlet:

(Get-ADForest).Domains | ForEach-Object {
    $hostNameList = Get-ADDomainController -Filter * -Server $PSItem | 
        Sort-Object -Property HostName |    
            Select-Object -ExpandProperty HostName

    Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $hostNameList
}

Thank you Richard

Daniel, thank you…this is useful information for an upcoming refresh effort.

If I wanted to pipe the results (cross domains btw) into a csv, how do i correct this attempt?

foreach ($domain in (Get-ADForest).Domains) { 
    $hostNameList = Get-ADDomainController -Filter * -Server $domain |
        Sort-Object -Property HostName | 
            Select-Object -ExpandProperty HostName

    Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $hostNameList | Export-Csv .\DC_Inventory.csv -NoTypeInformation
}

I tried outside the last curly brace but that’s an empty pipe.

Richard,

I wanted to convert memory to a simple integer:

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

…but my formatting is bit truncated.

this is as close as I’ve come:

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

How can I see all columns?