使用Powershell向多个收件人发送HTML格式的电子邮件
我有一个PowerShell功能,我想对其进行扩展,但遇到了一些功能上的障碍 最终目标:使用Windows PowerShell通过SMTP向多个收件人发送html格式的电子邮件 标准: 用户必须指定导入相关变量的CSV路径,或者; 用户必须将这些变量指定为参数。 html文件必须位于脚本外部,并且必须包含powershell变量。 迄今为止的职能: 如您所见,该函数没有强制参数。这很容易添加,但我不确定如何将CSVFile参数指定为必需参数或所有其他参数 下面是一个html应该是什么样子的粗略示例。如何导入和初始化PowerShell变量?请注意,以下电子邮件中的变量将从其他脚本导入,这与此无关:使用Powershell向多个收件人发送HTML格式的电子邮件,html,windows,email,powershell,parameters,Html,Windows,Email,Powershell,Parameters,我有一个PowerShell功能,我想对其进行扩展,但遇到了一些功能上的障碍 最终目标:使用Windows PowerShell通过SMTP向多个收件人发送html格式的电子邮件 标准: 用户必须指定导入相关变量的CSV路径,或者; 用户必须将这些变量指定为参数。 html文件必须位于脚本外部,并且必须包含powershell变量。 迄今为止的职能: 如您所见,该函数没有强制参数。这很容易添加,但我不确定如何将CSVFile参数指定为必需参数或所有其他参数 下面是一个html应该是什么样子的粗略
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #DEE8F1;
}
p.normal {
font-family: "Calibri";
color: black;
font-size: 13px;
}
p.normal em{
font-family: "Calibri";
color: black;
font-size: 13px;
font-weight: bold;
}
</style>
</head>
<body>
<p class="normal"><em>$($user.Name)</em>,<br /><br />
Your password will expire in <em>$($userTable.DaysTillExpiry)</em> days
on <em>$($userTable.ExpiryDate)</em><br /><br />
Your domain password is required for Computer Login, remote VPN,
and Email Access.<br /><br />
To change your password, press CTRL-ALT-DEL and choose Change Password.<br /><br>
For your password to be valid it must be 8 or more characters long and<br />
contain a mix of the following properties:<br /><br />
uppercase letters (A-Z)<br />
lowercase letters (a-z)<br />
numbers (0-9)<br /><br />
Regards,<br /><br /><br />
<em>IT Department</em> <br />
Ph: +xx xxxx xxxx <br /><br /><br />
<br /></h1>
</body>
</html>
任何帮助都将不胜感激 好的。这是一个大的。我不确定我是否能帮你解决所有的问题,但让我们试试。我不能提供一个完整的工作脚本,只是给你一些提示,告诉你如何解决这个问题,好吗 首先,为了使csvfile或所有其他参数成为必需的,您应该阅读参数集。基本上,您可以将csvfile指定给一个参数集,将所有其他参数指定给第二个参数集。然后您可以将参数设置为强制,用户只能选择csvfile或其他参数 其次,为了能够从外部文件中读取html,并且仍然能够正确扩展变量,您需要像这样读取文件:
$mailBody = Get-Content $htmlFilePath | Foreach-Object {$ExecutionContext.InvokeCommand.ExpandString($_)}
祝你好运
PS> Get-Help Send-MailMessage -Parameter Body
-Body <String>
Specifies the body (content) of the e-mail message.
Required? false
Position? 3
Default value None
Accept pipeline input? false
Accept wildcard characters? false
我会给你
珍妮多
您的密码将于2014年11月15日23天后过期
计算机登录、远程VPN和电子邮件访问需要域密码
[……]
html文件必须位于脚本外部,并且必须包含powershell变量。关于这一点还不确定,但不应该成为一个阻碍。@ojk有你的解决办法,可以通过VAR摄取。好了,谢谢你的回复。这个过程很有效,但我在html文件中读取了以下内容:$emailTemplate=[System.IO.file]::ReadAllText$htmlFilePath-替换'smtpRecipientName',$smtpRecipientName`-替换'DayStillExpirement',$daystillexpirement`-替换'expireydate',$expireydate
PS> Get-Help Send-MailMessage -Parameter Body
-Body <String>
Specifies the body (content) of the e-mail message.
Required? false
Position? 3
Default value None
Accept pipeline input? false
Accept wildcard characters? false
Function Get-HTMLvar
{
[PSCustomObject]
$user = @{ Name = 'JaneDoe' }
[PSCustomObject]
$userTable = @{ DaysTillExpiry = '23'
ExpiryDate = '2014/11/15' }
$user + $userTable
}
Function Get-HTMLbody
{
[CmdletBinding()]
Param
(
#$user Object
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
$user,
#$userTable Object
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
$userTable
)
$emailTemplate = "
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #DEE8F1;
}
p.normal {
font-family: `"Calibri`";
color: black;
font-size: 13px;
}
p.normal em{
font-family: `"Calibri`";
color: black;
font-size: 13px;
font-weight: bold;
}
</style>
</head>
<body>
<p class=`"normal`"><em>$($user.Name)</em>,<br /><br />
Your password will expire in <em>$($userTable.DaysTillExpiry)</em> days
on <em>$($userTable.ExpiryDate)</em><br /><br />
Your domain password is required for Computer Login, remote VPN,
and Email Access.<br /><br />
To change your password, press CTRL-ALT-DEL and choose Change Password.<br /><br>
For your password to be valid it must be 8 or more characters long and<br />
contain a mix of the following properties:<br /><br />
uppercase letters (A-Z)<br />
lowercase letters (a-z)<br />
numbers (0-9)<br /><br />
Regards,<br /><br /><br />
<em>IT Department</em> <br />
Ph: +xx xxxx xxxx <br /><br /><br />
<br /></h1>
</body>
</html>"
Write-Output $emailTemplate
}
Function Send-HTMLemail
{
[CmdletBinding()]
Param
(
[Parameter()]
[string]$smtpRecipientAddress = 'Rcpt@domain.local',
[Parameter()]
[string]$smtpSubject = 'Subject',
[Parameter()]
[string]$smtpFromAddress = 'Admin@domain.local',
[Parameter()]
[string]$smtpServer = 'relay.domain.local',
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[string]$emailTemplate
)
Send-MailMessage -From $smtpFromAddress `
-To $smtpRecipientAddress `
-SmtpServer $smtpServer `
-Body $emailTemplate `
-BodyAsHtml `
-Subject $smtpSubject `
-Verbose
}
PS> Get-HTMLvar | Get-HTMLbody | Send-HTMLemail