Difference between
modified post 3347 by rubrub on Thu 12th Apr 04:42 and
original post 3346 by ddadasf on Thu 12th Apr 04:41
Showold version | new version | both versions
| 1 | 1 | ################################################## | |
| 2 | 2 | # cmdlets | |
| 3 | 3 | ################################################## | |
| 4 | 4 | #------------------------------------------------- | |
| 5 | 5 | # Send-HTMLFormattedEmail | |
| 6 | 6 | #------------------------------------------------- | |
| 7 | 7 | # Usage: Send-HTMLFormattedEmail -? | |
| 8 | 8 | #------------------------------------------------- | |
| 9 | 9 | function Send-HTMLFormattedEmail { | |
| 10 | 10 | <# | |
| 11 | 11 | .Synopsis | |
| 12 | 12 | Used to send an HTML Formatted Email. | |
| 13 | 13 | .Description | |
| 14 | 14 | Used to send an HTML Formatted Email that is based on an XSLT template. | |
| 15 | 15 | .Parameter To | |
| 16 | 16 | Email address or addresses for whom the message is being sent to. | |
| 17 | 17 | Addresses should be seperated using ;. | |
| 18 | 18 | .Parameter ToDisName | |
| 19 | 19 | Display name for whom the message is being sent to. | |
| 20 | 20 | .Parameter CC | |
| 21 | 21 | Email address if you want CC a recipient. | |
| 22 | 22 | Addresses should be seperated using ;. | |
| 23 | 23 | .Parameter BCC | |
| 24 | 24 | Email address if you want BCC a recipient. | |
| 25 | 25 | Addresses should be seperated using ;. | |
| 26 | 26 | .Parameter From | |
| 27 | 27 | Email address for whom the message comes from. | |
| 28 | 28 | .Parameter FromDisName | |
| 29 | 29 | Display name for whom the message comes from. | |
| 30 | 30 | .Parameter Subject | |
| 31 | 31 | The subject of the email address. | |
| 32 | 32 | .Parameter Content | |
| 33 | 33 | The content of the message (to be inserted into the XSL Template). | |
| 34 | 34 | .Parameter Relay | |
| 35 | 35 | FQDN or IP of the SMTP relay to send the message to. | |
| 36 | 36 | .XSLPath | |
| 37 | 37 | The full path to the XSL template that is to be used. | |
| 38 | 38 | #> | |
| 39 | 39 | param( | |
| 40 | 40 | [Parameter(Mandatory=$True)][String]$To, | |
| 41 | 41 | [Parameter(Mandatory=$True)][String]$ToDisName, | |
| 42 | 42 | [String]$CC, | |
| 43 | 43 | [String]$BCC, | |
| 44 | 44 | [Parameter(Mandatory=$True)][String]$From, | |
| 45 | 45 | [Parameter(Mandatory=$True)][String]$FromDisName, | |
| 46 | 46 | [Parameter(Mandatory=$True)][String]$Subject, | |
| 47 | 47 | [Parameter(Mandatory=$True)][String]$Content, | |
| 48 | 48 | [Parameter(Mandatory=$True)][String]$Relay, | |
| 49 | 49 | [Parameter(Mandatory=$True)][String]$XSLPath | |
| 50 | 50 | ) | |
| 52 | 52 | try { | |
| 53 | 53 | # Load XSL Argument List | |
| 54 | 54 | $XSLArg = New-Object System.Xml.Xsl.XsltArgumentList | |
| 55 | 55 | $XSLArg.Clear() | |
| 56 | 56 | $XSLArg.AddParam("To", $Null, $ToDisName) | |
| 57 | 57 | $XSLArg.AddParam("Content", $Null, $Content) | |
| 59 | 59 | # Load Documents | |
| 60 | 60 | $BaseXMLDoc = New-Object System.Xml.XmlDocument | |
| 61 | 61 | $BaseXMLDoc.LoadXml("<root/>") | |
| 63 | 63 | $XSLTrans = New-Object System.Xml.Xsl.XslCompiledTransform | |
| 64 | 64 | $XSLTrans.Load($XSLPath) | |
| 66 | 66 | #Perform XSL Transform | |
| 67 | 67 | $FinalXMLDoc = New-Object System.Xml.XmlDocument | |
| 68 | 68 | $MemStream = New-Object System.IO.MemoryStream | |
| 70 | 70 | $XMLWriter = [System.Xml.XmlWriter]::Create($MemStream) | |
| 71 | 71 | $XSLTrans.Transform($BaseXMLDoc, $XSLArg, $XMLWriter) | |
| 73 | 73 | $XMLWriter.Flush() | |
| 74 | 74 | $MemStream.Position = 0 | |
| 76 | 76 | # Load the results | |
| 77 | 77 | $FinalXMLDoc.Load($MemStream) | |
| 78 | 78 | $Body = $FinalXMLDoc.Get_OuterXML() | |
| 80 | 80 | # Create Message Object | |
| 81 | 81 | $Message = New-Object System.Net.Mail.MailMessage | |
| 83 | 83 | # Now Populate the Message Object. | |
| 84 | 84 | $Message.Subject = $Subject | |
| 85 | 85 | $Message.Body = $Body | |
| 86 | 86 | $Message.IsBodyHTML = $True | |
| 88 | 88 | # Add From | |
| 89 | 89 | $MessFrom = New-Object System.Net.Mail.MailAddress $From, $FromDisName | |
| 90 | 90 | $Message.From = $MessFrom | |
| 92 | 92 | # Add To | |
| 93 | 93 | $To = $To.Split(";") # Make an array of addresses. | |
| 94 | 94 | $To | foreach {$Message.To.Add((New-Object System.Net.Mail.Mailaddress $_.Trim()))} # Add them to the message object. | |
| 96 | 96 | # Add CC | |
| 97 | 97 | if ($CC){ | |
| 98 | 98 | $CC = $CC.Split(";") # Make an array of addresses. | |
| 99 | 99 | $CC | foreach {$Message.CC.Add((New-Object System.Net.Mail.Mailaddress $_.Trim()))} # Add them to the message object. | |
| 100 | 100 | } | |
| 102 | 102 | # Add BCC | |
| 103 | 103 | if ($BCC){ | |
| 104 | 104 | $BCC = $BCC.Split(";") # Make an array of addresses. | |
| 105 | 105 | $BCC | foreach {$Message.BCC.Add((New-Object System.Net.Mail.Mailaddress $_.Trim()))} # Add them to the message object. | |
| 106 | 106 | } | |
| 108 | 108 | # Create SMTP Client | |
| 109 | 109 | $Client = New-Object System.Net.Mail.SmtpClient $Relay | |
| 111 | 111 | # Send The Message | |
| 112 | 112 | $Client.Send($Message) | |
| 113 | 113 | } | |
| 114 | 114 | catch { | |
| 115 | 115 | throw $_ | |
| 116 | 116 | } | |
| 117 | 117 | } | |
| 119 | 119 | ################################################## | |
| 120 | 120 | # Main | |
| 121 | 121 | ################################################## | |
| 122 | 122 | Export-ModuleMember Send-HTMLFormattedEmail | |
| 124 | 124 | ### XSLT Template Example | |
| 125 | 125 | <?xml version="1.0"?> | |
| 126 | 126 | <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
| 128 | 128 | <xsl:output media-type="xml" omit-xml-declaration="yes" /> | |
| 129 | 129 | <xsl:param name="To"/> | |
| 130 | 130 | <xsl:param name="Content"/> | |
| 131 | 131 | <xsl:template match="/"> | |
| 132 | 132 | <html> | |
| 133 | 133 | <head> | |
| 134 | 134 | <title>My First Formatted Email</title> | |
| 135 | 135 | </head> | |
| 136 | 136 | <body> | |
| 137 | 137 | <div width="400px"> | |
| 138 | 138 | <p>Dear <xsl:value-of select="$To" />,</p> | |
| 139 | 139 | <p></p> | |
| 140 | 140 | <p><xsl:value-of select="$Content" /></p> | |
| 141 | 141 | <p></p> | |
| 142 | 142 | <p><strong>Please do not respond to this email!</strong><br /> | |
| 143 | 143 | An automated system sent this email, if any point you have any questions or concerns please open a help desk ticket.</p> | |
| 144 | 144 | <p></p> | |
| 145 | 145 | <Address> | |
| 146 | 146 | Many thanks from your:<br /> | |
| 147 | 147 | Really Cool IT Team<br /> | |
| 148 | 148 | </Address> | |
| 149 | 149 | </div> | |
| 150 | 150 | </body> | |
| 151 | 151 | </html> | |
| 152 | 152 | </xsl:template> | |
| 153 | 153 | </xsl:stylesheet> |
ContributeMost Recent Contributions (feed)
- Sub-Array with the Large
- 1 second ago
-
http://www.mytechinterviews.com/sub-array-with-the-largest-sum
Question: You are given an array with integers (both positive and negative) in any random order. Find the sub-array with the largest sum.
On Sunday Netflix will release 15 new episodes of Arrested Development! YEAH!
- New-ObjectFromGenericTyp
- 12 hours ago
-
Get an instances of a Generic Type.
- Lync Version
- 13 hours ago
-
Gets the version of Microsoft Lync components
- Set-Wallpaper (CTP3) fix
- 16 hours ago
-
Set-Wallpaper lets you set your windows desktop wallpaper. It requires PInvoke and I wrote it using PowerShell 2’s
Add-Type, although it could be done in v1 using the JIT code generation tricks Lee Holmes has mentioned in the past …- Updated for CTP3
- Made it run as a script instead of a function.
- command line fix 05 2013
tested on win7 pro, execute cmd : powershell c:\path\script.ps1 “c:\path\wallpaper.jpg”
See http://stackoverflow.com/questions/6147731/powershell-script-fails-first-time-but-works-second-time
- ShowUI Clock 4
- 37 hours ago
-
This is another analog clock, with “hands” and old school stopped movement (no click sound, but maybe you could add that)...
- ShowUI Clock 4
- 37 hours ago
-
This is another analog clock, with “hands” and old school stopped movement (no click sound, but maybe you could add that)...
- Auto Services
- 40 hours ago
-
############################################################################################# #
- NAME: Autoservices.ps1
- AUTHOR: Rob Sewell http://newsqldbawiththebeard.wordpress.com @fade2blackuk
- DATE:15/05/2013 #
- COMMENTS: # Script to show the services running that are set to Automatic startup –
- good for checking after reboot
- ————————————————————————————————-
- Ping
- 41 hours ago
-
############################################################################################# #
- NAME: Ping.ps1
- AUTHOR: Rob Sewell http://newsqldbawiththebeard.wordpress.com @fade2blackuk
- DATE:15/05/2013 #
- COMMENTS: This script to set up a continous ping
- Use CTRL + C to stop it
- ————————————————————————————————————
- Untitled
- 41 hours ago
-
############################################################################################# #
- NAME: RDP.ps1
- AUTHOR: Rob Sewell http://newsqldbawiththebeard.wordpress.com @fade2blackuk
- DATE:15/05/2013 #
- COMMENTS: This script to open a RDP
- ————————————————————————————————————
- Untitled
- 41 hours ago
-
############################################################################################# #
- NAME: StopSQLServices.ps1
- AUTHOR: Rob Sewell http://newsqldbawiththebeard.wordpress.com @fade2blackuk
- DATE:15/05/2013 #
- COMMENTS: This script will stop all SQL Services on a server
- ————————————————————————————————————
PowerShell Code Repository