From 260d06f8bf884530ccdce19fb91e5b323946a198 Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 19 Nov 2024 14:50:14 -0700 Subject: [PATCH] new script - not functional yet. --- OnboardingAutomation.ps1 | 108 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 OnboardingAutomation.ps1 diff --git a/OnboardingAutomation.ps1 b/OnboardingAutomation.ps1 new file mode 100644 index 0000000..55b4f84 --- /dev/null +++ b/OnboardingAutomation.ps1 @@ -0,0 +1,108 @@ +# PowerShell script to send an onboarding welcome email with attachments to a user based on their start date + +## Thanks CoPilot + +# SMTP server configuration +$SMTPServer = "mail.mpe.ca" +$SMTPPort = "25" +$Username = "HelpDesk@mpe.ca" + +# Get all users where start date is today, or today + 30 days for Handbook Form. +# extensionAttribute9 = StartDate +# {(extensionAttribute9 -GE (get-date).Date) -and (extensionAttribute9 -GE (get-date).AddDays(1).Date)} + + +$usersToday = get-aduser -filter * -properties extensionAttribute9, manager, mail | where (Convert-NTTime $_.extensionAttribute9) -EQ (get-date).date +# $startDate = [System.DateTime]::FromFileTime($usersToday.extensionAttribute9) + +$users30Day = get-aduser -filter * -properties extensionAttribute9, manager, mail | where extensionAttribute9 -GE (get-date).AddDays(-30).Date | where extensionAttribute9 -GE (get-date).AddDays(-29).Date + + +# function to convert NT Date to Normal. +function Convert-NTTime { + param ( + [Parameter(Mandatory = $true)] + [string]$NTDate + ) + $In_date = [Int64]$NTDate + $returnDate = [System.DateTime]::FromFileTime($In_date).Date +} + +# Function to send email +function Send-WelcomeEmail { + param ( + [Parameter(Mandatory = $true)] + [string]$To, + [Parameter(Mandatory = $true)] + [string]$Subject, + [Parameter(Mandatory = $true)] + [string]$Body, + [Parameter(Mandatory = $true)] + [string[]]$AttachmentPaths + ) + + # Create a new MailMessage object + $MailMessage = New-Object System.Net.Mail.MailMessage + $MailMessage.From = $From + $MailMessage.To.Add($To) + $MailMessage.Subject = $Subject + $MailMessage.Body = $Body + $MailMessage.IsBodyHtml = $false + + # Add attachments to the MailMessage + foreach ($AttachmentPath in $AttachmentPaths) { + if (Test-Path $AttachmentPath) { + $Attachment = New-Object System.Net.Mail.Attachment($AttachmentPath) + $MailMessage.Attachments.Add($Attachment) + } + else { + Write-Host "Attachment not found: $AttachmentPath" + } + } + + # Configure SMTP client + $SMTPClient = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort) + # $SMTPClient.EnableSsl = $true + # $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password) + + # Send the email + try { + $SMTPClient.Send($MailMessage) + Write-Host "Email sent successfully to $To" + } + catch { + Write-Host "Error sending email: $_" + } +} + +## Main Loop + +foreach ($user in $usersToday) { + # Send Day 1 Paperwork + $user_name = $user.name + + # Email configuration + $From = "onboarding@mpe.ca" + $To = $user.mail + $Subject = "Welcome to MPE a division of Englobe!" + $Body = "Good day, $user_name,`n`nWelcome to MPE! Please find attached the necessary documents for your onboarding process. If you have any questions or concers, please reach out to your local Office Admin.`n`nBest Regards,`nYour Onboarding Team" + $AttachmentPaths = @("C:\Users\eeckert\Downloads\Testing\Handbook Receipt and Acknowledgement Form 1.pdf") + + + Send-WelcomeEmail -To $To -Subject $Subject -Body $Body -AttachmentPaths $AttachmentPaths +} +foreach ($user in $users30Day) { + # Send Day 1 Paperwork + $user_name = $user.name + + # Email configuration + $From = "onboarding@mpe.ca" + $To = $user.mail + $Subject = "Welcome to MPE a division of Englobe!" + $Body = "Good day, $user_name,`n`nWelcome to MPE! Please find attached the necessary documents for your onboarding process. If you have any questions or concers, please reach out to your local Office Admin.`n`nBest Regards,`nYour Onboarding Team" + $AttachmentPaths = @("C:\Users\eeckert\Downloads\Testing\Handbook Receipt and Acknowledgement Form 1.pdf") + + + Send-WelcomeEmail -To $To -Subject $Subject -Body $Body -AttachmentPaths $AttachmentPaths +} +