Parsing things in "Get-AdUser" filter

Home Forums Scripting PowerShell Parsing things in "Get-AdUser" filter

This topic contains 8 replies, has 4 voices, and was last updated by  JeremyW 1 year, 3 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #167169

    Hi all – long time!

    I am running a script which takes a person’s first and last name from an SAP extract, and reads AD to get their UPN. For most people, this works; however there are a bunch of users whose first name is an issue. For instance “Philip Davies” (names changed to protect the innocent) in SAP is “Phil Davies” in AD. So: I have used the following command and it works:

    Code:
    Get-ADUser -SearchBase “OU=CBC Users,DC=cbc,DC=int” -Filter {GivenName -eq “Phil” -and Surname -eq “Davies”}

    I then realised I can check for the first three characters which will NORMALLY be the same in the contracted name… so I did this which also works:

    Code:
    Get-ADUser -SearchBase “OU=CBC Users,DC=cbc,DC=int” -Filter {GivenName -like “Phi*” -and Surname -eq “Davies”}

    Next step: variables; so I try this and it works:

    Code:
    $fna=”Phil”
    Get-ADUser -SearchBase “OU=CBC Users,DC=cbc,DC=int” -Filter {GivenName -eq $fna -and Surname -eq “Davies”}

    But if I try this:

    Code:
    $fna=”Philip”
    $fna=$fna.Substring(0,3)
    Get-ADUser -SearchBase “OU=CBC Users,DC=cbc,DC=int” -Filter {GivenName -like $fna* -and Surname -eq “Davies”}

    I get no result. It doesn’t matter if I use brackets, double-quotes, single-quotes, anything. As soon as I try to parse a variable AND use a wildcard, it either produces an error message or no result.

    Can anyone please help me with this either by using the “-ldapfilter” method or telling me how to parse AND wildcard?

    Thanks


    Ossian
    Moderator
    #191786

    Just a thought – and untested – can you build up a string by concatenating $fna and “*” and then passing that string to the filter?

    #220894
    Ossian;n512721 wrote:
    Just a thought – and untested – can you build up a string by concatenating $fna and “*” and then passing that string to the filter?

    I forgot to document that in my post; the script runs without error doing that but produces a null result :( (i.e. it considers the * as an asterisk in the string instead of parsing it as a wildcard)


    JeremyW
    Moderator
    #271560

    Try this:
    Get-ADUser -SearchBase “OU=CBC Users,DC=cbc,DC=int” -Filter {GivenName -like $($fna.substring(0,3) + ‘*’) -and Surname -eq “Davies”}[/CODE][CODE]Get-ADUser -SearchBase “OU=CBC Users,DC=cbc,DC=int” -Filter {GivenName -like $($fna.substring(0,3) + ‘*’) -and Surname -eq “Davies”}[/CODE]

    #220896

    Oooh, thanks Jeremy… I’ll try that now.

    #220897

    Get-ADUser : Cannot process argument because the value of argument “path” is not valid. Change the value of the “path” argument and run the operation again.
    At C:SamanageSAPReportMungeManagers.ps1:23 char:18
    + $rec=Get-ADUser -SearchBase “OU=CBC Users,DC=cbc,DC=int” -Filter {Gi …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Get-ADUser], PSArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.Management.Automation.PSArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    *SIGH*

    #220898
    Code:
    $fna=”Philip”
    $fna=”$($fna.Substring(0,3))*”
    Get-ADUser -SearchBase “OU=CBC Users,DC=cbc,DC=int” -Filter {GivenName -like $fna -and Surname -eq “Davies”}

    So – I got the answer from StackOverflow in the end… seems to be a variation on Jeremy’s answer; just sets the variable with Jeremy’s code and then compares in the -Filter as normal.

    Thank you everyone for looking and helping!


    JeremyW
    Moderator
    #271566

    Thanks for posting the solution!


    wullieb1
    Moderator
    #245667

    I know you fixed this already but i achieve the same thing doing this

    Get-ADUser -SearchBase “OU=CBC Users,DC=cbc,DC=int” -Filter {(GivenName -like $fna) -and (Surname -eq “Davies”)} [/CODE][CODE]
    Get-ADUser -SearchBase “OU=CBC Users,DC=cbc,DC=int” -Filter {(GivenName -like $fna) -and (Surname -eq “Davies”)} [/CODE]

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.