Foreach Loop Not Workign

Welcome Forums General PowerShell Q&A Foreach Loop Not Workign

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

 
Participant
2 years, 2 months ago.

  • Author
    Posts
  • #54746

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 324
    Helping Hand
    Rank: Contributor

    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
    
    • #54766

      Participant
      Points: 0
      Rank: Member

      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

    Participant
    Points: 0
    Rank: Member
       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

    Participant
    Points: 324
    Helping Hand
    Rank: Contributor

    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
    
    • #54825

      Participant
      Points: 0
      Rank: Member

      That worked For me Huge Thanks.

The topic ‘Foreach Loop Not Workign’ is closed to new replies.