关于将日期减少一个的Powershell脚本

时间:2016-10-27 16:56:57

标签: powershell

到目前为止,这是我的脚本。我的目的是从前一天获取事件日志,并每天早上给自己发送一封电子邮件。到目前为止,这是我的剧本。

(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的新手,所以请轻松一点。

1 个答案:

答案 0 :(得分:0)

所以你要做的第一件事就是在DateTime中得到昨天,这就是我假设你试图用$dateif ($date = (get-date).AddDays(-1))

然后让我们将日志位置放在一个变量中,以便我们可以在保存日志和发送附件时使用它

-After参数使用我们在DateTime中保存的$YesterdaySet-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