Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Set Variables
- $inv = (Get-Variable MyInvocation -Scope 0).Value;
- $wrkdir = Split-Path $inv.MyCommand.Path;
- $rootPath = (Split-Path $wrkdir);
- $logs = "$wrkdir\logs"
- $inputpath = "$wrkdir\inputs"
- $date = get-date -Format "yyyyMMdd"
- $inputfile = $date + "_NewHireRehire.txt"
- $FetchPath = "\\SERVERNAME\d$\apps\newhirerehire"
- $Inputtesetpath = "$FetchPath\$inputfile"
- $OutputPath = "$wrkdir\output"
- $ArchivePath = "$wrkdir\Archive"
- $Hostname = $env:COMPUTERNAME
- # Load up our common functions.
- # It should be like this '..\Utils\utils.ps1' from the script's directory.
- $imp = $rootPath + "\Utils\utils.ps1";
- . $imp;
- logmsgmfc $logs\$date.log "Starting Script"
- logmsgmfc $logs\$date.log "Importing Active Directory Cmdlets"
- Import-module Activedirectory
- $OU = Get-ADOrganizationalUnit -Filter {name -like "users - holding"}
- Function Create-Password {
- $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")
- $Uppercase = @("A","B","C","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z")
- $numeric = @("0","1","2","3","4","5","6","7","8","9")
- $special = @("!","#","%","&")
- $pwd = @()
- $pwd1 = @()
- $pwd2 = @()
- $pwd3 = @()
- $pwd4 = @()
- For($loop=1;$loop -le 2;$loop++) { $pwd1+=(get-random -inputobject $lowercase) }
- For($loop=1;$loop -le 2;$loop++) { $pwd2+=(get-random -inputobject $uppercase) }
- For($loop=1;$loop -le 2;$loop++) { $pwd3+=(get-random -inputobject $numeric) }
- For($loop=1;$loop -le 2;$loop++) { $pwd4+=(get-random -inputobject $special) }
- $pwd = ($pwd1 + $pwd2 + $pwd3 + $pwd4)
- $pwd = ($pwd | Sort-Object {get-random})
- $pwdfinal = ([string]::Join("",$pwd))
- $pwdfinal }
- Function Create-UserName {
- param ($fname, $mname, $lname)
- $fname = $fname.tochararray()
- $mname = $mname.tochararray()
- $lname = $lname.tochararray()
- $Tempusername = $fname[0] + $mname[0] + $lname[0] + $lname[1] + $lname[2] + $lname[3] + $lname[4] + $lname[5]
- $Tempusername2 = $fname[0] + $fname[1] + $mname[0] + $lname[0] + $lname[1] + $lname[2] + $lname[3] + $lname[4]
- $Tempusername3 = $fname[0] + $fname[1] + $fname[2] + $mname[0] + $lname[0] + $lname[1] + $lname[2] + $lname[3]
- $CheckUser1 = Get-Aduser -Filter {Samaccountname -eq $Tempusername}
- $CheckUser2 = Get-Aduser -Filter {Samaccountname -eq $Tempusername2}
- $CheckUser3 = Get-Aduser -Filter {Samaccountname -eq $Tempusername3}
- If ($CheckUser1 -eq $null) {$finalUserName = $Tempusername.tolower()}
- elseif ($checkuser2 -eq $null) {$finalUserName = $Tempusername2.tolower()}
- elseif ($checkuser3 -eq $null) {$finalUserName = $Tempusername3.tolower()}
- Else { logmsgmfc $logs\$date.log "No Available Usernames for $fname $mname $lname"
- logmsgmfc $logs\$date.log "Sending Alert Email for Username Creation Fail"
- SendEmail '[email protected]' '[email protected]' "Username Creation Failed" " Could not create Username for $FirstName $MiddleInt $LastName. Please check log $date.log"}
- Return $finalUserName
- }
- Function Set-GroupMembership {
- Param ($TargetUserName, $title, $DeptNumber)
- $DeptNumFilter = $DeptNumber + "*"
- $TargetUser = get-aduser -Filter {samaccountname -eq $TargetUserName}
- $SourceUser = get-aduser -filter {(department -like $deptNumFilter) -and (title -like $title)} -properties memberof |select-object -first 1
- if ($SourceUser -eq $null) {
- $SourceUser = get-aduser -filter {samaccountname -like "ggroups" } -properties memberof
- }
- foreach ($group in $SourceUser.memberof)
- {
- Try
- { add-adgroupmember $group -Member $TargetUser
- logmsgmfc $logs\$date.log "Adding Group $Group Successful" }
- Catch { logmsgmfc $logs\$date.log "Adding Group $Group Failed"
- logmsgmfc $logs\$date.log $_.exception.message }
- }
- }
- #fetch input file from SERVERNAME and place in Input folder
- logmsgmfc $logs\$date.log "Copy Input File from SERVERNAME to Input Folder"
- IF (Test-Path $FetchPath\$Inputfile) {
- Move-Item -path $FetchPath\$inputfile -destination $inputpath
- logmsgmfc $logs\$date.log "Input File from SERVERNAME Copied to Input Folder Successfully" }
- Else { logmsgmfc $logs\$date.log "Cannont Copy Input File from SERVERNAME to Input Folder, No File Found"; EXIT}
- logmsgmfc $logs\$date.log "Importing File $inputfile"
- Try {
- $Inputs = import-csv -Delimiter "`t" -Path $inputpath\$inputfile
- logmsgmfc $logs\$date.log "$inputfile Imported Successfully"
- }
- Catch {
- logmsgmfc $logs\$date.log "Importing File $InputFile Failed"
- logmsgmfc $logs\$date.log $_.Exception.Message
- EXIT
- }
- logmsgmfc $logs\$date.log "Begining User Creation Process"
- ForEach ($Row in $Inputs) {
- $FirstName = $Row.FirstName
- $MiddleInt = $Row.MiddleName
- $LastName = $Row.LastName
- $DeptNum = $Row.DepartmentNumber
- $DeptTitle = $Row.DepartmentTitle
- $Jobtitle = $row.Position
- $EmpNumber = $row.EmployeeNumber
- $Rehire = get-aduser -filter {description -like $EmpNumber}
- IF ($Rehire -eq $Null) {
- logmsgmfc $logs\$date.log "Creating Password"
- $password = Create-Password
- logmsgmfc $logs\$date.log "Creating Username for $FirstName $MiddleInt $LastName"
- $Username = Create-UserName $FirstName $MiddleInt $LastName
- IF ($Username -ne $null) {
- Try {
- logmsgmfc $logs\$date.log "Trying to Create User $username"
- 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
- logmsgmfc $logs\$date.log "User $username Created Successfully"
- }
- Catch {
- logmsgmfc $logs\$date.log "Creating User Failed"
- logmsgmfc $logs\$date.log $_.Exception.Message
- SendEmail '[email protected]' '[email protected]' "User Account Creation Failed" "Please check log $date.log"
- }
- logmsgmfc $logs\$date.log "Adding User to Proper Groups"
- Set-GroupMembership $Username $Jobtitle $DeptNum
- Try {
- logmsgmfc $logs\$date.log "Adding User Info to Results File"
- Add-Content -Path "$OutputPath\NewUsers_$date.csv" -Value "$firstname $MiddleInt $LastName,$UserName,$Password"
- }
- Catch {logmsgmfc $logs\$date.log "No Output to Add to Results File"}
- }
- Else {logmsgmfc $logs\$date.log "Creating User Failed Because of Lack of Available Usernames"}
- }
- Else {logmsgmfc $logs\$date.log "$firstname $middleint $lastname Appears to be a Rehire. User Account Already Exists"
- logmsgmfc $logs\$date.log "Attempting to Reenable Existing Account"
- Try {$Rehire | Enable-Adaccount
- logmsgmfc $logs\$date.log "Account Enabled"
- logmsgmfc $logs\$date.log "Moving to Users - Holding OU"
- $rehire | Move-adobject -TargetPath $OU
- logmsgmfc $logs\$date.log "Adding User Info to Results File"
- Add-Content -Path "$OutputPath\NewUsers_$date.csv" -Value "$firstname $MiddleInt $LastName,$Rehire.samaccountname,Rehire" }
- Catch {logmsgmfc $logs\$date.log "Enabling Existing Account Failed"
- logmsgmfc $logs\$date.log $_.exception.message}
- }
- } #END ForEach
- #Email Results to Helpdesk
- logmsgmfc $logs\$date.log "Sending Results File to HelpDesk"
- 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"
- #Archive Files
- logmsgmfc $logs\$date.log "Move Input Files to Archive Folder"
- Move-Item -path $inputpath\$Inputfile -Destination $ArchivePath
- logmsgmfc $logs\$date.log "Move Results File to Archive Folder"
- Move-Item -Path "$OutputPath\NewUsers_$date.csv" -Destination $ArchivePath
- #Purge Old Files from Archive
- purgeFiles -path $Archivepath -age 30 -include "*.*"
- logmsgmfc $logs\$date.log "Script Complete"
Advertisement
Add Comment
Please, Sign In to add comment