Getting VM or physical from foreach

Welcome Forums General PowerShell Q&A Getting VM or physical from foreach

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

 
Participant
1 year, 9 months ago.

  • Author
    Posts
  • #70310

    Participant
    Points: 71
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

      Participant
      Points: 71
      Rank: Member

      Thank you Richard

    • #70432

      Participant
      Points: 71
      Rank: Member

      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

    Moderator
    Points: 24
    Team Member
    Rank: Member

    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

      Participant
      Points: 71
      Rank: Member

      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.

The topic ‘Getting VM or physical from foreach’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort