Get-ADComputer Script Best Practice

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of David DeHerrera David DeHerrera 3 years, 3 months ago.

  • Author
    Posts
  • #13409
    Profile photo of David DeHerrera
    David DeHerrera
    Participant

    I am having an issue on adding a variable to my Get-ADComputer script.
    This one is failing:
    $PartialName = "3v1*"
    Get-ADComputer -Filter 'Name -like "$PartialName"' | select -ExpandProperty Name

    Here are the two ways I have been able to get it to work:
    $PartialName = "3v1*"
    Get-ADComputer -Filter "*" | ? {$_.Name -like "$PartialName"} | select -ExpandProperty Name

    Or
    $PartialName = "3v1*"
    Get-ADComputer -Filter "Name -like ""$PartialName""" | select -ExpandProperty Name

    What is the best way of doing this? Less resources, more efficient...etc?

  • #13410
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Well, there's mainly "the way that works."

    In your first example, you're sending the literal $PartialName to Active Directory. It has no idea what to do with it. In your last example, by surrounding the filter in double quotes, you're allowing PowerShell to replace $PartialName with its contents, before sending the filter to AD. So that works.

    Your second example is querying every single computer from AD and then filtering them locally. I suspect that will not perform as well as the second one.

  • #13413
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    Did you try

    $PartialName = "3v1*"

    Get-ADComputer -Filter "Name -like '$PartialName'" | select -ExpandProperty Name

    Alternatively use an LDAP filter – I think this should work & can't test it at the moment

    Get-ADComputer -LDAPFilter "(cn=$PartialName)"

  • #13453
    Profile photo of David DeHerrera
    David DeHerrera
    Participant

    @Don Jones – That makes sense, I am just confused as why the variable wasn't working.

    @Richard Siddaway – That worked like a champ. So I watched Jason Helmick and Jeffrey Snover, on the MVA PowerShell and he explained that you can put a variable in between "'s ("Here is $Variable") and it would stay a variable, and anything between the ' would be plain text. So why is this working?

    Thank you both for helping me out with this.

  • #13476
    Profile photo of David DeHerrera
    David DeHerrera
    Participant

    @dave Wyatt – Thank you sir.

  • #13454
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    “Name -like '$PartialName'” is a double-quoted string; any variables inside of it get expanded. The single quotes inside that string are just text; they don't receive any special treatment from PowerShell, and don't affect the behavior of variable expansion.

    The reverse applies as well: 'Name -like "$PartialName"' is a single-quoted string. It doesn't matter that the string happens to contain double quotes; the variable is still not going to be expanded.

You must be logged in to reply to this topic.