Solved

Pwrshell - Generate username from firstname and lastname

Posted on 2013-02-06
3
1,054 Views
Last Modified: 2013-02-20
Hi,

I have a variable with a users firstname ($FirstName) and another variable with the users lastname ($LastName).

What I want is a new variable with a generated username.

The username should either be:
1. The first two characters from $FirstName + the first character from $LastName
2. The first character from $FirstName + the first two characters from $LastName
3. The first and third character from $FirstName + the first character from $LastName
4. The first character from $FirstName + the first and third character from $LastName
5. The first two characters from $FirstName + the second character from $LastName

I want it check up against our AD to verify that the username is not taken. If it is, it need to check username-option 2. If username-option 2 is taken, it should check for username option 3 and so on.

When it finds a free username, it should be set as $Username.

Is it possible or is there a better solution?
0
Comment
Question by:Sum Wum
3 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38859212
here:

cls
$FirstName = "jonny"
$LastName = "begood"

function isUserExists($username){
	return Get-ADUser -LDAPFilter "(sAMAccountName=$username)"
}

$NameOptions = ($FirstName[0,1]+$LastName[0] -join ('')).Replace(' ',''), 
				($FirstName[0]+$LastName[0,1] -join ('')).Replace(' ',''), 
				($FirstName[0,2]+$LastName[0] -join ('')).Replace(' ',''), 
				($FirstName[0]+$LastName[0,2] -join ('')).Replace(' ',''), 
				($FirstName[0,1]+$LastName[1] -join ('')).Replace(' ','')
				
foreach ($name in $NameOptions){
	$res = isUserExists $name 
	if($res -ne $null){
		break
	}
}

#found $name 

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 38860512
The part in lines 9 to 13 could be shortened:
# old:
  ($FirstName[0,1]+$LastName[0] -join ('')).Replace(' ','')[/b]
# new:
  -join $FirstName[0,1]+$LastName[0] -replace ' '[/b]

Open in new window

aso.
I think the check for isUserExists is wrong - it return something (not $null) if the user exists, and so the check should be reversed (-eq $null).
In addition we should also consider all suggested combinations used up already. $res will still have a value in that case after the foreach (while being $null if there is a free combination found).
0
 
LVL 1

Author Closing Comment

by:Sum Wum
ID: 38912885
More than perfect! Thanks!!!
0

Join & Write a Comment

This article will help you understand what HashTables are and how to use them in PowerShell.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now