powershell script does not query ad user and no errors

Biju Thankappan 101 Reputation points
2020-11-21T17:18:04.753+00:00

Hi, I have an input folder that has files names as below: ![41651-image.png][1] [1]: /api/attachments/41651-image.png?platform=QnA I'm using the following script to get the 5 digit number from the filenames that is the logon name/samaccountname in our ad. Then query ad to check if the user exists or not. Import-Module ActiveDirectory $InputFolder = "Z:\Scripts\setuserthumbnailfoto\Input\" $Pictures = Get-ChildItem $InputFolder -Filter .jpg foreach ($Picture in $Pictures) { $a = $Picture.Name -replace '[.]','' $a = $a -replace '\D+([0-9]).','$1' $a = $a -match '0(?<Number>([1-9][0-9]*))' $a = $Matches.Number $a = $a -match '\d{5}' $a = $Matches.Values Try { $ADUser = Get-ADUser -Identity $a } Catch {} if ($ADUser) { Write-Host $a "Exists" } else { Write-Host $a "Doesn't Exists!!!" } } Unfortunately, there are no errors to show...only that the output is coming as below: 12345 Doesn't Exists!!! 12345 Doesn't Exists!!! 12345 Doesn't Exists!!! 12345 Doesn't Exists!!! 67890 Doesn't Exists!!! 67891 Doesn't Exists!!! 67890 Doesn't Exists!!! 67890 Doesn't Exists!!! 54321 Doesn't Exists!!! 54321 Doesn't Exists!!! 54321 Doesn't Exists!!! 54321 Doesn't Exists!!! I know all these users exists in AD, except for 67891 What am I doing wrong? Please help Regards, BT

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,244 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
{count} votes

Accepted answer
  1. Rich Matheisen 45,906 Reputation points
    2020-11-22T16:08:05.04+00:00

    I'm glad to see that it works. I don't have an AD so I wasn't able to test the code.

    If your intention is to just extract the first consecutive sequence of five digits that appear in a string that doesn't begin with a zero then you can eliminate almost all of your code with the correct regex. It would have been a lot easier to test if you'd posted a text file containing the data instead of an image!

    $x = '00000000.00012345.bb.t',
    '0000.12345.bb.t',
    '000.12345.bb.t',
    '00.12345.bb.t',
    '00054321.00000000.t.b',
    '00054321.00000000.t1.b1',
    '00054321.00012345.t1.b1',
    '54321.00012345.t1.b1',
    '00000000.00067890.d.b',
    '00000000.067890.d.b',
    '00000000.67890.d.b',
    '00000000.00067891.d.bb',
    '00001234.1234.x.y',
    '000A1234.A1234.x.y',
    '000A1234.012345.x.y'
    
    $x |
        ForEach-Object{
            if ($_ -match "([1-9]\d\d\d\d)"){
                "{0} found in {1}" -f $matches[0], $_
            }
            else{
                "Pattern not found in {0}" -f $_
            }
        }
    

    Here are the results of running that test. Note that there are a couple of strings that won't match you criteria:

    12345 found in 00000000.00012345.bb.t
    12345 found in 0000.12345.bb.t
    12345 found in 000.12345.bb.t
    12345 found in 00.12345.bb.t
    54321 found in 00054321.00000000.t.b
    54321 found in 00054321.00000000.t1.b1
    54321 found in 00054321.00012345.t1.b1
    54321 found in 54321.00012345.t1.b1
    67890 found in 00000000.00067890.d.b
    67890 found in 00000000.067890.d.b
    67890 found in 00000000.67890.d.b
    67891 found in 00000000.00067891.d.bb
    Pattern not found in 00001234.1234.x.y
    Pattern not found in 000A1234.A1234.x.y
    12345 found in 000A1234.012345.x.y
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 45,906 Reputation points
    2020-11-21T19:59:50.23+00:00

    I've added comments to your code and made the necessary corrections, but I haven't changed the way you arrive at your samAccountName:

    Import-Module ActiveDirectory
    $InputFolder = "Z:\Scripts\setuserthumbnailfoto\Input\"
    $Pictures = Get-ChildItem $InputFolder -Filter .jpg
    foreach ($Picture in $Pictures) {
        $a = $Picture.Name -replace '[.]', ''
        $a = $a -replace '\D+([0-9]).', '$1'
        $a = $a -match '0(?<Number>([1-9][0-9]+))'  # Should use +, not * because * can match ZERO number of characters
        $a = $Matches.Number
        $a = $a -match '^\d{5}$'        # if your intention is to validate that there are exactly 5 digits, use anchors in the regex '^\d{5}$
        $a = $Matches.Values            # $a is now a ValuesCollection, not a string
                                        # when Get-ADUser tries to use that as a string
                                        # it gets the class name (System.Collections.Hashtable+ValueCollection)
                                        # and not the samAccountName 12345 which is in $a[0]
                                        # You should use $a[0] as the -Identity value in Get-ADUser.
        Try {
            $ADUser = Get-ADUser -Identity $a[0] -ErrorAction Stop     # without STOP a non-terminating error will never run Catch block
            Write-Host $a[0] "Exists"   # use $a[0] here, too
        }
        Catch { 
            $ADUser = $null                         # If you don't assign a value $ADUser may retain it's previous value
            Write-Host $a[0] "Doesn't Exists!!!"    # use $a[0] here, too
        }
    
    1 person found this answer helpful.