PowerShell tips for getting the system log from a file _powershell

Source: Internet
Author: User
Tags datetime system log mailmessage

Sometimes you may need to analyze system files to transfer them to the hard drive, or you want to read the system log directly from "EVTX".

You can do this:

Copy Code code as follows:

$path = "$env: windir\system32\winevt\logs\setup.evtx"
Get-winevent-path $path

Another piece of code to get the system log

Copy Code code as follows:

$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
}
}

Support all versions of PowerShell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.