2016-06-23 23 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”它被浇铸为字符串后

相关问题