Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell Create unqiue AD users by adding ascending number to samaccountname

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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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 antispam), the admini…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 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