Image 如何将嵌入式图像添加到脚本中?
我完全被难住了。我正在尝试将嵌入的图像添加到通知电子邮件中。我能找到的唯一脚本是使用.net创建新的电子邮件对象。这只能使用.net创建电子邮件对象吗?有什么想法吗Image 如何将嵌入式图像添加到脚本中?,image,email,powershell,embed,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 no
#################################################
# 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
}
}
不使用dot-net类。我希望有帮助
$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
顺便说一句,所有的功劳都归功于创建代码的人。他似乎是powershell脚本的优秀资源 这有用吗?幽灵般的选民再次罢工。不要匿名投票,大声说出来,这样人们可以改进他们的问题。@Ernesto,请添加答案而不是评论。这是一个有效的答案,但应在此处列出详细信息以供将来参考。Ernesto,我已尝试在该链接中添加.net代码以代替send mailmessage,但没有效果。我没有添加作为答案,因为我不知道这是否对您有帮助。我不知道为什么有人没有解释就否决了这一点。也许你需要一些配置来访问.net framework类的东西。这一过程中的每一步都会让我感到困扰。当我修改$Body参数并添加变量时,这些变量不起作用。是因为$Body参数被封装了吗?当您尝试添加变量时,可能是语法问题。你是如何添加它们的?我在脚本的开头添加了它们。它适用于普通的sendmailmessage,但不适用于上面发布的脚本中的参数。