使用从Windows ID找到的数据,并通过Powershell将其放入电子邮件中
我正在尝试创建一个Powershell脚本,该脚本获取文本文件的特定部分,读取文本文件,然后将其放入电子邮件内容并发送。这就是我目前拥有的:使用从Windows ID找到的数据,并通过Powershell将其放入电子邮件中,powershell,Powershell,我正在尝试创建一个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"
$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
我能够创建文本文件,输出数据,我相信获取内容会获得特定的时间段,但我不确定如何使用设置内容并将其放入电子邮件中。任何建议/帮助都将不胜感激通过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
通过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
我正在尝试获取文本的特定部分(时间),这是我唯一需要的,有没有更简单的方法来获取这一部分?我在想:导出到文本->阅读文本->获取特定部分->发送到电子邮件。如果有办法的话:从日志中获取时间->发送到电子邮件中,那就更好了@米切尔莫纳奇。我已经更新了解决方案,只获取
$logs
变量中生成的时间。该变量显示不正确,并且在尝试运行时,不会将时间合并到正文部分。您有一个Out字符串,但没有其他参数作为提示。现在是出局时间还是类似的时间?时间是准确的,只是让它进入身体是我的问题:'D@MitchellMonarch抱歉,我没有测试上次更新。我刚刚用一个更新的解决方案对它进行了测试,它可以正常工作。要回答您的问题,只需将输出放入字符串中即可format@MitchellMonarch输出字符串
应自行设置。您应该能够运行$logs=(Get EventLog system | Where Object{$\uuu.timewrited-gt(Get Date).Adddays(-1)}).timewrited | Out String
,然后调用$logs
作为测试。这就是你的邮件正文将有的内容。我正在尝试获取文本的特定部分(时间),这是我唯一需要的,有没有更简单的方法来获取这一部分?我在想:导出到文本->阅读文本->获取特定部分->发送到电子邮件。如果有办法的话:从日志中获取时间->发送到电子邮件中,那就更好了@米切尔莫纳奇。我已经更新了解决方案,只获取$logs
变量中生成的时间。该变量显示不正确,并且在尝试运行时,不会将时间合并到正文部分。您有一个Out字符串,但没有其他参数作为提示。现在是出局时间还是类似的时间?时间是准确的,只是让它进入身体是我的问题:'D@MitchellMonarch抱歉,我没有测试上次更新。我刚刚用一个更新的解决方案对它进行了测试,它可以正常工作。要回答您的问题,只需将输出放入字符串中即可format@MitchellMonarch输出字符串
应自行设置。您应该能够运行$logs=(Get EventLog system | Where Object{$\uuu.timewrited-gt(Get Date).Adddays(-1)}).timewrited | Out String
,然后调用$logs
作为测试。这是您的邮件正文将具有的内容