How to display a range of count values

This topic contains 10 replies, has 3 voices, and was last updated by Profile photo of Vikram Vijayakumar Vikram Vijayakumar 2 years, 1 month ago.

  • Author
    Posts
  • #19584
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    I am having a serious brain fart right now and I know this is quite simple, but I just cannot for the life of me conceptualize this.

    How would I show only values that fall within the range of say something like 10 through 50 in the following example:

    $GetADComputersList = Get-ADObject -Filter 'ObjectCategory -eq "Computer"' -SearchBase "OU=My,DC=test,DC=domain"
    
    $GetADComputersList.Count

    I was thinking about using the range operator, but this did not work as I had intended.

    Thanks for any help I can get

  • #19585
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Please try if below works for you.

    $Result = ($GetADComputersList.Count -in 10..50)
    $Result
    
  • #19587
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    thanks for the reply. No that did not work either. It shows False as the output

  • #19588
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    What is the exact output you've expect from this?

    if ($GetADComputersList.Count -in 10..50)
    {
      $GetADComputersList.Count
    }
    
  • #19589
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    And why are you not using Get-ADComputer?

    $GetADComputersList = Get-ADComputer -SearchBase "OU=My,DC=test,DC=domain"
    if ($GetADComputersList.Count -in 10..50)
    {
      $GetADComputersList.Count
    }
    
  • #19592
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    The output I would like to see is a specified range (number) of computers that exist within OUs. I have most of my code written and it is working great, but I want to be able to choose what values I want to display. For instance, I don't want to see 0 values. Perhaps I only want to see the OUs that have between 10 to 20 computers in them.

    The reason I am not using Get-ADComputer is because I am not using this script to find JUST computer objects. I am using it to also find Group, Organizational Unit, and User objects as well.

    For instance, this code here works perfect, but I simply want to be able to choose a range, or a starting value so I can determine what values I want displayed:

    $Object = "Computer"
    
    $BaseOU = "OU=My,DC=test,DC=domain"
    
    $OUProps = Get-ADObject -Filter 'ObjectCategory -eq "OrganizationalUnit"' -SearchBase $BaseOU -Properties CanonicalName, DistinguishedName, Name 
    
    
    $GetCount = Foreach ($OUDN in $OUProps.DistinguishedName)
    {
        (Get-ADObject -Filter 'ObjectCategory -eq $Object' -SearchBase $OUDN).Count 
    }
     
    
     $GetCount
  • #19593
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Thanks for the better explanation. I hope the following code works for you and produces the expected output.

    $ObjectClass = "Computer"
    $BaseOU = "OU=My,DC=test,DC=domain"
    $OUs = Get-ADObject -Filter 'ObjectCategory -eq "OrganizationalUnit"' -SearchBase $BaseOU -Properties CanonicalName, DistinguishedName, Name
    
    $Results = Foreach ($OU in $OUs)
    {
        $Count = @(Get-ADObject -Filter 'ObjectCategory -eq $ObjectClass' -SearchBase $OU.DistinguishedName).Count
    
        $Result = Select-Object -InputObject $OU -Property CanonicalName, DistinguishedName, Name
        Add-Member -InputObject $Result -MemberType NoteProperty -Name ChildObjectCount -Value $Count
        Add-Member -InputObject $Result -MemberType NoteProperty -Name ChildObjectClass -Value $ObjectClass
    
        Write-Output -InputObject $Result
    }
    
    # Output objects with the ChildObjectCount within the range 10 to 50
    $Results | Where-Object { $_.ChildObjectCount -in 10..50 }
    
    # Output objects with the ChildObjectCount greater than or equal to 10
    $Results | Where-Object { $_.ChildObjectCount -ge 10 }
    
  • #19611
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Yep, that worked GREAT!

    Thanks

  • #19640
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Dan,

    How do I get this to show ONLY "0" values as well?

    I tried to use 0..0, but I am guessing the reason it did not show any values is due to the fact that there is no range between 0 and 0.

    Thanks

  • #19641
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Disregard, I got it

  • #19643
    Profile photo of Vikram Vijayakumar
    Vikram Vijayakumar
    Participant

    $GetADComputersList = Get-ADObject -Filter 'ObjectCategory -eq "Computer"' -SearchBase "OU=My,DC=test,DC=domain"
    $GetADComputersList | Select -First 50

    Guess this is what you are looking for?

You must be logged in to reply to this topic.