我有一个PowerShell功能,我想扩展它,但已经遇到了一些功能上的障碍。
结束目标:通过SMTP将带有Windows PowerShell的html格式的电子邮件发送给多个收件人。
标准:
到目前为止的功能:
Function Send-HTMLEmail
{
[CmdletBinding()]
Param
(
[Parameter(ValueFromPipelineByPropertyName=$true)][string]$smtpRecipientAddress,
[Parameter(ValueFromPipelineByPropertyName=$true)][string]$smtpSubject,
[Parameter(ValueFromPipelineByPropertyName=$true)][string]$smtpFromAddress = 'info@domain.com',
[Parameter(ValueFromPipelineByPropertyName=$true)][string]$smtpServer = 'exchserver',
[Parameter(ValueFromPipelineByPropertyName=$true)][string]$htmlFilePath,
[Parameter(ValueFromPipelineByPropertyName=$true)][string]$CSVFile
)
$smtpClient = new-object system.net.mail.smtpClient
$mailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = $smtpServer
$mailMessage.From = $smtpFromAddress
$mailMessage.To.add($smtpRecipientAddress)
$mailMessage.Subject = $smtpSubject
$mailMessage.IsBodyHtml = $true
$mailMessage.Body = $emailTemplate
$smtpClient.Send($mailMessage)
}
如您所见,该函数没有必需参数。这很容易添加,但我不确定如何将CSVFile参数指定为强制参数或所有其他参数。
下面是html应该是什么样子的粗略示例。如何导入并初始化PowerShell变量? (请注意,以下电子邮件中的变量将从另一个脚本导入,这是不相关的):
<!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>
非常感谢任何帮助!
答案 0 :(得分:0)
确定。这是一个很大的问题。不确定我是否能够帮助您解决所有问题,但请试试。而且我无法提供一个完整的脚本,只是给你提示如何解决它,好吗?
首先,为了能够使csvfile或所有其他参数成为必需参数,你应该阅读参数集。基本上,您将csvfile分配给参数集,将所有其他分配给第二个参数集。然后,您可以将参数设置为必需,并且用户只能选择csvfile或其他参数。
其次,为了能够从外部文件中读取html,并且仍然正确扩展了变量,您需要读取这样的文件:
$mailBody = Get-Content $htmlFilePath | Foreach-Object {$ExecutionContext.InvokeCommand.ExpandString($_)}
祝你好运!
答案 1 :(得分:0)
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
好吧,所以你需要一个String传递给-Body。
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
会给你
JaneDoe,
您的密码将于2014年11月15日在23天后过期
您的域密码是计算机登录,远程VPN和电子邮件访问所必需的。
[..]