到目前为止,这是我的脚本。我的目的是从前一天获取事件日志,并每天早上给自己发送一封电子邮件。到目前为止,这是我的剧本。
(Get-EventLog -LogName Application -After "10/26/16" | ConvertTo-Html | set-content 'C:\Users\myusername\Documents\Powershell Outputs\1day event log.html')
$date= get-date
if ($date=get-date).AddDays(-1)
Else {"File Not Found"}`
Send-MailMessage -To "my email" -From "my email" -subject "Eventlog for Yesterday" -body "This is a daily sent automated email for the event log of the previous day." -Attachments "`
现在我做错了什么,我是PowerShell的新手,所以请轻松一点。
答案 0 :(得分:0)
所以你要做的第一件事就是在DateTime
中得到昨天,这就是我假设你试图用$date
和if ($date = (get-date).AddDays(-1))
然后让我们将日志位置放在一个变量中,以便我们可以在保存日志和发送附件时使用它
-After
参数使用我们在DateTime
中保存的$Yesterday
。 Set-Content
的路径是$logPath
#Get DateTime for Yesterday
$Yesterday = (Get-Date).AddDays(-1)
#Set the location of the log attachment
$logPath = 'C:\Users\myusername\Documents\Powershell Outputs\1day event log.html'
#Get the Event Log After Yesterday, Convert it to HTML, Save it to the logPath
Get-EventLog -LogName Application -After $Yesterday | ConvertTo-Html | Set-Content $logPath
#Send Mail Message with the logPath as an attachment
Send-MailMessage -To "my email" -From "my email" -subject "Eventlog for Yesterday" -body "This is a daily sent automated email for the event log of the previous day." -Attachments $logpath