Solved

Pwrshell - Generate username from firstname and lastname

Posted on 2013-02-06
3
1,131 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 69

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

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Ouput in html in powershell 2 21
remote WMI RPC server is unavailable. 4 11
New-Aduser from SQL 27 20
LDAPS Server 2012 R2 Error 0 5 26
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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 configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

821 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