Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. #Define location of my script variable
  2. #the -parent switch returns one directory lower from directory defined.
  3. #below will return up to ImportADUsers folder
  4. #and since my files are located here it will find it.
  5. #It failes withpout appending "*.*" at the end
  6. #This file is required to update fields for existing users
  7. #Modify this script to create new users in UnifiedGov domain
  8.  
  9.  
  10. # $path = Split-Path -parent "C:\Users\administrator.CAPSCG\Desktop\migration\scripts\*.*"
  11.  
  12.  
  13. #Define CSV and log file location variables
  14. #they have to be on the same location as the script
  15.  
  16. $csvfile = "C:\Users\Administrator\Desktop\migration\scripts\testcsv.csv"
  17. $logfile = "C:\Users\Administrator\Desktop\migration\scripts\logfile.txt"
  18. $i = 0
  19. $date = Get-Date
  20.  
  21. #Define variable for a server with AD web services installed
  22.  
  23. $ADServer = 'CAPSCG'
  24.  
  25.  
  26. #Get Admin accountb credential
  27.  
  28. $GetAdminact = Get-Credential
  29.  
  30. #Import Active Directory Module
  31.  
  32. Import-Module ActiveDirectory
  33.  
  34. #Set the OU to add new users.
  35.  
  36. $location = "OU=Test,OU=newusers,DC=capscg,DC=local"
  37.  
  38.  
  39. #Import CSV file and update users in the OU with details in the fileh
  40. #Create the function script to update the users
  41.  
  42. # Function Create-ADUsers {
  43.  
  44. # "AD user creation logs for( " + $date + "): " | Out-File $logfile -append
  45. # "--------------------------------------------" | Out-File $logfile -append
  46.  
  47. Import-Csv -Path $csvfile | ForEach-Object {
  48.  
  49. $GivenName = $_.'firstname'
  50. $Surname = $_.'lastname'
  51. $Username = “$($FirstName)$($Surname)”
  52. # $ManagerDN = (Get-ADUser -server $ADServer -Credential $GetAdminact -LDAPFilter "(DisplayName=$Manager)").DistinguishedName #Manager required in DN format
  53.  
  54. #Define samAccountName to use with NewADUser in the format firstName.LastName
  55.  
  56. # $sam = $GivenName.ToLower() + "." + $Surname.ToLower()
  57.  
  58. #Define domain to use for UserPrincipalName (UPN)
  59.  
  60. $Domain = '@capscg.local'
  61.  
  62. #Define UserPrincipalname
  63.  
  64. $UPN = $sam + $Domain
  65.  
  66. #Now create new users using info from CSV
  67. #First check whether the user exist, if use is not in ad, create it
  68.  
  69. Try { $nameinAD = Get-ADUser -server $ADServer -Credential $GetAdminact -LDAPFilter "(sAMAccountName=$sam)" }
  70. Catch { }
  71. If(!$nameinAD)
  72. {
  73. $i++
  74.  
  75.  
  76. #Create new AD accounts using the info from the CSV
  77. #If "-enabled $TRUE" is not set, the account will be disabled by default
  78.  
  79. # $setpassword = (ConvertTo-SecureString "CAPS1234!" -AsPlainText -force)
  80.  
  81. 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
  82.  
  83. # Enable Mailbox
  84.  
  85. Enable-Mailbox $Username
  86.  
  87. # Move the users to the OU set above.
  88.  
  89. Move-ADObject -server $ADServer -Credential $GetAdminact -TargetPath $location
  90.  
  91. # Rename the object to a good looking name to avoid displaying sAMAccountNames (eg tests1.user1)
  92. #First create usernames as DNs, Rename-ADObject only accepts DistinguishedNames
  93.  
  94. #$newdn = (Get-ADUser -server $ADServer -Credential $GetAdminact -Identity $sam).DistinguishedName
  95. #Rename-ADObject -server $ADServer -Credential $GetAdminact -Identity $newdn -NewName $DisplayName
  96.  
  97. #Update log file with users created successfully
  98.  
  99. $DisplayName + " Created successfully" | Out-File $logfile -append
  100.  
  101. }
  102.  
  103. Else
  104. { #Update log file with users not created
  105. $DisplayName + " Not Created - User Already Exists" | Out-File $logfile -append
  106. }
  107.  
  108. }
  109.  
  110. # Run the function script
  111. Create-ADUsers
  112. #Finish
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement