-
Notifications
You must be signed in to change notification settings - Fork 11
/
Tips - Here-String use to send automatic and personalized mails.ps1
149 lines (132 loc) · 4.72 KB
/
Tips - Here-String use to send automatic and personalized mails.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Here-String : use it to send automatic and personalized mails
# But what is a Here-String ?
$Message=@"
Hello, this is your admin
Welcome to the Corp
Today we're showing you a Here-String.
"@
$Message | gm
<#
$Message | gm
TypeName : System.String
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
CompareTo Method int CompareTo
....
#>
$Message.GetType()
<#
$Message.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
#>
# as we can see, a Here-String is a [String]
<#
We can notice that we can use the syntax @ "" @ or @ '' @
In the second case, as in any chain surrounded by simple quote,the variables are not interpreted.
#>
# So for what use (s)?
<#
Imagine that we want to send a personalized email to x people. It's boring to manually generate a body for the mail every time!
We could consider something like this
#>
$FirstName = "Michel"
$SupportCallNumber = " XX-XX-XX-XX-XX"
$Report = Get-Item -Path "C:\Temp\report.html"
$Message =@"
Bonjour $FirstName !
Vous trouverez ci-joint le rapport sur le parc machine en date du $($Report.CreationTime.ToShortDateString())
Si vous avez des questions sur ce dernier, vous pouvez appeler le support au $SupportCallNumber
Cordialement
L'équipe Admin Système
"@
<#
It's clean, in text format, but it's clean.
We can then consider having a body in Html to allow more important enrichments
#>
### Determination of the Body for the mails
$HtmlBody = @"
<head>
<style type='text/css'>
body {
font-family: Calibri
p{color:blue;}
}
</style>
</head>
<body>
<center><P>Bonjour $FirstName,</P></center>
<br>
<br><P>Vous trouverez ci-joint le rapport sur le parc machine en date du $($Report.CreationTime.ToShortDateString())</P>
<br>
<P> Si vous avez des questions sur ce dernier, vous pouvez appeler le support au $SupportCallNumber</P>
<br>
<center><P>Cordialement
<br>
<B>L'équipe Admin Système</B></center>
</body>
"@
# Note the internal css in the HEAD (minima in this case)
# At the beginning of the script we will also define the variables.
$From = "sender@domain.com"
$To = "Dest1@domain.com", "Dest2@domain.com"
$Cc = "AdminGroup@domain.com"
$HtmlBody = $HtmlBody
$SmtpServer = "smtp.domain.com"
$priority = "normal"
$Encoding = "UTF8"
$Subjet = "Computers Asset Report in date of $Date"
# Preparing a Hash table for mail parameters
$SendMailMessageParams = @{
From = $From
To = $To
Cc = $Cc
Body = $HtmlBody
SmtpServer = $SmtpServer
Priority = $priority
Encoding = $Encoding
Subject = $Subjet
BodyAsHtml = $true
}
Send-MailMessage @SendMailMessageParams
<#
Another example of use
Here we will have a here-string with different values separated by a comma
we'll convert the Here-String to hash table with convert-FromCsv, and paste it into a variable
We will specify the Headers to use
then we will use a mail body template incorporating variables
#>
$InfoUsers = @'
DomainScripto,UserID,ContosoJSmith,Fabrikam
toto,UserID,Contosototo,Fabrikam
'@ | ConvertFrom-Csv -Delimiter "," -Header "username", "userid", "password", "office"
$template = @"
Hello and welcome to Contoso {0} !
Your user id on the system will be {1} with a Temporary Password of {2} and your home Office is in {3}.
You can be reached at $PhoneNumber. If you have any issues with your setup you can call 425-555-1111 in order to reach the Help desk.
Thanks!
Your Friendly Neighborhood It Department
"@
# finally we make our loop foreach to send to each his personalized mail
foreach ($item in $InfoUsers)
{
$SendMailMessageParams = @{
From = $From
To = $To
Cc = $Cc
Body = $letter
SmtpServer = $SmtpServer
Priority = $priority
Encoding = $Encoding
Subject = $Subjet
BodyAsHtml = $false
}
$letter = $template -f $item.username, $item.useriduserid, $item.passwordpassword, $item.office
Send-MailMessage @SendMailMessageParams -Body
}
<#
largely inspired by:
Source : https://devblogs.microsoft.com/scripting/maximizing-the-power-of-here-string-in-powershell-for-configuration-data/
#>