I am scripting some code that will help my team automate some AD User mod tasks and ensure that we are doing things consistently. I am prompting for a user's last name (or partial last name) and returning a list of matching users (Index, Name, userID) to ensure we mod the correct account.
For example if I search for Smith it would return:
[0] Smith, John jsmith
[1] Smith, Jane jsmith1
[2] Smithfield, Robert rsmithfield
The tech would then need to enter the corresponding number of the account they want to modify. I put in an input validation check to ensure that a valid index number is chosen (in this case it will not accept a negative int or an int greater than 2).
Unfortunately I do not know how to also ensure that a letter or symbol is not entered. In this case if users enter the number 3 it will return a message of "Invalid Selection" but if they enter the letter d, it accepts it and then errors out.
How do I make it so it will only accept an int?
FYI - after I get this working I will be creating functions and function calls so it would be more modular, this is just initial testing of the concept.
#prompt for last name (or at least portion of last name, then conver to a string with wild card for use in filter
$lname = Read-Host "`nEnter at least the first three chars of users last name (you may enter full last name)"
$search = $lname + "*"
#Empty arrays
$ResultsArray=@("")
$ADUsersList=@("")
#add aduser properties to array, return error and exit if no user found
$ADUsersList += Get-ADUser -filter 'surname -like $search' -Properties * | select name,samaccountname,DistinguishedName
if ($ADUsersList.count -le 1){
Write-host "USER NOT FOUND"
return
}
#populate a 2D array with NAM and with UserID
$ResultsArray = @(($ADUsersList.name),($ADUsersList.samaccountname))
#return list of found users and user ids. Preface with index number to use for selection menu
for($i=0;$i-le $ResultsArray[0].length-1;$i++){
“[{0}] = {1} `t {2}” -f $i,$ResultsArray[0][$i],$ResultsArray[1][$i]
}
#Prompt
[int]$selection = Read-Host "`nEnter number for user you want to modify"
#Input validation - Only allow a valid index #. If valid, write user selected to screen before proceeding
if ($selection -gt $ResultsArray[0].length -or $selection -lt 0){
Write-host "INVALID SELECTION!!!" -ForegroundColor Red
}
else {
Write-Host "`nYou selected user " -nonewline
Write-Host $ResultsArray[0][$selection] -nonewline -ForegroundColor Green
Write-Host " with user ID a of " -nonewline
Write-Host $ResultsArray[1][$selection] -nonewline -ForegroundColor Green
}
$ResultsArrayis a jagged array, not a 2D array. Also, you don't need to initialize your arrays separately; e.g.,$ADUsersList = Get-ADUser ...will do (or, if you want to ensure that the result is an array even in the event that only one user is returned:$ADUsersList = @(Get-ADUser ...))