2014-06-24 34 views
0

我完全難倒了。我正在嘗試向通知電子郵件中添加嵌入式圖像。我能找到的唯一腳本是使用.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 <[email protected]>」 
################################################# 

#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 [email protected] -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 <[email protected]>" 
    To = '[email protected]' 
    #Cc = '[email protected]', '[email protected]' 
    SmtpServer = 'smtprelay.domain.com' 

} 

Send-MailMessage @params 


} 

} 
+0

這有幫助嗎? http://winsysadm.net/send-mail-with-inline-embedded-images-with-powershell/ – Ernesto

+0

幻影downvoters再次罷工。不要成爲一個匿名downvoter,說出來,讓人們可以改善他們的問題。 –

+0

@ Ernesto,請添加爲答案而不是評論。這是一個有效的答案,但細節應列在這裏供將來參考。 –

回答

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 = '[email protected]' 
    To = '[email protected]' 
    Cc = '[email protected]', '[email protected]' 
    SmtpServer = 'smtp.gmail.com' 
    Port = 587 
    Credential = (Get-Credential) 
    UseSsl = $true 
} 

Send-MailMessage @params 

順便說一句,David Wyatt誰創建的代碼。他似乎是一個很好的PowerShell腳本資源。

+0

它一路戰鬥我。它更接近當我修改$ Body參數並添加變量時,變量不起作用。是因爲$ Body參數被封裝了嗎? – user2402045

+0

當您嘗試添加變量時,可能是語法問題。你如何添加它們? – Ernesto

+0

我在腳本的開頭添加了它們。它適用於正常的send-mailmessage,但不適用於您在上面發佈的腳本中的參數。 – user2402045

相關問題