Guest User

Untitled

a guest
Oct 12th, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module activedirectory
  2. $MasterFile
  3. $DomainController = (Get-ADDomain).DNSRoot
  4.  
  5. $companyUsersOU = (Get-ADDomain).DistinguishedName
  6. $UserPath = "OU=Dev," + $companyUsersOU
  7. $MasterFile = "C:\csv\export7.csv"
  8. $CSVADMismatch = "C:\csv\mismatch.csv"
  9. "Reading File"
  10. $CSV = Import-Csv $MasterFile
  11. "File Read"
  12. $ADuserMatch = @()
  13. $CSVuserMatch = @()
  14. $CSVMatch = @()
  15. $NoMatch = ""
  16. $ADMatch = @()
  17. $NoDupCSV = @()
  18. $CSVDUPmsg = ""
  19. $MisMatch = @()
  20. $ADDupsBody = ""
  21. $ADerror = ""
  22. $i = 0
  23. $ic = 0
  24. $it = 0
  25. $AdModifications = ""
  26. $Body = ""
  27. $EID = ""
  28. $Actions = ""
  29. $ADMANID = ""
  30. $CSVMANID = ""
  31. $PhoneMisMatch = ""
  32. $ShouldDisable = ""
  33. $DupCount = 0
  34. $ADChangeMade = 0
  35. $ManError = 0
  36. $Failed = 0
  37. $ADCreated = 0
  38.  
  39.  
  40. #########################################SMTP Settings##################################
  41. $From = "test@ctnetworks.net"
  42. $To = "ryan@ctnetworks.net"
  43. $Cc = "rgoldstein@itsavvy.com"
  44. #$Attachment = "C:\temp\Some random file.txt"
  45. $Subject = "Email Subject"
  46. $SMTPServer = "10.247.232.12"
  47. $SMTPPort = "25"
  48.  
  49.  
  50.  
  51.  
  52.  
  53. ######################################################################
  54.  
  55.  
  56.  
  57.  
  58. #Find and Remove Duplicates from CSV
  59.  
  60. "Creating Dup List"
  61. $DupList = $CSV | Group-Object -Property "Empl ID" | Where-Object { $_.Count -ge 2 } | ForEach-Object { $_.Group } #Group Hashtable by EmployeeID
  62. "DUPLIST Created"
  63.  
  64. if (($DupList.Count -ge 2) -and ($DupList -ne "")) {
  65.     foreach ($Dup in $DupList) {
  66.         $ic++
  67.         Write-Progress -Activity "Checking Users" -Status "User: $ic of $($DupList.Count)" -PercentComplete (($ic / ($DupList.Count)) * 100)
  68.         $CSVDUPmsg += "THE FOLLOWING DUPLICATE HAS BEEN FOUND PLEASE CHECK THIS USER MANUALLY " + $Dup.("First Name") + " " + $Dup.("Last Name") + ",  with employee ID " + $Dup.("Empl ID") + "`r`n" #duplicate users are enumurated into Body
  69.     }
  70.     "creating no DUP CSV"
  71.     $NoDupCSV = $CSV | Group-Object -Property "Empl ID" | Where-Object { $_.Count -le 1 } | ForEach-Object { $_.Group } #New CSV is created
  72.     "NO DUP CSV Created"
  73.     $body += "`r`n`r`n`r`n"
  74.  
  75. } else { $NoDupCSV = $csv } #if no dupes are found, hashtable is direct copied
  76.  
  77.  
  78. ############################
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. foreach ($User in $NoDupCSV) { #MAIN LOOP checks all users in CSV
  88.  
  89.     $i++
  90.     Write-Progress -Activity "Checking Users" -Status "User: $i of $($NoDupCSV.Count)" -PercentComplete (($i / ($NodupCSV.Count + 1)) * 100)
  91.  
  92.  
  93.     ################################Clears temp variables##########################################
  94.     if ($fname) { Remove-Variable fname } #clears firstname
  95.     if ($lname) { Remove-Variable lname } #clears lastname
  96.     if ($mname) { Remove-Variable mname } #clears lastname
  97.     if ($EID) { Remove-Variable EID } #cleras employeeID
  98.     if ($ADManID) { Remove-Variable ADmanID } #clears ManagerID if it exists (not all users have manager and red errors are annoying)
  99.     if ($CSVman) { Remove-Variable CSVman }
  100.     if ($CSVmanID) { Remove-Variable CSVmanID }
  101.     if ($Action) { Remove-Variable Action }
  102.     if ($DupSAM) { Remove-Variable DupSAM }
  103.     if ($DupUPN) { Remove-Variable DupUPN }
  104.     if ($NewSAM) { Remove-Variable NewSAM }
  105.     if ($NewUPN) { Remove-Variable NewUPN }
  106.     if ($DupDN) { Remove-Variable DupDN }
  107.     if ($NewDN) { Remove-Variable NewDisplayName }
  108.     if ($FriendlyDN) { Remove-Variable FriendlyDN }
  109.     if ($Current) { Remove-Variable Current }
  110.     if ($DupEmail) { Remove-Variable DupEmail }
  111.  
  112.     ############################################################################################################
  113.  
  114.  
  115.     #######SETS BASIC Variables#################
  116.     $fname = $User.("First Name")
  117.     $lname = $User.("Last Name")
  118.     $EID = $User.("Empl ID")
  119.     $Action = $User.("Action")
  120.  
  121.     ###########################################  
  122.  
  123.  
  124.  
  125.  
  126.     $Current = Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(EmployeeID -eq $EID)' ####  Searches AD for EMployeeID match #######
  127.     $SamName = $Current.samAccountName
  128.  
  129.  
  130.  
  131.     if ($Current) { #If AD match is found
  132.         ###If 2 AD results are matched Enumerate AD duplicates into body########
  133.         if ($Current.Count -ge 2) {
  134.             #Duplicate Found in AD
  135.             foreach ($ADdup in $Current) {
  136.                 $AllADdup = Get-ADUser -Identity $ADdup.samAccountName -PR *
  137.                 $ADDupsBody += "THE FOLLOWING DUPLICATE HAS BEEN FOUND IN ACTIVE Directory PLEASE CHECK THIS USER MANUALLY " + $ADDup.givenname + " " + $ADdup.surname + ",  with Homepage ID " + $AllADDup.HomePage + " and EmployeeID " + $AllADdup.EmployeeID + "`r`n"
  138.             }
  139.         }
  140.  
  141.         ##################################################################
  142.  
  143.  
  144.         elseif ($Current.Enabled -eq "True") { #Single Match with Enabled ADAccount is found.
  145.  
  146.  
  147.             $AllAtrrib = Get-ADUser -Identity $SamName -PR *
  148.             if ($AllAtrrib.Manager) { $ADManID = (Get-ADUser $AllAtrrib.Manager -PR "EmployeeID").EmployeeID } #Retreive Managers Employee ID if manager field is not NULL
  149.  
  150.             # $ADManID
  151.             if ($mobile) { Remove-Variable mobile }
  152.             $mobile = $AllAtrrib | Select-Object mobilephone
  153.  
  154.             $ADMatch = New-Object PSObject -Property @{
  155.                 "Last Name" = $AllAtrrib.surname
  156.                 "First Name" = $AllAtrrib.givenname
  157.                 "Company" = $AllAtrrib.Company
  158.                 "Department" = $AllAtrrib.Department
  159.                 "Title" = $AllAtrrib.Title
  160.                 "EmployeeID" = $AllAtrrib.EmployeeID
  161.                 "WebsiteID" = $AllAtrrib.HomePage
  162.                 "HireDate" = $AllAtrrib.HireDate
  163.                 "JobEntryDate" = $AllAtrrib.JobEntryDate
  164.                 "ManagerID" = $ADManID
  165.                 "Birthdate" = $AllAtrrib.Birthdate
  166.                 "MobilePhone" = $Mobile.MobilePhone
  167.                 "Location" = $AllAtrrib.Office
  168.                 "personalEmail" = $AllAtrrib.personalEmail
  169.                 "PSIndex" = $i
  170.             }
  171.  
  172.  
  173.             $ADuserMatch += $ADMatch
  174.  
  175.  
  176.             $CSVMatch = New-Object PSObject -Property @{
  177.                 "Last Name" = $User.("Last Name")
  178.                 "First Name" = $User.("First Name")
  179.                 "Company" = $User.("Company")
  180.                 "Department" = $User.("Department")
  181.                 "Title" = $User.("Job Title ")
  182.                 "EmployeeID" = $User.("Empl ID")
  183.                 "WebsiteID" = $User.("Empl ID")
  184.                 "HireDate" = $User.("Hire Date")
  185.                 "JobEntryDate" = $User.("Job Entry")
  186.                 "ManagerID" = $User.("Reports To Manager ID ")
  187.                 "Birthdate" = $User.("Birthdate")
  188.                 "MobilePhone" = $User.("Phone Number")
  189.                 "Location" = $User.("Location ")
  190.                 "personalEmail" = $User.("Electronic Mail Address ")
  191.                 "PSIndex" = $i
  192.             }
  193.  
  194.             $CSVuserMatch += $CSVMatch
  195.  
  196.             $CompanyMisMatch = Compare-Object $ADMatch $CSVMatch -Property Company | Where-Object { $_.SideIndicator –eq "=>" }
  197.             $DeptMisMatch = Compare-Object $ADMatch $CSVMatch -Property Department | Where-Object { $_.SideIndicator –eq "=>" }
  198.             $TitleMisMatch = Compare-Object $ADMatch $CSVMatch -Property Title | Where-Object { $_.SideIndicator –eq "=>" }
  199.             $JobDateMisMatch = Compare-Object $ADMatch $CSVMatch -Property JobEntryDate | Where-Object { $_.SideIndicator –eq "=>" }
  200.             $HireDateMisMatch = Compare-Object $ADMatch $CSVMatch -Property HireDate | Where-Object { $_.SideIndicator –eq "=>" }
  201.             $IDMisMatch = Compare-Object $ADMatch $CSVMatch -Property EmployeeID | Where-Object { $_.SideIndicator –eq "=>" }
  202.             $WebIDMisMatch = Compare-Object $ADMatch $CSVMatch -Property WebSiteID | Where-Object { $_.SideIndicator –eq "=>" }
  203.             $ManIDMisMatch = Compare-Object $ADMatch $CSVMatch -Property ManagerID | Where-Object { $_.SideIndicator –eq "=>" }
  204.             $BirthMisMatch = Compare-Object $ADMatch $CSVMatch -Property Birthdate | Where-Object { $_.SideIndicator –eq "=>" }
  205.             $MobilePhoneMisMatch = Compare-Object $ADMatch $CSVMatch -Property MobilePhone | Where-Object { $_.SideIndicator –eq "=>" }
  206.             $LocationMisMatch = Compare-Object $ADMatch $CSVMatch -Property Location | Where-Object { $_.SideIndicator –eq "=>" }
  207.             $EmailMisMatch = Compare-Object $ADMatch $CSVMatch -Property personalEmail | Where-Object { $_.SideIndicator –eq "=>" }
  208.  
  209.             ##################Gets Dates for comparison##############################
  210.             $today = Get-Date
  211.             $JBEntryDate = Get-Date $User.("Job Entry")
  212.             $Hiredate = Get-Date $User.("Hire Date")
  213.             $HiredateDifference = New-TimeSpan $hiredate $today
  214.             #########################################################################
  215.  
  216.  
  217.             #$today -ge $JBEntrydate
  218.  
  219.             if (($User.("Employee Status") -ne "D") -and ($User.("Employee Status") -ne "R") -and ($User.("Employee Status") -ne "T")) {
  220.                 ##CREATE EMAIL BODY##
  221.                 if ($TitleMisMatch.SideIndicator -eq "=>") { #Changes Jobtitle is JobEntryDate is past.
  222.                     if ($today -ge $JBEntrydate) {
  223.                         $AdModifications += $User.("First Name") + " " + $User.("Last Name") + "'s Job Title is being changed to " + $User.("Job Title ") + "`r`n"
  224.                         Set-ADUser -Identity $SamName -Title $User.("Job Title ")
  225.                         $ADChangeMade = 1
  226.                     }
  227.                 }
  228.  
  229.                 if ($DeptMisMatch.SideIndicator -eq "=>") { #Changes Department is JobEntryDate is past.
  230.                     if ($today -ge $JBEntrydate) {
  231.                         $AdModifications += $User.("First Name") + " " + $User.("Last Name") + "'s Department is being changed to " + $User.("Department") + "`r`n"
  232.                         Set-ADUser -Identity $SamName -Department $User.("Department")
  233.                         $ADChangeMade = 1
  234.                     }
  235.                 }
  236.  
  237.                 if ($CompanyMisMatch.SideIndicator -eq "=>") { #Changes Company is JobEntryDate is past.
  238.                     if ($today -ge $JBEntrydate) {
  239.                         $AdModifications += $User.("First Name") + " " + $User.("Last Name") + "'s Company is being changed to " + $User.("Company") + "`r`n"
  240.                         Set-ADUser -Identity $SamName -Company $User.("Company")
  241.                         $ADChangeMade = 1
  242.                     }
  243.                 }
  244.                 if ($LocationMisMatch.SideIndicator -eq "=>") { #sets office locations
  245.                     $AdModifications += $User.("First Name") + " " + $User.("Last Name") + "'s Office Location is being changed to " + $User.("Location ") + " `r`n"
  246.                     Set-ADUser -Identity $SamName -Office $User.("Location ")
  247.                     $ADChangeMade = 1
  248.                 }
  249.                 if ($IDMisMatch.SideIndicator -eq "=>") { #SEts EmployeeID  Probably never runs
  250.                     $AdModifications += $User.("First Name") + " " + $User.("Last Name") + "'s EmployeeID is being changed to " + $User.("Empl ID") + "`r`n"
  251.                     Set-ADUser -Identity $SamName -EmployeeID $User.("Empl ID")
  252.                     $ADChangeMade = 1
  253.                 }
  254.                 if ($BirthMisMatch.SideIndicator -eq "=>") { #checks birthday
  255.                     $AdModifications += $User.("First Name") + " " + $User.("Last Name") + "'s Birthday is being changed to " + $User.("Birthdate") + "`r`n"
  256.                     Set-ADUser -Identity $SamName -Replace @{ Birthdate = $User.("Birthdate") }
  257.                     $ADChangeMade = 1
  258.                 }
  259.                 if ($WEBIDMisMatch.SideIndicator -eq "=>") { #sets Webiste ID
  260.                     $AdModifications += $User.("First Name") + " " + $User.("Last Name") + "'s WebsiteID is being changed to " + $User.("Empl ID") + "`r`n"
  261.                     Set-ADUser -Identity $SamName -HomePage $User.("Empl ID")
  262.                     $ADChangeMade = 1
  263.                 }
  264.                 if ($EmailMisMatch.SideIndicator -eq "=>") { #sets personal email
  265.                     if ($User.("Electronic Mail Address ") -and ($User."Electronic Mail Address " -ne " ")) {
  266.                         $AdModifications += $User.("First Name") + " " + $User.("Last Name") + "'s Personal Email is being changed to " + $User.("Electronic Mail Address ") + "`r`n"
  267.                         try { Set-ADUser -Identity $SamName -Replace @{ personalEmail = $User.("Electronic Mail Address ") } } catch {}
  268.                         $ADChangeMade = 1 }
  269.                 }
  270.                 if ($HireDateMisMatch.SideIndicator -eq "=>") {
  271.                     $AdModifications += $User.("First Name") + " " + $User.("Last Name") + "'s Hire Date is being changed to " + $User.("Hire Date") + "`r`n"
  272.                     Set-ADUser -Identity $SamName -Replace @{ HireDate = $User.("Hire Date") }
  273.                     $ADChangeMade = 1
  274.                 }
  275.                 if ($JobDateMisMatch.SideIndicator -eq "=>") {
  276.                     $AdModifications += $User.("First Name") + " " + $User.("Last Name") + "'s Job Entry Date is being changed to " + $User.("Job Entry") + "`r`n"
  277.                     Set-ADUser -Identity $SamName -Replace @{ JobEntryDate = $User.("Job Entry") }
  278.                     $ADChangeMade = 1
  279.                 }
  280.                 if ($MobilePhoneMisMatch.SideIndicator -eq "=>") {
  281.                     if (($User."Phone Number") -and ($User."Phone Number" -ne " ")) {
  282.                         $AdModifications += $User.("First Name") + " " + $User.("Last Name") + "'s Mobile Phone is being changed to " + $User.("Phone Number") + "`r`n"
  283.                         Set-ADUser -Identity $SamName -MobilePhone $User.("Phone Number") }
  284.                     $ADChangeMade = 1
  285.                 }
  286.  
  287.                 if ($ManIDMisMatch.SideIndicator -eq "=>") {
  288.                     if (($User.("Reports To Manager ID ")) -and ($User.("Reports To Manager ID ")) -ne " ") {
  289.                         $Body += $User.("First Name") + " " + $User.("Last Name") + "'s Manager ID " + $User.("Reports To Manager ID ") + " Does not Match AD `r`n"
  290.                         $CSVMANID = $User.("Reports To Manager ID ")
  291.                         $CSVMan = Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(EmployeeID -eq $CSVMANID)'
  292.                         if ($csvman) {
  293.                             Set-ADUser -Identity $SamName -Manager $CSVMan
  294.                             $ADChangeMade = 1
  295.                         }
  296.                         if ($csvman) { $AdModifications += "Manager is being set to " + $CSVMan + "`r`n" }
  297.                         else {
  298.                             $Body += "Manager ID from CSV could not be found in AD" + "`r`n"
  299.                             $ManError = 1
  300.                             $Body += "`r`n"
  301.                         }
  302.                     }
  303.  
  304.  
  305.                 }
  306.                 $AdModifications += "`r`n"
  307.             }
  308.  
  309.             elseif ($AllAtrrib.Enabled -eq "True") { $ShouldDisable += $User.("First Name") + " " + $User.("Last Name") + " with EmployeeID " + $User.("Empl ID") + " Should be Disabled! `r`n" }
  310.  
  311.  
  312.         }
  313.  
  314.  
  315.     } else { #If their is NOT a AD match
  316.  
  317.  
  318.  
  319.         #######################################If EmployeeID does not match Does not Exist################################################################
  320.         if (($User.("Employee Status") -ne "D") -and ($User.("Employee Status") -ne "R") -and ($User.("Employee Status") -ne "T")) {
  321.             $fname = $User.("First Name") -replace '[-]',"" -replace '[.]',"" -replace '[,]',""
  322.             $lname = $User.("Last Name") -replace '[-]',"" -replace '[.]',"" -replace '[,]',""
  323.             $mname = $user.("Middle Name").Substring(0,1) -replace '[-]',"" -replace '[.]',"" -replace '[,]',""
  324.             #################################Random Password Generator########################################
  325.             $assembly = Add-Type -AssemblyName System.Web
  326.             $password = [System.Web.Security.Membership]::GeneratePassword(10,2)
  327.             ###################################################################################################
  328.  
  329.             ############################################Set Variables for New User#########################################
  330.  
  331.             $CSVMANID = $User.("Reports To Manager ID ")
  332.             $CSVMan = Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(EmployeeID -eq $CSVMANID)'
  333.             $NewSam = $fname.ToLower() + "." + $lname.ToLower()
  334.             if ($NewSAM.length -gt 19) { $NewSAM = $NewSAM.Substring(0,19) }
  335.             $NewDisplayName = ($User.("First Name") + " " + $User.("Last Name")) -replace '[-]'," " -replace '[.]'," "
  336.             $FriendlyDN = ($User.("First Name") + " " + $User.("Last Name")) -replace '[-]'," " -replace '[.]'," "
  337.             $NewUPN = ($NewSam + "@" + $DomainController).Trim()
  338.             $setpass = ConvertTo-SecureString -AsPlainText ("$password") -Force
  339.             $newSam = $NewSAM -replace '\s',''
  340.             ########################################################################################################################
  341.  
  342.  
  343.             #############################################Check for Dup UPN or SAM   #################
  344.             $DupSAM = Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(SamAccountName -eq $NewSam)'
  345.             $DupEmail = Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(EmailAddress -eq $NewUPN)'
  346.             #################################################################################################################
  347.             if (($DupSAM -or $DupEmail)) {
  348.                 $NewSam = $fname.ToLower() + $mname.ToLower() + "." + $lname.ToLower()
  349.                 if ($NewSAM.length -gt 19) { $NewSAM = $NewSAM.Substring(0,19) }
  350.                 $newSam = $NewSAM -replace '\s',''
  351.                 $NewUPN = ($NewSam + "@" + $DomainController).Trim()
  352.             }
  353.  
  354.             $DupSAM = Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(SamAccountName -eq $NewSam)'
  355.             $DupEmail = Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(EmailAddress -eq $NewUPN)'
  356.  
  357.             while (($DupSAM -or $DupEmail)) {
  358.                 $DupCount++
  359.                 $NewSam + " Is a DUP"
  360.                 $NewSAM = $fname.ToLower() + "." + $lname.ToLower() + $DupCount
  361.                 if ($NewSAM.length -gt 19) { $NewSAM = $NewSAM.Substring(0,19) }
  362.                 $newSam = $NewSAM -replace '\s',''
  363.                 $DupSAM = Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(SamAccountName -eq $NewSam)'
  364.                 $NewUPN
  365.                 $NewUPN = ($NewSam + "@" + $DomainController).Trim()
  366.                 $DupEmail = Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(EmailAddress -eq $NewUPN)'
  367.  
  368.  
  369.             }
  370.  
  371.  
  372.             $newSam = $NewSAM -replace '\s',''
  373.             $NewUPN = ($NewSam + "@" + $DomainController).Trim()
  374.             $DupCount = 1
  375.             ##############################################################################################################
  376.  
  377.  
  378.             ############################################Checks for duplicate CN########################################################
  379.             $DupDN = Get-ADUser -SearchBase $userpath -Server $DomainController -Filter '(CN -eq $NewDisplayName)'
  380.             if ($DupDN) { $NewDisplayName = ($User.("First Name") + " " + $user.("Middle Name") + " " + $User.("Last Name")) -replace '[-]'," " -replace '[.]'," " }
  381.             $DupDN = Get-ADUser -SearchBase $userpath -Server $DomainController -Filter '(CN -eq $NewDisplayName)'
  382.  
  383.  
  384.             while (($DupDN) -or ($DupCount > 20)) {
  385.                 $NewDisplayName = ($User.("First Name") + " " + $User.("Last Name") + " " + $DupCount) -replace '[-]'," " -replace '[.]'," "
  386.                 $DupCount++
  387.                 $DupDN = Get-ADUser -SearchBase $userpath -Server $DomainController -Filter '(CN -eq $NewDisplayName)'
  388.             }
  389.             #########################################################################################################################
  390.  
  391.  
  392.             #########################################Creating AD account##################################################  
  393.  
  394.  
  395.             if ($HiredateDifference.Days -lt 8) {
  396.                     $ADCreated = 1
  397.                 try { New-ADUser $NewDisplayName -SamAccountName $NewSam -DisplayName $FriendlyDN -GivenName $User.("First Name") -Surname $User.("Last Name") `
  398.                         -UserPrincipalName $NewUPN -Company ($User.("Company")) -Department $User.("Department") `
  399.                         -Title $User.("Job Title ") -EmployeeID $User.("Empl ID") -MobilePhone $User.("Phone Number") -HomePage $User.("Empl ID") `
  400.                         -Manager $CSVMan -Office $User.("Location ") -AccountPassword $setpass -Enabled $True -Path $UserPath `
  401.                         -OtherAttributes @{ 'HireDate' = $User.("Hire Date"); 'JobEntryDate' = $User.("Job Entry"); 'birthdate' = $User.("Birthdate") } }
  402.  
  403.                 catch {
  404.                     #"NO AD FOR YOU"
  405.                     $_
  406.                     $ADerror += $fname + " " + $lname + " generated an error when trying to create an AD account "
  407.                     $Failed = 1
  408. }
  409.  
  410.  
  411.                 try { Set-ADUser -Identity $newsam -Replace @{ personalEmail = $User.("Electronic Mail Address ") } } catch {}
  412.                 #$NewDisplayName + "is not valid with samName" +$NewSAM + "     user ID " + $User.("Empl ID")
  413.  
  414.  
  415.  
  416.                 ################################################################################################################################
  417.  
  418.                 ##############################################Send Email to manager################################
  419.                 $NewEmployee = "Hello " + $CSVman.givenname + " " + $CSVman.surname + "`r`n`r`n" + `
  420.                     "A new Employee " + $User.("First Name") + " " + $User.("Last Name") + ", with the EmployeeID " + $User.("Empl ID") + " has been Created" + "`r`n`r`n" + `
  421.                     "The  username is " + $NewSAM + " with a password of " + $password + "`r`n`r`n" + `
  422.                     "Click here for permissions and fill out the following form" + "`r`n`r`n" + `
  423.                     "Click here if you feel this email is created in error"
  424.  
  425.                 $NoMatch += "An AD Account is being created for " + $User.("First Name") + " " + $User.("Last Name") + " with the EmployeeID " + $User.("Empl ID")
  426.                 $NoMatch += "`r`n"
  427.  
  428.                 if ($failed = 0) {
  429.                     Send-MailMessage -From $From -To $To -Subject $Subject `
  430.                         -Body $NewEmployee -SmtpServer $SMTPServer -Port $SMTPPort `
  431.                         #-Credential (Get-Credential) #-Attachments $Attachment
  432.                 }
  433.  
  434.             }
  435.  
  436.             ################################################################################################################
  437.         }
  438.     }
  439. }
  440.  
  441. $ADuserMatch | Export-Csv C:\csv\ADMatches.csv -NoTypeInformation
  442. $CSVuserMatch | Export-Csv C:\csv\CSVMatches.csv -NoTypeInformation
  443.  
  444. #$NoMatch | Export-Csv C:\csv\NoMatches.csv -NoTypeInformation
  445.  
  446. #$MisMatch |
  447. #Select-Object "Last Name","First Name","ADCompany","CSVCompany","ADDepartment","CSVDepartment","ADTitle","CSVTitle" |
  448. #Export-Csv C:\csv\MixMatches.csv -NoTypeInformation
  449.  
  450.  
  451. $body += $CSVDUPmsg
  452. $body += "`r`n"
  453.  
  454.  
  455. $body += $ADDupsBody
  456. $body += $PhoneMisMatch
  457. $Body += "`r`n"
  458. $Body += $ShouldDisable
  459.  
  460. #$body += $NoMatch
  461.  
  462.  
  463. $SaveTime = (Get-Date).ToString("yyyy-MM-dd__hh-mm")
  464.  
  465. if($ManError -eq 1){
  466. $SavePath = "C:\Scripts\Logs\ADMODErrors_" + $SaveTime + ".txt"
  467. $body | Out-File $SavePath
  468. }
  469. if($failed -eq 0){
  470. $SavePath = "C:\Scripts\Logs\ADAccounts_Created_" + $SaveTime + ".txt"
  471. $NoMatch | Out-File $SavePath
  472. }
  473.  
  474. If($ADChangeMade -eq 1){
  475. $SavePath = "C:\Scripts\Logs\ADchangesMade_" + $SaveTime + ".txt"
  476. $AdModifications | Out-File $SavePath
  477. }
  478.  
  479. if ($ADcreated -eq 1) {
  480.     $SavePath = "C:\Scripts\Logs\ADCreation_error_" + $SaveTime + ".txt"
  481.     $ADerror | Out-File $SavePath
  482. }
Add Comment
Please, Sign In to add comment