Solved

Powershell Create unqiue AD users by adding ascending number to samaccountname

Posted on 2015-01-26
2
518 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 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 40570636
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
ID: 40572196
@oBdA

Thank you !
Works great !
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 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