Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Define location of my script variable
- #the -parent switch returns one directory lower from directory defined.
- #below will return up to ImportADUsers folder
- #and since my files are located here it will find it.
- #It failes withpout appending "*.*" at the end
- #This file is required to update fields for existing users
- #Modify this script to create new users in UnifiedGov domain
- # $path = Split-Path -parent "C:\Users\administrator.CAPSCG\Desktop\migration\scripts\*.*"
- #Define CSV and log file location variables
- #they have to be on the same location as the script
- $csvfile = "C:\Users\Administrator\Desktop\migration\scripts\testcsv.csv"
- $logfile = "C:\Users\Administrator\Desktop\migration\scripts\logfile.txt"
- $i = 0
- $date = Get-Date
- #Define variable for a server with AD web services installed
- $ADServer = 'CAPSCG'
- #Get Admin accountb credential
- $GetAdminact = Get-Credential
- #Import Active Directory Module
- Import-Module ActiveDirectory
- #Set the OU to add new users.
- $location = "OU=Test,OU=newusers,DC=capscg,DC=local"
- #Import CSV file and update users in the OU with details in the fileh
- #Create the function script to update the users
- # Function Create-ADUsers {
- # "AD user creation logs for( " + $date + "): " | Out-File $logfile -append
- # "--------------------------------------------" | Out-File $logfile -append
- Import-Csv -Path $csvfile | ForEach-Object {
- $GivenName = $_.'firstname'
- $Surname = $_.'lastname'
- $Username = “$($FirstName)$($Surname)”
- # $ManagerDN = (Get-ADUser -server $ADServer -Credential $GetAdminact -LDAPFilter "(DisplayName=$Manager)").DistinguishedName #Manager required in DN format
- #Define samAccountName to use with NewADUser in the format firstName.LastName
- # $sam = $GivenName.ToLower() + "." + $Surname.ToLower()
- #Define domain to use for UserPrincipalName (UPN)
- $Domain = '@capscg.local'
- #Define UserPrincipalname
- $UPN = $sam + $Domain
- #Now create new users using info from CSV
- #First check whether the user exist, if use is not in ad, create it
- Try { $nameinAD = Get-ADUser -server $ADServer -Credential $GetAdminact -LDAPFilter "(sAMAccountName=$sam)" }
- Catch { }
- If(!$nameinAD)
- {
- $i++
- #Create new AD accounts using the info from the CSV
- #If "-enabled $TRUE" is not set, the account will be disabled by default
- # $setpassword = (ConvertTo-SecureString "CAPS1234!" -AsPlainText -force)
- New-ADUser -Name “$FirstName $Surname” -GivenName $GivenName -Surname $Surname -SamAccountName $Username -Path $Path -AccountPassword (ConvertTo-SecureString “CAPS1234!” -AsPlainText -force) -OtherAttributes @{‘mail’=“$Username@caps.wa.edu.au”} -Enabled $true -ChangePasswordAtLogon $true
- # Enable Mailbox
- Enable-Mailbox $Username
- # Move the users to the OU set above.
- Move-ADObject -server $ADServer -Credential $GetAdminact -TargetPath $location
- # Rename the object to a good looking name to avoid displaying sAMAccountNames (eg tests1.user1)
- #First create usernames as DNs, Rename-ADObject only accepts DistinguishedNames
- #$newdn = (Get-ADUser -server $ADServer -Credential $GetAdminact -Identity $sam).DistinguishedName
- #Rename-ADObject -server $ADServer -Credential $GetAdminact -Identity $newdn -NewName $DisplayName
- #Update log file with users created successfully
- $DisplayName + " Created successfully" | Out-File $logfile -append
- }
- Else
- { #Update log file with users not created
- $DisplayName + " Not Created - User Already Exists" | Out-File $logfile -append
- }
- }
- # Run the function script
- Create-ADUsers
- #Finish
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement