Solved

Powershell Create unqiue AD users by adding ascending number to samaccountname

Posted on 2015-01-26
2
416 Views
Last Modified: 2015-01-27
Hi,

The company I'm working  at has a username policy: 2 first characters Givenname + 2 first characters Surname + ##
Example: John Smith will be given username josm01

I have this script that creates users. A part of the script defines samaccountname:

$sam = $_.Givenname.substring(0,2).ToLower() + $_.Lastname.substring(0,2).ToLower() + "01"
        Try   { $exists = Get-ADUser -LDAPFilter "(sAMAccountName=$sam)" }
        Catch { }
        If(!$exists)

Open in new window


If the username does not exist, the user is created.
If the username DOES exist, a line is written to a logfile and the creation of that user is cancelled.

I would like to have an adaption to the script:
If the username is found, the 01 is incremented by 1, to 02, or to the first available number.

Thanks in advance
0
Comment
Question by:Loyall
2 Comments
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
Get-ADUser with a correct LDAP filter doesn't throw an exception if it can't find the user, it just returns nothing.
Try this:
$SamPrefix = $u.Givenname.Substring(0, 2).ToLower() + $u.Lastname.Substring(0, 2).ToLower()
$ADOK = $True
$SamOK = $False
$Index = 1
Do {
	$sam = $SamPrefix + ("{0:D2}" -f $Index++)
	"Trying '$($sam)' ... " | Write-Host -NoNewline
	Try {
		If (Get-ADUser -LDAPFilter "(sAMAccountName=$sam)" -ErrorAction Stop) {
			"already taken." | Write-Host
		} Else {
			"OK, still free." | Write-Host
			$SamOK = $True
		}
	}
	Catch {
		"Error!" | Write-Host
		$_.Exception | Out-String | Write-Error
		$ADOK = $False
	}
} Until ($SamOK -Or !$ADOK -Or ($Index -ge 99))
If ($SamOK) {
	"Creating new user with samid '$sam'" | Write-Host
} ElseIf (!$ADOK) {
	"Error accessing AD!" | Write-Host
} Else {
	"Unable to find a free index between 1 and 99!" | Write-Host
}

Open in new window

0
 
LVL 2

Author Comment

by:Loyall
Comment Utility
@oBdA

Thank you !
Works great !
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

9 Experts available now in Live!

Get 1:1 Help Now