109 lines
3.8 KiB
PowerShell
109 lines
3.8 KiB
PowerShell
|
# 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
|
||
|
}
|
||
|
|