Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Import-Module ActiveDirectory
- $path = Split-Path -Parent $MyInvocation.MyCommand.Definition
- $newpath = $path + "import_users.csv"
- # Define variables
- $log = $path + "created_ActiveDirectory_users.log"
- $date = Get-Date
- $i = 0
- function createActiveDirectoryUsers {
- "Created the following Active Directory users (on " + $date + "): " | Out-File $log -Append
- "--------------------------------------------" | Out-File $log -Append
- Import-Csv $newpath | ForEach-Object {
- $samAccount = $_.SamAccountName
- try {
- $exists = Get-ADUser -LDAPFilter "(sAMAccountName=$samAccount)"
- } catch { }
- if (!$exists) {
- $i++
- # Set all variables according to the table names in the Excel
- # sheet / import CSV. The names can differ in every project, but
- # if the names change, make sure to change it below as well.
- $setpass = ConvertTo-SecureString -AsPlainText $_.Password -Force
- New-ADUser -Name $_.DisplayName -SamAccountName $_.SamAccountName -GivenName $_.GivenName -Initials $_.Initials `
- -Surname $_.SN -DisplayName $_.DisplayName -Office $_.OfficeName `
- -Description $_.Description -EmailAddress $_.eMail `
- -StreetAddress $_.StreetAddress -City $_.L `
- -PostalCode $_.PostalCode -Country $_.CO -UserPrincipalName $_.UPN `
- -Company $_.Company -Department $_.Department -EmployeeID $_.ID `
- -OfficePhone $_.Phone -AccountPassword $setpass -Enabled $true -Path $_.OU
- $output = $i.ToString() + ") Name: " + $_.CN + " sAMAccountName: "
- $output += $sam + " Pass: " + $_.Password
- $output | Out-File $log -append
- } else {
- "SKIPPED - USER ALREADY EXISTS OR ERROR: " + $_.CN | Out-File $log -append
- }
- }
- "----------------------------------------" + "`n" | Out-File $log -append
- }
- createActiveDirectoryUsers
- $_.DisplayName
- #Test to make sure your output looks correct
- #You can do this by running the following:
- Import-csv ".import_create_ad_users.csv" | Out-GridView
- # ERROR REPORTING ALL
- # When strict mode is on, Windows PowerShell generates a terminating error when the content of an expression, script, or script block violates basic best-practice coding rules.
- Set-StrictMode -Version latest
- Import-Module ActiveDirectory
- $path = Split-Path -Parent $MyInvocation.MyCommand.Definition
- $newpath = $path + "import_users.csv"
- # Define variables
- $log = $path + "created_ActiveDirectory_users.log"
- $date = Get-Date
- $i = 0
- Function createActiveDirectoryUsers {
- "Created the following Active Directory users (on " + $date + "): " | Out-File $log -Append
- "--------------------------------------------" | Out-File $log -Append
- #Import CSV
- Import-Csv $newpath | ForEach-Object {
- # Check to see if SamAccount exists
- $samAccount = $_.SamAccountName
- try {
- $exists = Get-ADUser -LDAPFilter "(sAMAccountName=$samAccount)"
- } catch { }
- If(!$exists){
- #Convert Password to Secure String
- $setpass = ConvertTo-SecureString -AsPlainText $_.Password -Force
- Try
- {
- New-ADUser -Name $_.DisplayName -SamAccountName $_.SamAccountName -GivenName $_.GivenName -Initials $_.Initials `
- -Surname $_.SN -DisplayName $_.DisplayName -Office $_.OfficeName `
- -Description $_.Description -EmailAddress $_.eMail `
- -StreetAddress $_.StreetAddress -City $_.L `
- -PostalCode $_.PostalCode -Country $_.CO -UserPrincipalName $_.UPN `
- -Company $_.Company -Department $_.Department -EmployeeID $_.ID `
- -OfficePhone $_.Phone -AccountPassword $setpass -Enabled $true -Path $_.OU
- $dn = (Get-ADUser $_.SamAccountName).DistinguishedName
- # Rename the object to a good looking name
- $newdn = (Get-ADUser $_.SamAccountName).DistinguishedName
- Rename-ADObject -Identity $newdn -NewName ($_.GivenName + " " + $_.Initials + " "+ $_.SN)
- #Create Log
- "[INFORMATION]`t Renamed the user $($_.SamAccountName) to $($_.GivenName) $($_.SN)`r`n" | Out-File $log -append
- "[INFORMATION]`t Created new user named: $($_.SamAccountName)" | Out-File $log -append
- "[INFORMATION]`t Password for new user: $($_.Password)" | Out-File $log -append
- }
- Catch
- {
- # Error if something with the user was broken
- Write-Host "[ERROR]`t Oops, something went wrong: $($_.Exception.Message)`r`n"
- }
- }
- Else
- {
- Write-Host "[SKIPPED]`t User $($_.SamAccountName) ($($_.GivenName) $($_.SN)) already exists or returned an error!`r`n"
- "[SKIPPED]`t User $($_.SamAccountName) ($($_.GivenName) $($_.SN)) already exists or returned an error!" | Out-File $log -append
- }
- $i++
- }
- "Processing ended (on " + $date + "): " | Out-File $log -append
- "--------------------------------------------" + "`r`n" | Out-File $log -append
- }
- createActiveDirectoryUsers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement