Solved

Pwrshell - Generate username from firstname and lastname

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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