我找到了这个密码到期脚本,其中我想在HTML消息的开头(标题)和末尾(页脚)嵌入本地图像,但无论采用哪种方式,我都会在收到的电子邮件中拍摄图像仍然以红叉显示。
$PPNConfig_DebugLevel = 0
$PPNConfig_NotificationTimeInDays = 100
$PPNConfig_SMTPServerAddress = "smtp.somedomain.local"
$PPNConfig_FromAddress = "admin@somedomain.local"
$PPNConfig_BodyIsHtml = $true
$PPNConfig_DirectoryRoot = "LDAP://DC=somedomain,DC=local"
$PPNConfig_MaxPageSize = 1000
function Configure-Notification-Subject($nName, $nNumDays)
{
return "$nName, your password will expire in $nNumDays days."
}
function Configure-Notification-Body-Html($nName, $nNumDays)
{
$bodyHtml = "
Body text here....
"
return $bodyHtml
}
function Get-Domain-MaxPassword-Age
{
$ThisDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$DirectoryRoot = $ThisDomain.GetDirectoryEntry()
$DirectorySearcher = [System.DirectoryServices.DirectorySearcher]$DirectoryRoot
$DirectorySearcher.Filter = "(objectClass=domainDNS)"
$DirectorySearchResult = $DirectorySearcher.FindOne()
$MaxPasswordAge = New-Object System.TimeSpan([System.Math]::ABS($DirectorySearchResult.properties["maxpwdage"][0]))
return $MaxPasswordAge
}
function Get-Users-With-Expiring-Passwords
{
$UsersToNotify = @()
$DirectoryRoot = New-Object System.DirectoryServices.DirectoryEntry($PPNConfig_DirectoryRoot)
$DirectorySearcher = New-Object System.DirectoryServices.DirectorySearcher($DirectoryRoot)
$DirectorySearcher.filter = "(&(objectCategory=Person)(objectClass=User)(!userAccountControl:1.2.840.113556.1.4.803:=2)(!(userAccountControl:1.2.840.113556.1.4.803:=65536)))"
$DirectorySearcher.pagesize = $PPNConfig_MaxPageSize
$MaxPasswordAge = Get-Domain-MaxPassword-Age
$MaxPasswordAgeDays = $MaxPasswordAge.Days
$DirectorySearchResult = $DirectorySearcher.FindAll() |
ForEach-Object -ErrorAction "SilentlyContinue" `
-Process `
{
$PwdChanged = ([adsi]$_.path).psbase.InvokeGet("PasswordLastChanged")
$DaysTillExpiring = $MaxPasswordAgeDays - ((Get-Date) - $PwdChanged).Days
if ($DaysTillExpiring -le $PPNConfig_NotificationTimeInDays)
{
$UserToAdd = New-Object psobject
$UserToAdd | Add-Member NoteProperty -Name "Name" -Value ([adsi]$_.path).name[0]
$UserToAdd | Add-Member NoteProperty -Name "Email" -Value ([adsi]$_.path).mail[0]
$UserToAdd | Add-Member NoteProperty -Name "DaysLeft" -Value $DaysTillExpiring
$UsersToNotify += $UserToAdd
}
}
return $UsersToNotify
}
function Send-Email-Notification-Of-Expiry($nName, $nEmail, $nDaysLeft)
{
$SmtpClient = New-Object System.Net.Mail.SmtpClient($PPNConfig_SMTPServerAddress)
$NewMail = New-Object System.Net.Mail.MailMessage
$NewMail.From = $PPNConfig_FromAddress
$NewMail.To.Add($nEmail)
$NewMail.Subject = Configure-Notification-Subject $nName $nDaysLeft
if ($PPNConfig_BodyIsHtml)
{
$NewMail.IsBodyHtml = $true
$NewMail.Body = Configure-Notification-Body-Html $nName $nDaysLeft
}
else
{
$NewMail.IsBodyHtml = $false
$NewMail.Body = Configure-Notification-Body-Plain $nName $nDaysLeft
}
$SmtpClient.Send($NewMail)
}
$UsersToNotify = Get-Users-With-Expiring-Passwords
foreach ($User in $UsersToNotify)
{
if ($PPNConfig_DebugLevel -gt 0)
{
Write-Host $User
}
else
{
Send-Email-Notification-Of-Expiry $User.Name $User.Email $User.DaysLeft
}
}
答案 0 :(得分:0)
这是我如何做到的。我把它放在这里,但我已经有一个完整的博客文章。我发送的电子邮件通知一直都是非常有用的
http://winsysadm.net/send-mail-with-inline-embedded-images-with-powershell/