Announcement

Collapse
No announcement yet.

Parsing things in "Get-AdUser" filter

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Parsing things in "Get-AdUser" filter

    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


    Tom
    For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

    Anything you say will be misquoted and used against you

  • #2
    Just a thought - and untested - can you build up a string by concatenating $fna and "*" and then passing that string to the filter?
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Originally posted by Ossian View Post
      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)


      Tom
      For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

      Anything you say will be misquoted and used against you

      Comment


      • #4
        Try this:
        Code:
        Get-ADUser -SearchBase "OU=CBC Users,DC=cbc,DC=int" -Filter {GivenName -like $($fna.substring(0,3) + '*') -and Surname -eq "Davies"}
        Regards,
        Jeremy

        Network Consultant/Engineer
        Baltimore - Washington area and beyond
        www.gma-cpa.com

        Comment


        • #5
          Oooh, thanks Jeremy... I'll try that now.


          Tom
          For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

          Anything you say will be misquoted and used against you

          Comment


          • #6
            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:\Samanage\SAPReport\MungeManagers.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.Man agement.Commands.GetADUser



            *SIGH*


            Tom
            For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

            Anything you say will be misquoted and used against you

            Comment


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


              Tom
              For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

              Anything you say will be misquoted and used against you

              Comment


              • #8
                Thanks for posting the solution!
                Regards,
                Jeremy

                Network Consultant/Engineer
                Baltimore - Washington area and beyond
                www.gma-cpa.com

                Comment


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

                  Code:
                   
                   Get-ADUser -SearchBase "OU=CBC Users,DC=cbc,DC=int" -Filter {(GivenName -like $fna) -and (Surname -eq "Davies")}

                  Comment

                  Working...
                  X