簡體   English   中英

PowerShell發送電子郵件

[英]PowerShell to send email

我正在運行任務計划程序以每30分鍾執行一次PowerShell。

所以

$ users = Search-ADAccount -LockedOut -SearchBase“ OU = People,DC = Example,DC = com” -SearchScope子樹| 選擇SamAccountName

這將返回用戶列表。 現在,我想與用戶創建一個電子郵件正文

$EmailBody = ForEach($user in $users) {"`r`n",$user}



Send-MailMessage -To $to -Subject "Locked Accounts" -BodyAsHtml $EmailBody -From $from -Credential $cred -SmtpServer $server -Debug

但是,即使沒有人被鎖定,我當前的代碼也會發送電子郵件。 無論如何,我可以檢查$ users是否至少有1個人嗎?

(我尚未測試$ EmailBody,但想知道這對於BodyAsHtml是否可以接受)

================

更新的代碼

if ($users)    {
    if ($users.count -gt 0) {#even if there are users, this line always is false. 
        # send the email here
        foreach($user in $users)
        {
            $message = $message + " " + $user + " is locked out" + "`r`n"
            Write-Host $user
        }
        Send-MailMessage -To $to -Subject "Locked Accounts" -BodyAsHtml $message -From $from -Credential $cred -SmtpServer $server -Debug        
    } }
if ($users -ne $null)   # lists can be empty as well, in which case the count fails
{
    if ($users.count -gt 0) {
        # send the email here
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM