【问题标题】:How to add an embedded image to a script?如何将嵌入图像添加到脚本中?
【发布时间】:2014-08-15 05:45:09
【问题描述】:

我完全被难住了。我正在尝试将嵌入图像添加到通知电子邮件中。我能找到的唯一脚本是使用 .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


}

}

【问题讨论】:

  • 这有帮助吗? winsysadm.net/…
  • 幻影投票者再次罢工。不要匿名投反对票,大声说出来,以便人们改进他们的问题。
  • @Ernesto,请添加为答案而不是评论。这是一个有效的答案,但应在此处列出详细信息以供将来参考。
  • Ernesto,我已经尝试在该链接中添加 .net 代码来代替 send-mailmessage,但它不起作用。
  • 我没有添加作为答案,因为我不知道它是否会对您有所帮助。我不知道为什么有人在没有解释的情况下对此投了反对票。也许您需要配置一些东西来访问 .net 框架类。

标签: image email powershell embed


【解决方案1】:

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

顺便说一句,所有的功劳都归功于创建代码的David Wyatt。他似乎是关于 powershell 脚本的一个很好的资源。

【讨论】:

  • 它在每一步都与我作斗争。当我修改 $Body 参数并添加变量时,它更接近接受,变量不起作用。是不是因为封装了$Body参数?
  • 当您尝试添加变量时,可能是语法问题。你是如何添加它们的?
  • 我在脚本的开头添加了它们。它适用于正常的发送邮件消息,但不适用于您上面发布的脚本中的参数。
猜你喜欢
  • 2015-09-10
  • 1970-01-01
  • 1970-01-01
  • 2019-06-01
  • 2011-09-07
  • 1970-01-01
  • 2010-09-24
  • 2020-10-03
  • 2023-04-08
相关资源
最近更新 更多