我正在使用此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 = "ali.shariaty@domain.com";
$mailTo = "ali.shariaty@domain.com";
$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 :(得分:0)
您是否尝试过将主体设置为仅包含消息?即。
$mailtext = $WriteHostParams.Object
目前您正在尝试将电子邮件的正文设置为哈希表,我希望在将其转换为字符串后将其设置为“System.Collections.Hashtable”