Get-ADComputer weird output

This topic contains 7 replies, has 4 voices, and was last updated by Profile photo of Matt McNabb Matt McNabb 2 years, 2 months ago.

  • Author
    Posts
  • #19458
    Profile photo of Ondrej Zilinec
    Ondrej Zilinec
    Participant

    I just played with cmd-let Get-ADComputer. I found out that if I run:

    $Computer = Get-ADComputer -SearchBase "DC=domain,DC=local" -Fileter "*"
    

    $Computer is [b]System.Array[/b]. It contains 29 records.

    But if I define filter to filter out only one object:

    $Computer = Get-ADComputer -SearchBase "DC=domain,DC=local" -Fileter 'Name -like "*OneComputerName*"'
    

    $Computer is [b]Microsoft.ActiveDirectory.Management.ADAccount[/b].

    This is really confusing. I know how to fix it, just define type of the object:

    [array]$Computer = Get-ADComputer -SearchBase "DC=domain,DC=local" -Fileter 'Name -like "*OneComputerName*"'
    

    But this not behaviour I would expect 🙂 Is this "fixed" in some version of PowerShell? I'm testing this in 4.0.

    Thank you,

  • #19461
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    This is default behaviour.

    The array is just a container for items of the Microsoft.ActiveDirectory.Management.ADAccount type

    $Computer = Get-ADComputer -SearchBase "DC=domain,DC=local" -Fileter 'Name -like "*OneComputerName*"'
    $Computer.GetType()
    
    $Computer = Get-ADComputer -SearchBase "DC=domain,DC=local" -Fileter "*"
    $Computer[1].GetType()
    

    Return the same results.

    You can still use the result in operations such as ForEach, so you shouldnt need to adjust your code.

  • #19462
    Profile photo of Øyvind Kallstad
    Øyvind Kallstad
    Participant

    As Tim points out, this is not a bug, but the expected behaviour when working with a loosely typed language. It will "guess" the type based on the data it get assigned. You can always strongly type your variables, like you have discovered yourself 🙂

  • #19463
    Profile photo of Ondrej Zilinec
    Ondrej Zilinec
    Participant

    can still use the result in

    That's corret, but when you want to list total items and you want to use .Count property of array it doesn't work on other than array object. I think I will stay with defining type of variables.

  • #19471
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    Don't forget you can use Measure-Object to count objects in the pipeline as well. This way it doesn't matter if it is an array or not, you'll always get the right count:

    Get-ADComputer -SearchBase "DC=domain,DC=local" -Fileter 'Name -like "*OneComputerName*"' | Measure-Object

    • #19481
      Profile photo of Ondrej Zilinec
      Ondrej Zilinec
      Participant

      Maybe this is not a case, but isn't Measure-Object more CPU intensive than variable definition?

  • #19496
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    It might be but whether that matters really depends on the scenario. Is this something that will be done many times in a script, or just once?

You must be logged in to reply to this topic.