Email Powershell脚本设计将消息正文写入日志
我正在尝试通过Outlook收件箱进行解析,并匹配某些邮件,然后针对每个匹配项,将电子邮件正文写入我的应用程序日志。到目前为止,我的代码如下所示,但我发现脚本为每个匹配都编写了相同的消息。也就是说,脚本存储的最后一个消息体将为筛选器捕获的每个匹配项写入X次Email Powershell脚本设计将消息正文写入日志,email,powershell,foreach,Email,Powershell,Foreach,我正在尝试通过Outlook收件箱进行解析,并匹配某些邮件,然后针对每个匹配项,将电子邮件正文写入我的应用程序日志。到目前为止,我的代码如下所示,但我发现脚本为每个匹配都编写了相同的消息。也就是说,脚本存储的最后一个消息体将为筛选器捕获的每个匹配项写入X次 Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null $olFolders = "Microsoft.Office.Interop.Outlook.olDefa
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$inbox = $namespace.getDefaultFolder($olFolders::olFolderInBox)
$filter = (%{$inbox.items | Where {$_.SenderName -match ‘TestUser’ -and $_.UnRead -eq $true}})
$filter.count
foreach ($msg in $filter)
{
$MsgBody = $filter | select-object Body | format-table -HideTableHeaders -Wrap | Out-String
}
for ($i = $filter.count; $i -gt 0 ; $i --) {
Write-eventlog -logname Application -source "TestAlerts" -eventID 100 -entrytype Information -message "$MsgBody"
#$($msg)[$i].UnRead -eq $false - this still isn't working either.
}
试试这个:
for ($i = $filter.count; $i -gt 0 ; $i --) {
$msg = $filter[$i]
$MsgBody = $msg | select-object Body | format-table -HideTableHeaders -Wrap | Out-String
Write-eventlog -logname Application -source "TestAlerts" -eventID 100 -entrytype Information -message "$MsgBody"
}
试试这个:
for ($i = $filter.count; $i -gt 0 ; $i --) {
$msg = $filter[$i]
$MsgBody = $msg | select-object Body | format-table -HideTableHeaders -Wrap | Out-String
Write-eventlog -logname Application -source "TestAlerts" -eventID 100 -entrytype Information -message "$MsgBody"
}
成功了-谢谢!!我唯一需要做的更改是设置$I-gt-1,因为它将无法在最后一条消息上写入。这很有效-谢谢!!我需要做的唯一更改是设置$I-gt-1,因为它将无法写入最后一条消息。