无法将Powershell电子邮件发送给经理

时间:2018-11-21 12:49:07

标签: powershell

我正在尝试创建一个PS脚本,以在接下来的10天内向管理员发送有关帐户过期(没有密码)的员工的电子邮件。 我知道已经讨论过几次了,但是我找不到适合该问题的解决方案。

这是我到目前为止所拥有的:

$MyEmail = "email@domain.com"
$SMTP= "domain.com"
$To = "email@domain.com"    
$Subject = "Account expiring"
$Body = "Hi_,

the following account is due to expire, please reply to this email if 
you wish to extend the account for a further 3 months

Kind regards,

Tech Team"

$Users = Get-ADUser -filter * -SearchBase "OU=Powershell Test 
OU,DC=domain,DC=com" -Properties emailaddress, 
Manager,accountexpirationdate
$Users | Select 
Name,emailaddress,accountexpirationdate,@{label="Manager";expression= . 
{(Get-ADUser $_.Manager -Properties emailaddress).emailaddress}}

 Send-MailMessage -To $to -From $MyEmail -Subject $Subject -Body $Body 
-SmtpServer $SMTP 

我相信我缺少了几块。 有人可以帮忙吗? 非常感谢。

谢谢

1 个答案:

答案 0 :(得分:0)

所以我已经编辑了以前的评论,因为我无法忍受那仍然不是很好:

$MyEmail = "email@domain.com"
$secpasswd = ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force
$SMTP= "domain.com"
$SMTPPort = "587"
$cred = New-Object System.Management.Automation.PSCredential ($MyEmail, $secpasswd)

$NeverExpires = 9223372036854775807;
$ExpireMin = (Get-Date).AddDays(0);
$ExpireMax = (Get-Date).AddDays(90);
$today = (Get-Date)

try{
    $expiringsoon = Get-ADUser -Filter * -SearchBase "OU=Powershell Test OU,DC=domain,DC=com" -Properties accountExpires | Where-Object {$_.accountExpires -ne $NeverExpires -and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) -lt $ExpireMax -and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) -gt $ExpireMin }
}catch{
    throw $_
}
foreach($user in $expiringsoon){
    try{
        $userDetails = Get-ADUser $user -Properties AccountExpirationDate,accountExpires,manager
    }catch{
        Write-Host "Error while searching for user '$user'" -ForegroundColor Red
    }
    $ExpiryDate = $userDetails.AccountExpirationDate
    $DaysLeft = ($ExpiryDate-$today).days
    $DateStr = $ExpiryDate.AddDays(-1).ToString("dd/MM/yyyy")
    $name = $user.Name
    $manager = $userDetails.manager
    try{
        $managerEmail = (Get-ADUser $manager -Properties emailAddress).emailAddress
        if(!$managerEmail){
            $managerEmail = $MyEmail
        }
    }catch{
        $managerEmail = $MyEmail
    }

    # Set Greeting based on Number of Days to Expiry. 

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

    if (($DaysLeft) -gt "1") 
    { 
        $messageDays = "in " + "$DaysLeft" + " days" 
    } 
    ElseIf (($DaysLeft) -eq "1") 
    { 
        $messageDays = "in 1 day" 
    } 
    else 
    { 
        $messageDays = "today"
    } 


   # Email Subject Set Here 
    $subject="The account of $name will expire $messageDays " 

    # Email Body Set Here, Note You can use HTML, including Images. 
    $body =" 
    Hi,
    <p>the following account '$name' is due to expire, please reply to this email if you wish to extend the account for a further 3 months<br>
    <p>Kind regards, <br>
    Tech Team  
    </P>"  

    #----- Show Details of sent mail -----#
    try{
        Send-MailMessage -to $managerEmail -from $MyEmail -SmtpServer $SMTP -subject $subject -body $body -bodyasHTML -priority High –Credential $cred -port $SMTPPort -UseSsl
        Write-Host "An e-mail has been send to $managerEmail about $name" -ForegroundColor Green
    }catch{
        Write-Host "Email to $managerEmail for $name failed with error: $($_.exception.message)" -ForegroundColor Red
    }
}

好的,因为我忍不住想要正确地编写脚本。这是我给你的礼物。完美的脚本,可以将电子邮件发送给用户在90天内到期的经理。我还包括错误处理。请将此脚本作为创建自己的脚本的指南,如果有任何疑问,请随时使用。