Jump to content

Recommended Posts

Posted

HI,

 

First post here, so be gentle! And apologies for the length of the post - just trying to put as much info as possible.

 

I've had a search, both here and on the wider web, but can't seem to find what I'm looking for.

 

2012 Domain.

 

Got some scripts working for bulk user creation - that bit is fine, creating user, creating setting home folder perms etc. creating remote share etc. etc. all good.

 

I have a situation in a school where they want to automate creation of single users, again this is working fine.

 

The issue I have is checking for the existence of a matching username.

 

The school username format is: year of entry, 4 characters from surname, number, first character of firstname.

 

So pupil Fred Bloggs starting this year is 13Blog1F. Again, based on text input and substring this is easy to get. I've even got it so that if Fred Bloggs already exists and Frank Bloggs starts it will do 13Blog2F. my issue is on the rare occasion we would get 13Blog3F necessary I'm struggling to get this to go.

 

An example of the code, minus all the group and home settings is:

 

# Single User Creation - NoelT 2013

# Ask Questions and Generate usernames

$firstname = Read-Host 'First Name?'

$surname = Read-Host 'Surname?'

$YoE = Read-Host 'Year of Entry (YY format)?'

$pwd = Read-Host 'Password?'

$Surnameshort = $surname.substring(0,4)

$firstnameshort = $firstname.substring(0,1)

$fullname = ($yoe+$surnameshort+1+$firstnameshort)

$altname = ($yoe+$surnameshort+2+$firstnameshort)

$alt3 = ($yoe+$surnameshort+3+$firstnameshort)

$alt4 = ($yoe+$surnameshort+4+$firstnameshort)

$alt5 = ($yoe+$surnameshort+5+$firstnameshort)

 

 

 

 

 

 

# Create User based on info provided in the correct OU

 

 

 

 

# To check for existence of user in AD first

if (dsquery user -samid $fullname){

new-aduser -Enabled $true -AccountPassword (ConvertTo-SecureString -AsPlainText $pwd -Force) -SamAccountName $altname -GivenName $firstname -Surname $surname -name $altname -Displayname ("$firstname" + " " + "$surname") -Description "Student User" -path "OU=Test,DC=domain,DC=co,DC=uk" -ChangePasswordAtLogon $true

}

 

 

else {

new-aduser -Enabled $true -AccountPassword (ConvertTo-SecureString -AsPlainText $pwd -Force) -SamAccountName $fullname -GivenName $firstname -Surname $surname -name $fullname -Displayname ("$firstname" + " " + "$surname") -Description "Student User" -path "OU=Test,DC=domain,DC=co,DC=uk" -ChangePasswordAtLogon $true

}

 

I've tried elseif too, but can't seem to get it going.

 

Any suggestions would be massively welcome.

 

Thanks in advance,

 

Noel

Posted

Hi,

 

You could put it in a loop (not sure was language your using so the following will need tweaking)

 

for ($i = 1; $i < 10; $i++)

{

$fullname = ($yoe+$surnameshort+$i+$firstnameshort)

if (dsquery user -samid $fullname){

new-aduser -Enabled $true -AccountPassword (ConvertTo-SecureString -AsPlainText $pwd -Force) -SamAccountName $altname -GivenName $firstname -Surname $surname -name $altname -Displayname ("$firstname" + " " + "$surname") -Description "Student User" -path "OU=Test,DC=domain,DC=co,DC=uk" -ChangePasswordAtLogon $true

break;

}

}

 

}

  • Thanks 1
Posted
Hi Penfold,Thanks for taking the time to reply. I'm using powershell on 2012. But that looks like it will do the trick.I will try that when I get to the office tomorrow. Cheers

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...