Solved

Pwrshell - Generate username from firstname and lastname

Posted on 2013-02-06
3
1,184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read https://technet.microsoft.com/en-us/windows-server-docs/security/securing-privileged-access/s…
A hard and fast method for reducing Active Directory Administrators members.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 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