Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function CheckOU($Path) {
- #Check if the does not exist, then create it
- if(![adsi]::Exists("LDAP://$Path")) {
- #Notify user
- Write-Host "Path does not exist, creating";
- #Find the OU. This will only work if its the first OU that does not exist.
- #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
- $newOUName = $Path.Split(",");
- $newOUName = $newOUName[0].Split("=");
- $newOUName = $newOUName[1];
- New-ADOrganizationalUnit -Name $newOUName -Path "OU=Users,OU=Paca,DC=paca,DC=dk";
- }
- }
- function AddADUsers($userList) {
- #Secure the password to be used for new users
- $pass = ConvertTo-SecureString -String "P@ssw0rd" -AsPlainText -Force;
- #Loop through the user list
- Foreach($user in $userList) {
- #Tell the user we are creating a new account
- Write-Host "Creating account '$($user.SamAccountName)' in $($user.Path)"
- #Check if the OU exist otherwise it will create it
- CheckOU($user.Path);
- # Try add the account to AD
- Try
- {
- #Make user hash
- $samAccountName = "$($user.Name[0])$($user.Surname)";
- $hash = @{
- Name = "$($user.Name) $($user.Surname)"
- Displayname = "$($user.Name) $($user.Surname)"
- Path = $($user.Path)
- Surname = $user.Surname
- GivenName = $user.Givenname
- Samaccountname = $samAccountName
- Title = $user.Title
- Department = $user.Department
- AccountPassword = $pass
- Enabled = $True
- ChangePasswordAtLogon = $True
- Description = $user.Description
- City = $user.City
- }
- #Parse in the user hash to AD
- New-ADUser @hash -PassThru;
- #Enables mailbox for user
- #Enable-Mailbox -Identity $samAccountName -Alias $samAccountName
- }
- #Catch the "identity already exists Exception
- Catch [Microsoft.ActiveDirectory.Management.ADIdentityAlreadyExistsException]
- {
- #Generate random number to be used for later
- $randomNumber = Get-Random -minimum 1000 -maximum 9999;
- #Create the new SamAacountName
- $samAccountName = "$($user.Name[0])$($user.Surname)$($randomNumber)";
- #Tell the user that the user was already in AD and what the new username will be
- Write-Host "Username was already in AD, creating a new random username for '$($user.Name) $($user.Surname)', new username is '$($samAccountName)'";
- #Create hash
- $hash = @{
- Name = "$($user.Name) $($user.Surname) $($randomNumber)"
- Displayname = "$($user.Name) $($user.Surname)"
- Path = $($user.Path)
- Surname = $user.Surname
- GivenName = $user.Givenname
- Samaccountname = $samAccountName
- Title = $user.Title
- Department = $user.Department
- AccountPassword = $pass
- Enabled = $True
- ChangePasswordAtLogon = $True
- Description = $user.Description
- City = $user.City
- }
- #Add the user hash to AD
- New-ADUser @hash -PassThru
- #Enables mailbox for user
- #Enable-Mailbox -Identity $samAccountName -Alias $samAccountName
- }
- Catch
- {
- # Some other terrible error occured!
- Write-Error "Something went totally wrong!"
- }
- }
- }
- function ImportUsersFromCsv($path) {
- Write-Host "Starting to import users"
- #Import user list
- $userList = Import-Csv $path;
- #Add the users
- AddADUsers($userList);
- }
- function ImportUsersFromXml($path) {
- Write-Host "Starting to import users"
- #Import user list
- $userList = Import-Clixml $path;
- #Add the users
- AddADUsers($userList);
- }
- function ConvertFromCsvToXml() {
- Import-CSV "C:\Users\pawo\Desktop\UsersToImport.csv" | Export-CliXML "C:\Users\pawo\Desktop\UsersToImport.xml"
- }
- #ImportUsersFromCsv -path "C:\Users\pawo\Desktop\UsersToImport.csv"
- ImportUsersFromXml -path "C:\Users\pawo\Desktop\UsersToImport.xml"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement