+ Post New Thread
Results 1 to 3 of 3
Scripts Thread, User Creation and verification help? in Coding and Web Development; HI, First post here, so be gentle! And apologies for the length of the post - just trying to put ...
  1. #1

    Join Date
    Dec 2013
    Posts
    3
    Thank Post
    2
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    User Creation and verification help?

    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

  2. #2
    penfold_99's Avatar
    Join Date
    Feb 2008
    Location
    East Sussex
    Posts
    985
    Thank Post
    58
    Thanked 165 Times in 117 Posts
    Rep Power
    68
    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;
    }
    }

    }

  3. Thanks to penfold_99 from:

    NoelT (9th December 2013)

  4. #3

    Join Date
    Dec 2013
    Posts
    3
    Thank Post
    2
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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

SHARE:
+ Post New Thread

Similar Threads

  1. Active Directory - Single and Bulk User creation and FREE!!
    By siuko in forum How do you do....it?
    Replies: 39
    Last Post: 20th June 2012, 11:29 AM
  2. Replies: 4
    Last Post: 15th November 2010, 11:13 AM
  3. Bulk User Creation Tools
    By mark in forum Downloads
    Replies: 16
    Last Post: 10th July 2007, 04:24 PM
  4. Bulk user creation
    By Dos_Box in forum How do you do....it?
    Replies: 63
    Last Post: 13th November 2006, 07:02 PM
  5. Free MSI Creation and how to do it
    By daverage in forum Wireless Networks
    Replies: 1
    Last Post: 22nd November 2005, 10:03 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •