Advertisement
Guest User

Spaghetti (Hello, Powershell)

a guest
Aug 14th, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###################################################################
  2. #Author  :  oneZergArmy
  3. #Date    :  14.08.2015
  4. #Version :  1.1
  5. #Desc.   :  Create users from .csv file and add them to a group
  6. ###################################################################
  7.  
  8. Import-Module ActiveDirectory
  9.  
  10. #Imports the .csv file. Remember to change the path and name of the file.
  11. $csv = import-csv -Path 'C:\Students-ICT.csv' -Delimiter ';'
  12.  
  13. #Goes through each row in the .csv file.
  14. foreach ($row in $csv)
  15. {
  16.  
  17.     #------------------------------------------------------------------
  18.     #VARIABLES - Change these
  19.     #------------------------------------------------------------------
  20.  
  21.     #Domain name for the server
  22.     $domain = '@lab.local' #testing server
  23.  
  24.     #Default password
  25.     $password = "Passord1"
  26.  
  27.     #Paths for were the users are added
  28.     $path = "ou=$row.Klasse,ou=Brukere,ou=IKT-FAG,dc=lab,dc=local"
  29.  
  30.     #------------------------------------------------------------------
  31.     #VARIABLES - Don't change
  32.     #------------------------------------------------------------------
  33.  
  34.     #Splits the given name and the surname into seperate variables.
  35.     $givenName = $row.Name.split()[0]
  36.     $surname = $row.Name.split()[1]
  37.    
  38.     #Gets the users class from the csv document
  39.     $class = $row.Klasse
  40.    
  41.     #Generates a username. Jake Doe = JakDoe
  42.     $username = $givenName.substring(0,3) + $surname.substring(0,3)
  43.    
  44.     #UserPrincipalName generator
  45.     $UPN = $username + $domain
  46.    
  47.     #Checks if the username already exists in AD
  48.     $checkUser = Get-ADUser -filter {SamAccountName -eq $username}
  49.  
  50.     #------------------------------------------------------------------
  51.     #Script
  52.     #------------------------------------------------------------------
  53.  
  54.     #Function for creating a user
  55.     Function createUser(){
  56.    
  57.         New-ADUser -Name $row.Name -GivenName $givenName -Surname $surname -displayName $row.Name -AccountPassword (ConvertTo-SecureString -AsPlainText $password -Force) -ChangePasswordAtLogon $true -SamAccountName $username -UserPrincipalName $UPN -Path $path -Enabled $true
  58.        
  59.     }
  60.    
  61.     #Checks if the user is already in AD. If so, the user will be named something like JakDoe2
  62.     if($checkUser){
  63.  
  64.         $username = $username + 2
  65.         createUser
  66.    
  67.     #If it does not exist, user will be created and named JakDoe
  68.     }else {
  69.  
  70.         createUser
  71.  
  72.     }
  73.  
  74.     #Add the user to the correct group
  75.     Add-ADGroupMember -Identity $class -Member $username    
  76.    
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement