Powershell小技巧之从文件获取系统日志

时间:2021-05-22

有时你可能会需要分析系统文件将他们传输到硬盘,或你想直接从“evtx”读取系统日志。

你可以这样做:

复制代码 代码如下:
$path = "$env:windir\System32\Winevt\Logs\Setup.evtx"
Get-WinEvent -Path $path

另附上一段获取系统日志的代码

复制代码 代码如下:
$StartTime = (get-date).Date + (new-timespan -Hours 6 -Minutes 35)
$EndTime = (get-date).Date + (new-timespan -Hours 6 -Minutes 36)
$global:TaskStart
$Global:TaskComplete
$Global:events
$Global:event
$Global:TimeSpent
$Global:events = get-winevent -FilterHashtable @{logname = "Microsoft-Windows-TaskScheduler/Operational"; ID=107;StartTime=$StartTime;EndTime=$EndTime}
Foreach($Global:event in $Global:events)
{
cls
$StartLogs=get-winevent -FilterHashtable @{logname = "Microsoft-Windows-TaskScheduler/Operational";ID=100;StartTime=$StartTime}
$CompleteLogs=get-winevent -FilterHashtable @{logname = "Microsoft-Windows-TaskScheduler/Operational";id=102;StartTime=$StartTime}
$global:TaskStart=$StartLogs | where {$_.ActivityId -eq $Global:event.ActivityId}
$Global:TaskComplete=$CompleteLogs | where {$_.ActivityId -eq $Global:event.ActivityId}
$global:TimeSpent=($global:TaskComplete.timeCreated-$global:TaskStart.timeCreated).TotaLMinutes
if(($global:TaskStart -ne $NULL) -and ($Global:TaskComplete -ne $null) -and ($Global:TimeSpent -gt 1)){

$Messagebody="Sync task started at: "+$global:TaskStart.TimeCreated.DateTime+"`r`n"
$Messagebody=$Messagebody+"`r`nSync task completed at: "+$global:TaskComplete.timeCreated.DateTime+"`r`n"
$Messagebody=$Messagebody+"`r`nTask lasted for "+("{0:N2}" -f ($Global:TimeSpent) )+" minutes"

Send-MailMessage -From "CustomerLog@avepoint.com" -To "Zhijie.bai@avepoint.com","Infrastructure_cn@avepoint.com" -Subject "Customer Logs Sync Report:Success" -Body $Messagebody -SmtpServer "10.100.100.153" -Encoding UTF8
}
else{
$Messagebody="########################################################################`r`n"
$Messagebody=$Messagebody+"`r`nCustom logs Sync failed, please login 10.2.0.125 to check and sync again`r`n"
$Messagebody=$Messagebody+"`r`n########################################################################`r`n"
Send-MailMessage -From "CustomerLog@avepoint.com" -To "Zhijie.bai@avepoint.com","Infrastructure_cn@avepoint.com" -Subject "Customer Logs Sync Report:Failed" -Body $Messagebody -SmtpServer "10.100.100.153" -Encoding UTF8 -Priority High
}
}

支持Powershell所有版本

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章