Advertisement
Guest User

Opgave 5

a guest
May 28th, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function CheckOU($Path) {
  2.  
  3.     #Check if the does not exist, then create it
  4.     if(![adsi]::Exists("LDAP://$Path")) {
  5.        
  6.         #Notify user
  7.         Write-Host "Path does not exist, creating";
  8.  
  9.         #Find the OU. This will only work if its the first OU that does not exist.
  10.         #If its deeper like "OU=Y,OU=X,OU=Users,OU=Paca,DC=paca,DC=dk" it will fail as X does not exist and its trying to create Y
  11.  
  12.         $newOUName = $Path.Split(",");
  13.         $newOUName = $newOUName[0].Split("=");
  14.         $newOUName = $newOUName[1];
  15.  
  16.         New-ADOrganizationalUnit -Name $newOUName -Path "OU=Users,OU=Paca,DC=paca,DC=dk";
  17.     }
  18. }
  19.  
  20. function AddADUsers($userList) {
  21.     #Secure the password to be used for new users
  22.     $pass = ConvertTo-SecureString -String "P@ssw0rd" -AsPlainText -Force;
  23.  
  24.     #Loop through the user list
  25.     Foreach($user in $userList) {
  26.    
  27.         #Tell the user we are creating a new account
  28.         Write-Host "Creating account '$($user.SamAccountName)' in $($user.Path)"
  29.        
  30.         #Check if the OU exist otherwise it will create it
  31.         CheckOU($user.Path);
  32.  
  33.         # Try add the account to AD
  34.         Try
  35.         {
  36.             #Make user hash
  37.             $samAccountName = "$($user.Name[0])$($user.Surname)";
  38.             $hash = @{
  39.                 Name = "$($user.Name) $($user.Surname)"
  40.                 Displayname = "$($user.Name) $($user.Surname)"
  41.                 Path = $($user.Path)
  42.                 Surname = $user.Surname
  43.                 GivenName = $user.Givenname
  44.                 Samaccountname = $samAccountName
  45.                 Title = $user.Title
  46.                 Department = $user.Department
  47.                 AccountPassword = $pass
  48.                 Enabled = $True
  49.                 ChangePasswordAtLogon = $True
  50.                 Description = $user.Description
  51.                 City = $user.City
  52.             }
  53.  
  54.             #Parse in the user hash to AD
  55.  
  56.             New-ADUser @hash -PassThru;
  57.            
  58.             #Enables mailbox for user
  59.             #Enable-Mailbox -Identity $samAccountName -Alias $samAccountName
  60.         }
  61.  
  62.         #Catch the "identity already exists Exception
  63.         Catch [Microsoft.ActiveDirectory.Management.ADIdentityAlreadyExistsException]
  64.         {
  65.             #Generate random number to be used for later
  66.             $randomNumber = Get-Random -minimum 1000 -maximum 9999;
  67.  
  68.             #Create the new SamAacountName
  69.             $samAccountName = "$($user.Name[0])$($user.Surname)$($randomNumber)";
  70.  
  71.             #Tell the user that the user was already in AD and what the new username will be
  72.             Write-Host "Username was already in AD, creating a new random username for '$($user.Name) $($user.Surname)', new username is '$($samAccountName)'";
  73.  
  74.             #Create hash
  75.             $hash = @{
  76.                 Name = "$($user.Name) $($user.Surname) $($randomNumber)"
  77.                 Displayname = "$($user.Name) $($user.Surname)"
  78.                 Path = $($user.Path)
  79.                 Surname = $user.Surname
  80.                 GivenName = $user.Givenname
  81.                 Samaccountname = $samAccountName
  82.                 Title = $user.Title
  83.                 Department = $user.Department
  84.                 AccountPassword = $pass
  85.                 Enabled = $True
  86.                 ChangePasswordAtLogon = $True
  87.                 Description = $user.Description
  88.                 City = $user.City
  89.             }
  90.  
  91.             #Add the user hash to AD
  92.             New-ADUser @hash -PassThru
  93.            
  94.             #Enables mailbox for user
  95.             #Enable-Mailbox -Identity $samAccountName -Alias $samAccountName
  96.         }
  97.         Catch
  98.         {
  99.             # Some other terrible error occured!
  100.             Write-Error "Something went totally wrong!"
  101.         }  
  102.     }
  103. }
  104.  
  105. function ImportUsersFromCsv($path) {
  106.     Write-Host "Starting to import users"
  107.  
  108.     #Import user list
  109.     $userList = Import-Csv $path;
  110.  
  111.     #Add the users
  112.     AddADUsers($userList);
  113. }
  114.  
  115.  
  116. function ImportUsersFromXml($path) {
  117.     Write-Host "Starting to import users"
  118.  
  119.     #Import user list
  120.     $userList = Import-Clixml $path;
  121.  
  122.     #Add the users
  123.     AddADUsers($userList);
  124. }
  125.  
  126.  
  127. function ConvertFromCsvToXml() {
  128.    
  129.     Import-CSV "C:\Users\pawo\Desktop\UsersToImport.csv" | Export-CliXML "C:\Users\pawo\Desktop\UsersToImport.xml"
  130. }
  131.  
  132.  
  133.  
  134. #ImportUsersFromCsv -path "C:\Users\pawo\Desktop\UsersToImport.csv"
  135. ImportUsersFromXml -path "C:\Users\pawo\Desktop\UsersToImport.xml"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement