I would like to find Active Directory User Accounts so that when I input something like
Joe Jones,
there will be a match on User Accounts where the SamAccountName looks something like this:
AA.Joe.Jones
AA.joe.ones
aa.Joe.Jones
xx.Joe.Jones
XX.Joe.Jones
So from the Gui Input, Joe Jones,
$NewFirst = joe
$NewLast = jones
And
SamAccountName (of the AD Record) = AA.Joe.Jones
this match works:
$GoodSam = Get-ADUser -Filter "SamAccountName -like '*$NewLast*' -and SamAccountName -like '*$NewFirst*'" -prop $Properties |
Where-Object { $_.SamAccountName -match "^[A-Za-z]{2}(\.[A-Za-z]*){0,1}\.$NewFirst\.$NewLast$" } |
Select-Object $Properties
But it takes several seconds to run.
I want something to execute like lightning.
So I am trying this:
$GoodSam = Get-ADUser -Filter "SamAccountName -like '[A-Za-z][A-Za-z].*$NewFirst*.$NewLast' " -Properties $properties | Select-Object $properties
But there is no match found.
And since I know the pattern works by testing this:
# Define the NewFirst and NewLast values
$NewFirst = "Joe"
$NewLast = "Jones"
# Example SamAccountNames to test
$SamAccountNames = @(
"io.joe.jones",
"ab.joe.jones",
"xy.joe.jones",
"joe.jones",
"jones.joe",
"jjones",
"joe.smith",
"joe.jones"
)
# Test each SamAccountName
foreach ($SamAccountName in $SamAccountNames) {
if ($SamAccountName -like "[A-Za-z][A-Za-z].*$NewFirst*.$NewLast") {
Write-Host "Match found for SamAccountName: $SamAccountName"
} else {
Write-Host "No match for SamAccountName: $SamAccountName"
}
}
I do not know why this does not find a match:
$GoodSam = Get-ADUser -Filter "SamAccountName -like '[A-Za-z][A-Za-z].*$NewFirst*.$NewLast' " -Properties $properties | Select-Object $properties
Thank you in advance for your help on this.