BooDaa63

Untitled

Aug 3rd, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Set Variables
  2. $inv = (Get-Variable MyInvocation -Scope 0).Value;
  3. $wrkdir = Split-Path $inv.MyCommand.Path;
  4. $rootPath = (Split-Path $wrkdir);
  5. $logs = "$wrkdir\logs"
  6. $inputpath = "$wrkdir\inputs"
  7. $date = get-date -Format "yyyyMMdd"
  8. $inputfile = $date + "_NewHireRehire.txt"
  9. $FetchPath = "\\SERVERNAME\d$\apps\newhirerehire"
  10. $Inputtesetpath = "$FetchPath\$inputfile"
  11. $OutputPath = "$wrkdir\output"
  12. $ArchivePath = "$wrkdir\Archive"
  13. $Hostname = $env:COMPUTERNAME
  14. # Load up our common functions.  
  15. # It should be like this '..\Utils\utils.ps1' from the script's directory.
  16. $imp = $rootPath + "\Utils\utils.ps1";
  17. . $imp;
  18.  
  19. logmsgmfc $logs\$date.log "Starting Script"
  20.  
  21. logmsgmfc $logs\$date.log "Importing Active Directory Cmdlets"
  22. Import-module Activedirectory
  23. $OU = Get-ADOrganizationalUnit -Filter {name -like "users - holding"}
  24.  
  25.  
  26. Function Create-Password {
  27.  
  28. $lowercase = @("a","b","c","d","e","f","g","h","i","j","k","m","n","p","q","r","s","t","u","v","w","x","y","z")
  29. $Uppercase = @("A","B","C","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z")
  30. $numeric = @("0","1","2","3","4","5","6","7","8","9")
  31. $special = @("!","#","%","&")
  32.  
  33. $pwd = @()
  34. $pwd1 = @()
  35. $pwd2 = @()
  36. $pwd3 = @()
  37. $pwd4 = @()
  38.  
  39.  
  40. For($loop=1;$loop -le 2;$loop++) { $pwd1+=(get-random -inputobject $lowercase) }
  41. For($loop=1;$loop -le 2;$loop++) { $pwd2+=(get-random -inputobject $uppercase) }
  42. For($loop=1;$loop -le 2;$loop++) { $pwd3+=(get-random -inputobject $numeric) }
  43. For($loop=1;$loop -le 2;$loop++) { $pwd4+=(get-random -inputobject $special) }
  44.  
  45.  
  46. $pwd = ($pwd1 + $pwd2 + $pwd3 + $pwd4)
  47.  
  48. $pwd = ($pwd | Sort-Object {get-random})
  49.  
  50. $pwdfinal = ([string]::Join("",$pwd))
  51.  
  52. $pwdfinal }
  53.  
  54.  
  55. Function Create-UserName {
  56.  
  57. param ($fname, $mname, $lname)
  58.  
  59.      
  60.     $fname = $fname.tochararray()
  61.     $mname = $mname.tochararray()
  62.     $lname = $lname.tochararray()
  63.        
  64.     $Tempusername = $fname[0] + $mname[0] + $lname[0] + $lname[1] + $lname[2] + $lname[3] + $lname[4] + $lname[5]
  65.     $Tempusername2 = $fname[0] + $fname[1] + $mname[0] + $lname[0] + $lname[1] + $lname[2] + $lname[3] + $lname[4]
  66.     $Tempusername3 = $fname[0] + $fname[1] + $fname[2] + $mname[0] + $lname[0] + $lname[1] + $lname[2] + $lname[3]
  67.    
  68.     $CheckUser1 = Get-Aduser -Filter {Samaccountname -eq $Tempusername}
  69.     $CheckUser2 = Get-Aduser -Filter {Samaccountname -eq $Tempusername2}
  70.     $CheckUser3 = Get-Aduser -Filter {Samaccountname -eq $Tempusername3}
  71.     If ($CheckUser1 -eq $null) {$finalUserName = $Tempusername.tolower()}
  72.          elseif ($checkuser2 -eq $null) {$finalUserName = $Tempusername2.tolower()}
  73.          elseif ($checkuser3 -eq $null) {$finalUserName = $Tempusername3.tolower()}
  74.  
  75.     Else {  logmsgmfc $logs\$date.log "No Available Usernames for $fname $mname $lname"
  76.             logmsgmfc $logs\$date.log "Sending Alert Email for Username Creation Fail"
  77.             SendEmail '[email protected]' '[email protected]' "Username Creation Failed" " Could not create Username for $FirstName $MiddleInt $LastName.  Please check log $date.log"}
  78.  Return $finalUserName
  79.      
  80.      }
  81.  
  82. Function Set-GroupMembership {
  83.  
  84. Param ($TargetUserName, $title, $DeptNumber)
  85.  
  86. $DeptNumFilter = $DeptNumber + "*"
  87.  
  88. $TargetUser = get-aduser -Filter {samaccountname -eq $TargetUserName}
  89.  
  90. $SourceUser = get-aduser -filter {(department -like $deptNumFilter) -and (title -like $title)} -properties memberof |select-object -first 1
  91.  
  92. if ($SourceUser -eq $null) {
  93.                             $SourceUser = get-aduser -filter {samaccountname -like "ggroups" } -properties memberof
  94.                             }
  95.  
  96. foreach ($group in $SourceUser.memberof)
  97.      {
  98.        Try
  99.             { add-adgroupmember $group -Member $TargetUser
  100.               logmsgmfc $logs\$date.log "Adding Group $Group Successful" }
  101.         Catch { logmsgmfc $logs\$date.log "Adding Group $Group Failed"
  102.                 logmsgmfc $logs\$date.log $_.exception.message }
  103.       }    
  104.  
  105. }
  106.  
  107.  
  108. #fetch input file from SERVERNAME and place in Input folder
  109. logmsgmfc $logs\$date.log "Copy Input File from SERVERNAME to Input Folder"
  110. IF (Test-Path $FetchPath\$Inputfile) {
  111. Move-Item -path $FetchPath\$inputfile -destination $inputpath
  112. logmsgmfc $logs\$date.log "Input File from SERVERNAME Copied to Input Folder Successfully" }
  113. Else { logmsgmfc $logs\$date.log "Cannont Copy Input File from SERVERNAME to Input Folder, No File Found"; EXIT}
  114.  
  115.  
  116. logmsgmfc $logs\$date.log "Importing File $inputfile"
  117. Try {
  118.         $Inputs = import-csv -Delimiter "`t" -Path $inputpath\$inputfile
  119.         logmsgmfc $logs\$date.log "$inputfile Imported Successfully"
  120.     }
  121. Catch {
  122.         logmsgmfc $logs\$date.log "Importing File $InputFile Failed"
  123.         logmsgmfc $logs\$date.log $_.Exception.Message
  124.         EXIT
  125.       }
  126.  
  127. logmsgmfc $logs\$date.log "Begining User Creation Process"
  128.  
  129.  
  130. ForEach ($Row in $Inputs) {
  131.  
  132. $FirstName = $Row.FirstName
  133. $MiddleInt = $Row.MiddleName
  134. $LastName = $Row.LastName
  135. $DeptNum = $Row.DepartmentNumber
  136. $DeptTitle = $Row.DepartmentTitle
  137. $Jobtitle = $row.Position
  138. $EmpNumber = $row.EmployeeNumber
  139. $Rehire = get-aduser -filter {description -like $EmpNumber}
  140.  
  141. IF ($Rehire -eq $Null) {
  142. logmsgmfc $logs\$date.log "Creating Password"
  143. $password = Create-Password
  144.  
  145. logmsgmfc $logs\$date.log "Creating Username for $FirstName $MiddleInt $LastName"
  146. $Username = Create-UserName $FirstName $MiddleInt $LastName
  147.  
  148. IF ($Username -ne $null) {
  149.  
  150. Try {
  151.  
  152.         logmsgmfc $logs\$date.log "Trying to Create User $username"
  153.         New-aduser $Username -GivenName $FirstName -Initials $MiddleInt -Surname $LastName -DisplayName "$firstname $middleint $lastname" -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) -userprincipalname ($Username + "@bmh.bmnet.com") -Enabled $true -Description $EmpNumber -Department $Deptnum -ScriptPath "XXX.bat" -Path $OU.distinguishedname
  154.         logmsgmfc $logs\$date.log "User $username Created Successfully"
  155.     }
  156. Catch {
  157.         logmsgmfc $logs\$date.log "Creating User Failed"
  158.         logmsgmfc $logs\$date.log $_.Exception.Message
  159.         SendEmail '[email protected]' '[email protected]' "User Account Creation Failed" "Please check log $date.log"
  160.         }
  161.  
  162. logmsgmfc $logs\$date.log "Adding User to Proper Groups"
  163. Set-GroupMembership $Username $Jobtitle $DeptNum
  164.  
  165. Try {
  166.     logmsgmfc $logs\$date.log "Adding User Info to Results File"
  167.     Add-Content -Path "$OutputPath\NewUsers_$date.csv" -Value "$firstname $MiddleInt $LastName,$UserName,$Password"
  168.     }
  169. Catch {logmsgmfc $logs\$date.log "No Output to Add to Results File"}
  170.  
  171.  
  172. }
  173. Else {logmsgmfc $logs\$date.log "Creating User Failed Because of Lack of Available Usernames"}
  174. }
  175. Else {logmsgmfc $logs\$date.log "$firstname $middleint $lastname Appears to be a Rehire. User Account Already Exists"
  176.       logmsgmfc $logs\$date.log "Attempting to Reenable Existing Account"
  177.       Try {$Rehire | Enable-Adaccount
  178.            logmsgmfc $logs\$date.log "Account Enabled"
  179.            logmsgmfc $logs\$date.log "Moving to Users - Holding OU"
  180.            $rehire | Move-adobject -TargetPath $OU
  181.            logmsgmfc $logs\$date.log "Adding User Info to Results File"
  182.     Add-Content -Path "$OutputPath\NewUsers_$date.csv" -Value "$firstname $MiddleInt $LastName,$Rehire.samaccountname,Rehire" }
  183.       Catch {logmsgmfc $logs\$date.log "Enabling Existing Account Failed"
  184.               logmsgmfc $logs\$date.log $_.exception.message}
  185.             }
  186. } #END ForEach
  187.  
  188. #Email Results to Helpdesk
  189. logmsgmfc $logs\$date.log "Sending Results File to HelpDesk"
  190. SendEmailAttachment '[email protected]' '[email protected]' "User Creation Results $date" "This file contains the usernames and passwords for the newly created users" "$OutputPath\NewUsers_$date.csv"
  191.  
  192.  
  193. #Archive Files
  194. logmsgmfc $logs\$date.log "Move Input Files to Archive Folder"
  195. Move-Item -path $inputpath\$Inputfile -Destination $ArchivePath
  196. logmsgmfc $logs\$date.log "Move Results File to Archive Folder"
  197. Move-Item -Path "$OutputPath\NewUsers_$date.csv" -Destination $ArchivePath
  198. #Purge Old Files from Archive
  199. purgeFiles -path $Archivepath -age 30 -include "*.*"
  200.  
  201. logmsgmfc $logs\$date.log "Script Complete"
Advertisement
Add Comment
Please, Sign In to add comment