Created
March 19, 2015 10:45
-
-
Save weipah/051c3fd421fa5dde30e4 to your computer and use it in GitHub Desktop.
MassMailerHTML - Inform users from ActiveDirectory per Mail with Send-Mailmessage Cmdlet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script to inform multiple users via Mail | |
# Two datasources | |
# 1. Old Firewall VPN Config -> Exported as XML | |
# 2. Active Directory Accounts | |
$date = (get-date) | |
# Which file should be send with the mail as an attachment? | |
$attachment = "C:\Users\xyz\Documents\beispiel-anhang.txt" | |
# Text for HTML Body | |
$body = @" | |
<p>Aufgrund der Umstellung einer Netzwerkkomponente wird <b>ab TT.MM. 12.00 Uhr</b> die VPN-Verbindung über den XYZ-Client nicht mehr unterstützt.</p> | |
<p>Als Übergangslösung dient die Verbindungsdatei im Anhang.</p> | |
<p>Bei Fragen wenden Sie sich bitte an <a href="mailto:[email protected]">[email protected]</a></p> | |
<p> | |
Mit freundlichen Grüßen,<br> | |
<b>IT-Support</b><br> | |
</p> | |
"@ | |
# Read XML-Config-Extract from old Firewall | |
$xml = [xml](get-content "C:\Users\xyz\Documents\WatchGuard\config\Usernames_export.xml") | |
$members = ($xml.SelectSingleNode("/profile/user-group-list/user-group[name='IPSECUser']/member-list")).member | |
$user_komplett = @() | |
foreach ($user in $members) { | |
try { | |
$user_komplett += @(get-aduser -identity $user -server HOSTNAME -properties EmailAddress,accountexpirationdate,accountexpires|? {($_.enabled -eq $true) -and (($_.accountexpires -ge 0) -or (($_.accountexpirationdate -gt $date) -or (($_.accountexpirationdate -eq 0) -or (-NOT($_.accountexpirationdate -like "*")))))}|select-object samaccountname, surname, givenname, emailaddress) | |
} catch { | |
write-host "User $user not found in Directory." | |
} | |
} | |
# Second part users from AD group | |
$user_komplett += @(Get-ADGroupMember -Identity "ADGroupName" -recursive -Server HOSTNAME |get-aduser -server HOSTNAME -properties EmailAddress,accountexpirationdate,accountexpires|? {($_.enabled -eq $true) -and (($_.accountexpires -ge 0) -or (($_.accountexpirationdate -gt $date) -or (($_.accountexpirationdate -eq 0) -or (-NOT($_.accountexpirationdate -like "*")))))}|select-object samaccountname, surname, givenname, emailaddress ) | |
# Output as file | |
$user_komplett|?{($_.surname.length -gt 0) -and ($_.emailaddress.length -gt 0) -and ($_.givenname.length -gt 0)}|sort surname, emailaddress -unique|ft * -a|out-file ".\user_komplett.txt" | |
write-host $user_komplett.count | |
# Surname and Givenname in first row | |
$anrede = "<p>Sehr geehrte(r) " + $user_komplett[0].givenname + " " + $user_komplett[0].surname + ",</p>" | |
$strBody = $anrede + $body | |
Send-MailMessage -To "[email protected]" -From "Support <[email protected]>" -Subject "Änderungen VPN-Zugang" -SmtpServer "smtp.example.org" -Body $strBody -BodyAsHtml -Attachment $attachment -encoding ([System.Text.Encoding]::UTF8) | |
start-sleep -s 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment