-
Notifications
You must be signed in to change notification settings - Fork 0
/
Send-SHDMessage.ps1
102 lines (94 loc) · 4.19 KB
/
Send-SHDMessage.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
function Send-SHDMessage {
<#
.SYNOPSIS
Sends a decently formated email message to a list of users from a supplied user.
.DESCRIPTION
Sends a decently formated email message to a list of users from a supplied user.
.PARAMETER Users
Mandatory: This is where you can put the multiple usernames of each person you wish to email.
.PARAMETER Message
Mandatory: This is a string that will be used as the message. Please note, you do not need to add a dear or a thank you.
.PARAMETER SenderEmail
Mandatory: This will be the senders email address. When a person replys to the email, they will reply to this email address.
.PARAMETER SenderName
Mandatory: The name which you will use on the sender email. This can be a different name to make it look like you are sending on behalf of someone.
.PARAMETER Subject
Mandatory: THe subject of the email.
.PARAMETER MailServer
Mandatory: The mail server you are going to use.
.PARAMETER Credential
Optional credentials switch that allows you to use another credential.
.EXAMPLE
$Message = @{
Username = "bsmith","nanderson","mjohnson"
Message = "We regrat to inform you, you were given another raise of 20%.<p> Please dance quietly as not to distrub cookie from eating her cookies.</p>"
SenderEmail = "HR@example.com"
SenderName = "Bob Jackson"
Subject = "Raze Party"
MailServer = Mail.example.com
Credential = (get-credential)
}
Send-SHDMessage @Message
.OUTPUTS
[none]
.NOTES
Author: David Bolding
.LINK
https://github.com/rndadhdman/PS_Super_Helpdesk
#>
[cmdletbinding()]
param (
[Parameter(HelpMessage = "Allows for custom Credential.", Mandatory = $True)][string[]]$username,
[Parameter(HelpMessage = "The Message to send.", Mandatory = $True)][String]$Message,
[Parameter(HelpMessage = "The Reply email.", Mandatory = $True)][String]$SenderEmail,
[Parameter(HelpMessage = "The Name of the sender.", Mandatory = $True)][string]$Sendername,
[Parameter(HelpMessage = "Message subject.", Mandatory = $True)][String]$Subject,
[Parameter(HelpMessage = "Mail Server.", Mandatory = $True)][string]$MailServer,
[Parameter(HelpMessage = "Allows for custom Credential.")][System.Management.Automation.PSCredential]$Credential
)
Write-Verbose "Starting User Loop to Send email."
foreach ($user in $username) {
Write-Verbose "Getting user Information"
try {
$TheUser = Get-ADUser -Identity $user -Properties *
}
catch {
Write-Warning "$User can not be found."
break
}
if ($TheUser.Enabled -eq $true) {
Write-Verbose "Building the HTML part of the email."
$htmlbody = @"
<html>
<style>
body {background-color:#ffffff;}
table {border: 1px solid rgb(104,107,112); text-align: left;}
th {background-color: #d2e3f7;border-bottom:2px solid rgb(79,129,189);text-align: left;}
tr {border-bottom:2px solid rgb(71,85,112);text-align: left;}
td {border-bottom:1px solid rgb(99,105,112);text-align: left;}
</style>
<body style="font-family:verdana;font-size:13">
<p>
Dear $($TheUser.Name),<br><br>
$Message<br><br>
Thank you, <br><br>
$Sendername
<br>
<p>
</body>
</html>
"@
Write-Verbose "Sending the email to $($TheUser.Mail) from $($SenderMail)"
if ($PSBoundParameters.ContainsKey('Credential')) {
Send-MailMessage -To $TheUser.mail -From "$Sendername <$SenderEmail>" -Subject "$Subject" -BodyAsHtml $htmlbody -SmtpServer $MailServer -Credential $Credential
}
else {
Send-MailMessage -To $TheUser.mail -From "$Sendername <$SenderEmail>" -Subject "$Subject" -BodyAsHtml $htmlbody -SmtpServer $MailServer
}
}
else {
Write-Verbose "Displays the disabled username"
$TheUser | Select-Object Name, Samaccountname, Enabled
}
}
} #Review - Testing, Documentation