Foreach Loop Not Workign

Tagged: , ,

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

  • Author
    Posts
  • #54746
    Profile photo of Yeraldin Mueses
    Yeraldin Mueses
    Participant

    I need Help i am a newbie every time i run this i get 0 for the count any help?

    $OSLIST = "Windows 7", "Windows Server 2012", "Windows Server 2008", "Windows XP"
    $OUList = "Item1", "Item2" # Example Items

    Foreach($OU in $OUList) {

    Foreach($OS in $OSLIST){

    $Computers = @(Get-ADComputer -Properties Name,operatingSystem,lastLogontimeStamp -Filter {(OperatingSystem -like "*$OS*")})| where DistinguishedName -Like "*$OU*"
    $OU
    $OS
    $Computers.Count
    }

    }

  • #54750
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    It's better to filter the OU with the SearchBase. In your example, you are performing 8 queries against Get-ADComputer, one for each OS and one for each OU. Additionally, you are saying Get ALL computers for an OS and then pass them through the pipeline to filter by OU.

    Consider consolidating all of your OS's into a single query. Filter your OU's by using Get-ADOrganizationalUnit and then we tell Get-ADComputer to only get computers from that OU. To keep things neat and readable, I'm also using HereString and parameter Splatting. This isn't tested code, but should get you close to what you are trying to accomplish. This would perform 1 query for each OU, so 2 queries and they are only returning what you want.

    #HereString
    $osFilter = @"
    OperatingSystem -like '*Windows XP*'   
    -or
    OperatingSystem -like '*Windows 7*' 
    -or
    OperatingSystem -like '*Windows Server 2008*' 
    -or
    OperatingSystem -like '*Windows Server 2012*' 
    "@
    
    $computers = foreach($OU in (Get-ADOrganizationalUnit -Filter "Name -like 'User*'")) {
        #Splatting
        $splat = @{
            Properties =  @("Name","operatingSystem","lastLogontimeStamp")
            Filter = $osFilter
            SearchBase = $OU.DistinguishedName
        }
    
        Get-ADComputer @splat
    }
    
    $computers.Count
    
    • This reply was modified 8 months ago by Profile photo of Rob Simmers Rob Simmers.
    • #54766
      Profile photo of Yeraldin Mueses
      Yeraldin Mueses
      Participant

      Hey so i tried what you showed me. Except i changed Get-ADOrganizationalUnit to Get-Adobject.

      But i wanted to get a list count of each OS in Each OU. I only get a count of All The OS in one OU.

      Function Get-OSLIST2{
      [CmdletBinding()]
      #HereString

      $osFilter = @"
      OperatingSystem -like '*Windows XP*'
      -or
      OperatingSystem -like '*Windows 7*'
      -or
      OperatingSystem -like '*Windows Server 2008*'
      -or
      OperatingSystem -like '*Windows Server 2012*'
      "@

      $computers = foreach($OU in (Get-ADObject -Filter "Name -like 'Appleton'")) {
      #Splatting
      $splat = @{
      Properties = @("Name","operatingSystem","lastLogontimeStamp")
      Filter = $osFilter
      SearchBase = $OU.DistinguishedName
      }

      Get-ADComputer @splat
      }
      $OU.Name

      $computers.Count

      }

  • #54767
    Profile photo of Yeraldin Mueses
    Yeraldin Mueses
    Participant
       Function Get-OSLIST2{
    [CmdletBinding()]
    #HereString
    
    
    
    $osFilter = @"
    OperatingSystem -like '*Windows XP*'
    -or
    OperatingSystem -like '*Windows 7*'
    -or
    OperatingSystem -like '*Windows Server 2008*'
    -or
    OperatingSystem -like '*Windows Server 2012*'
    "@
    
    $computers = foreach($OU in (Get-ADObject -Filter "Name -like 'Appleton'")) {
        #Splatting
        $splat = @{
            Properties =  @("Name","operatingSystem","lastLogontimeStamp")
            Filter = $osFilter
            SearchBase = $OU.DistinguishedName
        }
    
        Get-ADComputer @splat
    }
    $OU.Name
    
    $computers.Count
    
    
    }
    
    
    
     
  • #54769
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You can get a count of each OS using Group-Object:

    function Get-OSList{
        [CmdletBinding()]
        param()
    
        begin {
    #Do not indent the here string
    $osFilter = @"
    OperatingSystem -like '*Windows XP*'
    -or
    OperatingSystem -like '*Windows 7*'
    -or
    OperatingSystem -like '*Windows Server 2008*'
    -or
    OperatingSystem -like '*Windows Server 2012*'
    "@
        }
        process {
            $computers = foreach($OU in (Get-ADObject -Filter "Name -like 'Appleton'")) {
                Write-Verbose ("Performing query for OU: {0}" -f $OU.Name)
                #Splatting
                $splat = @{
                    Properties =  @("Name","operatingSystem","lastLogontimeStamp")
                    Filter = $osFilter
                    SearchBase = $OU.DistinguishedName
                }
    
                Get-ADComputer @splat
            }
        }
        end {
            $computers | Select *
        }
    
    }
    
    $osList = Get-OSList
    $osList | Group-Object -Property OperatingSystem | Sort-Object Count -Descending
    

You must be logged in to reply to this topic.