I'm attempting to compare a string-based variable against a .csv based list to determine if the string exists. What I neglected to take into account is the possibility that the variable may be a sub-string of one of the item lists.
Prior to the piece of code below I've prompted the user to enter a first name and last name and then using that information provided, the following action is performed to generate an 8 character username and then check against the list to see if that username is available or already taken.
$LastName = $objTextBoxLN.text
$LastName = (Get-Culture).textinfo.totitlecase($LastName)
$UN = $objTextBoxLN.text.Substring(0,[System.Math]::Min(7, $objTextBoxLN.text.Length)).ToLower()+$objTextBoxFN.text.Substring(0,1).ToLower()
Write-Host "Searching for Username Conflicts"
Write-Host
IF(select-string -Path "c:\Folder_X\userlist.csv" -pattern $UN)
{
$UN = $objTextBoxLN.text.Substring(0,[System.Math]::Min(6, $objTextBoxLN.text.Length))+$objTextBoxFN.text.Substring(0,2)
Write-Host "Conflict found! Username" $UN.ToLower() "generated using 6 characters from last name plus 2 characters from first name (Option 2)"
Write-Host
ELSE
{
Write-Host "No Conflicts Found. Username" $UN "generated using 7 characters from last name plus 1 character from first name (Option 1)"
Write-Host
}
So if on the Userlist.csv we have a list of usernames:
- smithj
- whitew
- whitewi
etc ...
And the end-user enters "Harry Smit" it will generate a username of "smith" and then the IF statement will find "smith" on the list, inside the current username "smithj" and update the username to "smithhi" but this is not the behavior I was hoping to achieve, I would like it to find that the username "smith" doesn't exist and return the original username.
Appreciate any help anyone can offer, hopefully theres a simple solution.