Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell Create unqiue AD users by adding ascending number to samaccountname

Posted on 2015-01-26
2
Medium Priority
?
779 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 85

Accepted Solution

by:
oBdA earned 2000 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

Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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 anti-spam), the admin…
Loops Section Overview

927 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