我正在尝试创建一个Powershell脚本,该脚本获取文本文件的特定部分,将其读取,然后将其放入电子邮件的内容中并发送。这是我目前拥有的:
$logs = (Get-EventLog system | where {$_.InstanceId -eq 7001 -and
$_.TimeWritten -gt (Get-Date).Adddays(-1)}).TimeWritten | Out-String
#to file
$logs | Out-File ".\Results.txt"
#create COM object named Outlook
$Outlook = New-Object -ComObject Outlook.Application
#create Outlook MailItem named Mail using CreateItem() method
$Mail = $Outlook.CreateItem(0)
#add properties as desired
$Mail.To = "SomeMailAddress.com"
$Mail.Subject = "Time"
$Mail.Body = $logs
#send message
$Mail.Send()
#quit and cleanup
$Outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null
我能够创建文本文件,输出数据,我相信Get-Content正在获取该特定时间段,但是我不确定如何使用Set-Content并将其放入电子邮件中。任何建议/帮助将不胜感激
答案 0 :(得分:2)
通过PowerShell发送电子邮件的最简单方法是使用Send-MailMessge
。
下面是使用Outlook ComOjbect发送邮件的方式。 注意:如果必须使用Outlook comobject,请确保使用相同的帐户以相同的方式运行PowerShell和Outlook。
示例:
$logs = (Get-EventLog system | where {$_.InstanceId -eq 7001 -and $_.TimeWritten -gt (Get-Date).Adddays(-1)}).TimeWritten | Out-String
#create COM object named Outlook
$Outlook = New-Object -ComObject Outlook.Application
#create Outlook MailItem named Mail using CreateItem() method
$Mail = $Outlook.CreateItem(0)
#add properties as desired
$Mail.To = "jrider@yourDomain.com"
$Mail.Subject = "Time"
$Mail.Body = $logs
#send message
$Mail.Send()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null