#set user ids (AD: employeeid) #globals that will need changing $newAccountExpires = ([DateTime]"09/30/2015") # OU to create users in $container = [ADSI] "LDAP:///" #Function to retrieve required *fieldlist* where they match the *category* from active directory function Get-ADObjects{ param ( $searchroot = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain(), $Category = "(objectCategory=computer)", $fieldlist = @("name", "cn") ) $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = "LDAP://$searchroot" $objSearcher.Filter = ($Category) $objSearcher.SearchScope = "subtree" $objSearcher.pagesize = 10000 # TODO: request / consolidate from all domain controllers foreach ($i in $fieldlist) { $temp = $objSearcher.PropertiesToLoad.Add($i) } $colResults = $objSearcher.FindAll() $Results = @() foreach ($objResult in $colResults) { $Result = New-Object PSObject foreach ($Property in $objResult.Properties.getenumerator()) { if($Property.Value) { $Result | Add-Member NoteProperty $Property.Key ([string]$Property.Value) } } $Results += $Result } $Results } ########### Start Here ########### # CSV File to import from, TXiES SRG0200 export + aup and login columns $kescsv = Import-Csv "KES STUDENTS 14-15 + LOGINS.csv" # TXiES Report SRG0200 # cSV Format: ADCreatedDate,LOGIN,AUP,newuser,Name,Last,First,Middle,mid2,StudentID,Grade,CY,Sex,DOB,Phone,Instructor,Displaed Name,Dos[;ayed,Picture,group picture,Work $newstudents = 0 # Loop through each record (line) in the CSV file foreach($user in $kescsv){ if($user.login.trim() -ne "") { # $user | Get-Member | out-host # write-host $user.login ":" $user.studentid # find the user record in active directory $cat = [string]::format("(&(objectcategory=User)(samaccountname={0}))",$user.login.trim()) $adusers = get-adobjects -Category $cat -fieldlist @("cn", "name", "employeeid", "samaccountname", "accountExpires", "whenCreated", "homedirectory") # Check if we have any matched users if(($adusers -eq $null) -or ($adusers.count -eq 0)) { # No user found check if we have a confirmed AUP if($user.aup -eq "Y") { $newstudents++ # Work out graduation year $gradyear =0 if($user.grade -eq "EE") { $gradyear= ((Get-Date).year) + 14 } elseif($user.grade -eq "PK") { $gradyear= ((Get-Date).year) + 14 } elseif($user.grade -eq "KG"){ $gradyear= ((Get-Date).year) + 13 } else { $gradyear = (((Get-Date).year) + 13) - $user.grade } # Student Name $lastname = (Get-Culture).TextInfo.ToTitleCase($user.last.tolower()) $surname = (Get-Culture).TextInfo.ToTitleCase($user.first.tolower()) if($user.middle.length -gt 0) { $surname = $surname + " " + $user.middle.substring(0,1) + "." } $displayname = $lastname + ", " + $surname write-host "Create User " $user.login ":" $user.studentid " not found in AD " $user.grade $gradyear $displayname # Confirmation to create user $message = "Create User " + $user.login + ":" + $user.studentid + " not found in AD " + $user.grade + ":" + $gradyear + ":" + $displayname $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Create User in active directory" $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Skip creating this user and go onto next" $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no) $result = $host.ui.PromptForChoice("", $message, $options, 0) switch ($result) { 0 {"You selected Yes."} 1 {break} } # Active Directory user creation details $acduserName = $displayname $acduserName = $acduserName -replace ",", "\," $acduser = $container.Create("User", "cn=" + $acduserName) $acduser.Put("sAMAccountName", $user.login) $acduser.Put("givenName", $lastname) $acduser.Put("sn", $surname) $acduser.Put("displayName", $displayname) $acduser.Put("name", $displayname) $acduser.Put("employeeID", $user.studentid) $acduser.put("userPrincipalName", $user.login + "@") $acduser.put("Description", "Elem Student-" + $gradyear) $homedir = "\\\Class of " + $gradyear + "\" + $user.login $acduser.put("homedirectory",$homedir) $acduser.put("homedrive","h:") $acduser.psbase.InvokeSet("AccountExpirationDate",$newAccountExpires) $acduser.SetInfo() #$acduser.userFlags = 65536 -band 64 $acduser.psbase.InvokeSet('AccountDisabled', $false) $acduser.SetInfo() $acduser.SetPassword($user.studentid) $acduser.userAccountControl = 66048 $acduser.SetInfo() # Add user to groups Class of xxxx and xxxx Students if ($acduser -ne $null) { $group = [ADSI] ("LDAP:///CN=Class of " + $gradyear + ",") $group.psbase.Invoke("Add",$acduser.AdsPath) $group = [ADSI] "LDAP:///CN=xxxx Students," $group.psbase.Invoke("Add",$acduser.AdsPath) } # wait for a key #$HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL #$HOST.UI.RawUI.Flushinputbuffer() } } else { # process each user returned from active directory search (should only be one as where searching on unique samaccount field) foreach($aduser in $adusers) { $userAcc =[ADSI] ($aduser.adspath) if($aduser.employeeid -ne $user.studentid) { write-host "ids dont match" $aduser.employeeid "," $aduser.samaccountname "," $aduser.whenCreated "," $user.studentid # ask to set id ( needs confirmation id your updating is actually the student you think it is) } else { #write-host $aduser.employeeid "," $aduser.samaccountname "," $aduser.whenCreated "," $user.studentid # only update the user if the ids match if($user.AUP -eq "Y") { # check to see if student account expiry is before expiry if (([datetime]::fromfiletime($aduser.accountExpires)) -lt $newAccountExpires) { write-host "Updating expiry for " $aduser.samaccountname ":" $aduser.employeeID ":" ([datetime]::fromfiletime($aduser.accountExpires)) # $userAcc | Get-Member | out-host # Set new accountExpires $userAcc.psbase.InvokeSet("AccountExpirationDate",$newAccountExpires) #$userAcc.put("employeeid", [string]$user.studentid) #$userAcc.psbase.CommitChanges() $userAcc.setinfo() } # check to see if theres a user folder if(!(Test-Path -Path $aduser.homedirectory )){ write-host "creating directory " $aduser.homedirectory #New-Item -ItemType directory -Path $aduser.homedirectory } } } } } } } write-host "new student count" $newstudents