Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $content=Import-Csv -Path C:\test\ad.csv -Delimiter ";"
  2.  
  3. $domainName=Get-ADDomain | Select -ExpandProperty Forest
  4. $userContainer=Get-ADDomain | Select -ExpandProperty UsersContainer
  5.  
  6. $defaultUserContainer=$userContainer.Substring(3,$userContainer.IndexOf(",")-3)
  7.  
  8. foreach($line in $content)
  9. {
  10.    
  11.     $name=$line.("name")
  12.     $surname=$line.("surname")
  13.     $givenName=$line.("GivenName")
  14.     $displayName=$line.("DisplayName")
  15.  
  16.    
  17.         if ($line.("CannotChangePassword")  -like "*true*")
  18.         {
  19.             $cannotChangePassword=$true
  20.         }
  21.         else
  22.         {
  23.             $cannotChangePassword=$false
  24.         }
  25.        
  26.  
  27.         if ($line.("PasswordNeverExpires")  -like "*true*")
  28.         {
  29.             $passwordNeverExpires=$true
  30.         }
  31.         else
  32.         {
  33.             $passwordNeverExpires=$false
  34.         }
  35.  
  36.         $samAccountName=$line.("SamAccountName")
  37.    
  38.         $upn=$line.("UPN") #bool
  39.        
  40.         if ($line.("UPN") -eq $samAccountName+"@"+$domainName)
  41.         {
  42.             $upn=$line.("UPN")
  43.         }
  44.         else
  45.         {
  46.             "Error in UPN. UPN is set as NULL" >> test.txt
  47.             $upn=""
  48.         }
  49.  
  50.  
  51.         $ou=$line.("OU")
  52.         $checkOUInAD=Get-ADOrganizationalUnit -Filter { name -like '$ou'} | Select  -ExpandProperty name
  53.         if($checkOUInAD -eq $ou)
  54.         {
  55.             $folder="OU="+$ou
  56.         }
  57.         else
  58.         {
  59.             "Error in Organizational Unit. User added into default Container" >> test.txt
  60.             $folder="CN="+$defaultUserContainer
  61.         }
  62.         $folder
  63.  
  64.         if ($line.("Enabled") -like "*true*")
  65.         {
  66.             $enabled=$true
  67.         }
  68.         else
  69.         {
  70.             $enabled=$false
  71.         }
  72.        
  73. New-ADUser -Name $name -Surname $surname -GivenName $givenName -DisplayName $displayName -CannotChangePassword $cannotChangePassword -PasswordNeverExpires $passwordNeverExpires -SamAccountName $samAccountName -UserPrincipalName $upn -Path "$folder,DC=domena,DC=local" -AccountPassword(Read-Host -AsSecureString "Input Password") -Enabled $enabled
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement