如何将嵌入图像添加到脚本?

时间:2014-06-24 20:17:21

标签: image email powershell embed

我完全难过了。我正在尝试将嵌入图像添加到通知电子邮件中。我能找到的唯一脚本是使用.net来创建一个新的电子邮件对象。只能使用.net创建一个电子邮件对象吗?任何想法

#################################################
# Configure the following variables….
# expireindays1 + 2 = At what count of days left on a password do you want a notification?
$smtpServer=”smtprelay.domain.com”
$expireindays1 = 5
#$expireindays2 = 1
$from = “Technology Helpdesk <technologyhelpdesk@domain.com>”
#################################################

#Get Users From AD who are enabled
Import-Module ActiveDirectory
$users = get-aduser -filter * -Properties enabled, GivenName, passwordneverexpires, passwordexpired, emailaddress, passwordlastset |where {$_.Enabled -eq “True”} | where { $_.PasswordNeverExpires -eq $false } | where { $_.passwordexpired -eq $false }

foreach ($user in $users)
{
$Name = (Get-ADUser $user | foreach { $_.Name})
$UserID = (Get-ADUser $user -Properties *).Samaccountname
$emailaddress = $user.emailaddress
$passwordSetDate = (get-aduser $user -properties passwordlastset | foreach { $_.PasswordLastSet })
$PasswordPol = (Get-AduserResultantPasswordPolicy $user)
# Check for Fine Grained Password
if (($PasswordPol) -ne $null)
{
$maxPasswordAge = ($PasswordPol).MaxPasswordAge
}

else
{
$maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
}

$expireson = $passwordsetdate + $maxPasswordAge
$today = (get-date)
$daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days
#$subject=


$body = @' 
#<html>  
  <body>  
    <img src="cid:image1"><br>  
  <b>$UserID : Your password will expire soon</b>
<br>
<br>
<br>
Dear $name,<br>

Your password will expire in $daystoexpire days.  Please change your password before it expires to avoid password related problems. .<br>
<br>
<br>
<b>Password complexity rules:<br> 
<br>
- Must be 7 or 8 characters long<br>
- Must contain at least 1 uppercase letter<br> 
- Must contain at least 1 lowercase letter<br>
- Must contain at least 1 number<br> 
- Must NOT contain repeating characters (e.g., aa, 11)</b><br> 
<br>
<br>
Please use the following steps to change your password:<br> 
- Press CTRL+ALT+DEL<br> 
- Click Change a password…<br> 
- Verify your User ID is correct, then type in your old and new passwords (you cannot use previously used passwords)<br> 
- After the change is complete, you will be prompted that your password has been changed<br>
- If you have a Blackberry, iPhone, or iPad, those devices will need your new password as soon as your password has been changed<br>
<br>
If you have questions, or need assistance updating your passwords, please contact the Technology Help Desk. <br>
  </body>  
#</html>  
'@  

if (($daystoexpire -eq $expireindays1))

# -or ($daystoexpire -eq $expireindays2))


{
#Send-Mailmessage -smtpServer $smtpServer -from $from -to user@domain.com -subject $subject -body $body -bodyasHTML -priority High











####################################################################################################################







$images = @{ 
    image1 = "c:\temp\action needed.png"  
}  


$params = @{ 
    InlineAttachments = $images 
    #Attachments = 'C:\temp\action needed.png'
    Body = $body 
    BodyAsHtml = $true 
    Subject = ”Your password will expire in $daystoExpire days”
    From = "Technology Helpdesk <technologyhelpdesk@domain.com>"  
    To = 'user@domain.com' 
    #Cc = 'recipient2@domain.com', 'recipient3@domain.com' 
    SmtpServer = 'smtprelay.domain.com' 

} 

Send-MailMessage @params


}

}

1 个答案:

答案 0 :(得分:0)

This code不使用点网类。我希望它有所帮助。

$images = @{ 
    image1 = 'c:\temp\test.jpg' 
    image2 = 'C:\temp\test2.png' 
}  

$body = @' 
<html>  
  <body>  
    <img src="cid:image1"><br> 
    <img src="cid:image2"> 
  </body>  
</html>  
'@  

$params = @{ 
    InlineAttachments = $images 
    Attachments = 'C:\temp\attachment1.txt', 'C:\temp\attachment2.txt' 
    Body = $body 
    BodyAsHtml = $true 
    Subject = 'Test email' 
    From = 'username@gmail.com' 
    To = 'recipient@domain.com' 
    Cc = 'recipient2@domain.com', 'recipient3@domain.com' 
    SmtpServer = 'smtp.gmail.com' 
    Port = 587 
    Credential = (Get-Credential) 
    UseSsl = $true 
} 

Send-MailMessage @params

BTW全部归功于创建代码的David Wyatt。他似乎是PowerShell脚本的一个很好的资源。