无法使用Send-Mailmessage

时间:2015-08-26 09:57:24

标签: email powershell

我是PowerShell新手,在自动电子邮件中嵌入图片时遇到问题。我正在使用Technet的Robert Pearman脚本的修改版本。电子邮件本身会发送给任何密码将在未来14天内过期的用户。

该脚本扫描AD以查找帐户,检索所有详细信息并向其发送个性化的电子邮件。

我将有关将HTML图像嵌入电子邮件的所有内容都失败了。它看起来很简单,但是当电子邮件到达时,电子邮件始终显示红色X HTML占位符图像。

$smtpServer= "SMTP.company.biz" 
$expireindays = 14
$from = "Helpdesk <helpdesk@company.biz>"
$logging = "Disabled" # Set to Disabled to Disable Logging
$logFile = "<log file path>" # ie. c:\mylog.csv
$testing = "Enabled" # Set to Disabled to Email Users
$testRecipient = "admin@company.biz"
$date = Get-Date -format dd-MM-yyyy
#$images = @{image1='D:\Script_Folder\Overwatch_images\FFPasswordChange.jpg'}
$AttachmentPath = "D:\Script_Folder\Overwatch_Images\FFPasswordChange.jpg"
$Attachment = New-Object System.Net.Mail.Attachment($AttachmentPath)
$Attachment.ContentID = "image1"

# Check Logging Settings
if (($logging) -eq "Enabled")
{
# Test Log File Path
$logfilePath = (Test-Path $logFile)
if (($logFilePath) -ne "True")
{
    # Create CSV File and Headers
    New-Item $logfile -ItemType File
    Add-Content $logfile "Date,Name,EmailAddress,DaystoExpire,ExpiresOn"
}
} # End Logging Check

# Get Users From AD who are Enabled, Passwords Expire and are Not   Currently Expired
Import-Module ActiveDirectory
$users = get-aduser -filter {MemberOf -eq "Group1,OU=Groups,OU=Group   Companies,DC=Company,DC=Biz"} -searchbase "OU=Users,OU=Group Companies,DC=Company,DC=Biz"   -properties Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet,   EmailAddress, samaccountname | where {$_.Enabled -eq "True"} | where {   $_.PasswordNeverExpires -eq $false } | where { $_.passwordexpired -eq $false   }
$DefaultmaxPasswordAge = (Get-  ADDefaultDomainPasswordPolicy).MaxPasswordAge


# Process Each User for Password Expiry
foreach ($user in $users)
{
$Name = $user.Name
$emailaddress = $user.emailaddress
$passwordSetDate = $user.PasswordLastSet
$samaccountname = $user.samaccountname
$PasswordPol = (Get-AduserResultantPasswordPolicy $user)
# Check for Fine Grained Password
if (($PasswordPol) -ne $null)
{
    $maxPasswordAge = ($PasswordPol).MaxPasswordAge
}
else
{
    # No FGP set to Domain Default
    $maxPasswordAge = $DefaultmaxPasswordAge
}


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

# Set Greeting based on Number of Days to Expiry.

# Check Number of Days to Expiry
$messageDays = $daystoexpire

if (($messageDays) -ge "1")
{
    $messageDays = "in " + "$daystoexpire" + " Tagen"
}
else
{
    $messageDays = "heute."
}

# Email Subject Set Here
$subject="Ihr eIPN Passwort endet $messageDays"


$body = "
<HTML>
<BODY>
Sehr geehrter Comany User,
<p> Ihr eIPN Passwort f&uuml;r den Account $samaccountname l&auml;uft  $messageDays ab.<br>
<p> Damit Sie sicher und problemlos weiterarbeiten k&ouml;nnen, bitten wir Sie Ihr Passwort schnellstm&ouml;glich zu &auml;ndern.<br>
<p> Um Ihr eIPN Passwort zu &auml;ndern, dr&uuml;cken Sie bitte STRG-ALT- ENTF und w&auml;hlen Sie 'Kennwort &auml;ndern...' aus.<br>
<br>
<img src='CID:image1'>
<br>
<p> Sollten Sie sich im Au&szlig;endienst oder nicht im B&uuml;ro befinden, stellen Sie bitte vorher eine aktive VPN-Verbindung her.<br>
<p> Bei Problemen oder Fragen wenden Sie sich bitte an die Helpdesk unter der +49 9999 999999.<br>
<p> Mit freundlichen Gr&uuml;&szlig;en, <br>
Deutschland Helpdesk <br> 
</P>
</body>
</HTML>"


# If Testing Is Enabled - Email Administrator
if (($testing) -eq "Enabled")
{
    $emailaddress = $testRecipient
} # End Testing

# If a user has no email address listed
if (($emailaddress) -eq $null)
{
    $emailaddress = $testRecipient    
}# End No Valid Email

# Send Email Message
if (($daystoexpire -ge "0") -and ($daystoexpire -lt $expireindays))
{
     # If Logging is Enabled Log Details
    if (($logging) -eq "Enabled")
    {
        Add-Content $logfile    "$date,$Name,$emailaddress,$daystoExpire,$expireson" 
    }

    # Set Parameters
    $params = @{
    #InlineAttachements = $Images
    #Attachments = 'D:\Script_Folder\Overwatch-  Images\FFPasswordChange.jpg'
    Body = $body
    BodyasHTML = $true
    Subject = $subject
    From = $from
    To = $emailaddress
    SMTPServer = $smtpServer
    }

    # Send Email Message
    Send-Mailmessage @params -Priority High

    # -smtpServer $smtpServer -from $from -to $emailaddress -subject   $subject -body $body -bodyasHTML -InlineAttachments $images -priority High  


} # End Send Message

} # End User Processing



# End

我仍然有一些早期的尝试在代码中被删除了。我觉得自己走在正确的道路上但从未正确执行过。

如果有人能够看到我出错的地方或指出我正确的方向,那将是非常感激的!

0 个答案:

没有答案