Guest User

Untitled

a guest
Oct 23rd, 2018
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $unameFirst = "Jon"
  2. $unameLast = "Jones"
  3. $unameMiddle = ""
  4.  
  5. $unamefirst = $unameFirst.ToLower()
  6. if ($unameMiddle) {
  7.     $unameMiddle = $unameMiddle.ToLower()
  8.     $unameMiddle = $unameMiddle.split(".")[0]
  9.     }
  10. $unameLast = $unameLast.ToLower()
  11.  
  12. if ($unameMiddle) {
  13. $possibleNames = 1..$unameFirst.Length | ForEach-Object {
  14.     -join ($unameFirst.Substring(0,$_),$unameMiddle,$unameLast)
  15.     }
  16. }
  17. else {
  18.     $possibleNames = 1..$unameFirst.Length | ForEach-Object {
  19.         -join ($unameFirst.Substring(0,$_),$unameLast)
  20.         }
  21. }
  22.  
  23. #iterate through the possible names until we find one that's not in use
  24. foreach ($possibleName in $possibleNames) {
  25.     Write-Host "Trying username $possibleName"
  26.     try {
  27.     $usernameToCheck = Get-ADUser $possibleName
  28.     }
  29.     catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
  30.     }
  31.         if (!$usernameToCheck) {
  32.             write-host "Found available username: $possibleName"
  33.             break
  34.         }
  35.         else { Write-Host "Username $possibleName taken"}
  36. }
Advertisement
Add Comment
Please, Sign In to add comment