2016-06-23 77 views
0

我正在使用此PowerShell函數檢查文件夾。 它工作正常,當添加,更改和刪除文件發生在文件夾;它會在主機屏幕上顯示日誌消息。PowerShell函數結果將不會通過電子郵件發送

$FileSystemWatcher = New-Object System.IO.FileSystemWatcher 
$FileSystemWatcher | Get-Member -Type Properties,Event 
$FileSystemWatcher.Path = "C:\Users\ali.shariaty\Desktop\test" 
    Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Created -Action { 
    $Object = "{0} was {1} at {2}" -f $Event.SourceEventArgs.FullPath, 
    $Event.SourceEventArgs.ChangeType, 
    $Event.TimeGenerated 
    $WriteHostParams = @{ 
    ForegroundColor = 'Green' 
    BackgroundColor = 'Black' 
    Object = $Object 
    } 
    Write-Host @WriteHostParams 
} 
    Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Changed -Action { 
    $Object = "{0} was {1} at {2}" -f $Event.SourceEventArgs.FullPath, 
    $Event.SourceEventArgs.ChangeType, 
    $Event.TimeGenerated 
    $WriteHostParams = @{ 
    ForegroundColor = 'Yellow' 
    BackgroundColor = 'Black' 
    Object = $Object 
    } 
    Write-Host @WriteHostParams 
} 
    Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Deleted -Action { 
    $Object = "{0} was {1} at {2}" -f $Event.SourceEventArgs.FullPath, 
    $Event.SourceEventArgs.ChangeType, 
    $Event.TimeGenerated 
    $WriteHostParams = @{ 
    ForegroundColor = 'Red' 
    BackgroundColor = 'Black' 
    Object = $Object 
    } 
    Write-Host @WriteHostParams 
} 

我的問題是,當我將這些行添加到我的代碼給我發電子郵件的結果,我的電子郵件的正文是空的,不包含日誌信息。

$mailtxt = $WriteHostParams 
$mailSmtpServer = "mail.domain.com"; 
$mailFrom = "[email protected]"; 
$mailTo = "[email protected]"; 
$mailSubject = "Folder Change" 
$mailbody = $mailtxt 
$mail = New-Object Net.Mail.SmtpClient($mailSmtpServer); 
$msg = new-object Net.Mail.MailMessage; 
$msg.IsBodyHtml = 1; 
$msg.To.Add($mailTo); 
$msg.From = $mailFrom; 
$msg.Subject = $mailSubject; 
$msg.Body = $mailbody; 
$mail.Send($msg); 

有人可以幫助並告訴我我做錯了什麼嗎? 謝謝。

回答

0

您是否嘗試將身體設置爲只是消息?即

$mailtext = $WriteHostParams.Object 

目前您正試圖將電子郵件的正文設置爲一個哈希表,這是我所期望的也只是將其設置爲「System.Collections.Hashtable」它被澆鑄爲字符串後

相關問題