Getting VM or physical from foreach

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

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

    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?

  • #70336
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    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
      }
    }
    
    • #70420
      Profile photo of Jeff Taylor
      Jeff Taylor
      Participant

      Thank you Richard

    • #70432
      Profile photo of Jeff Taylor
      Jeff Taylor
      Participant

      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?

  • #70345
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    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
    }
    
    • #70421
      Profile photo of Jeff Taylor
      Jeff Taylor
      Participant

      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.

You must be logged in to reply to this topic.