# set the event log name you want to subscribe to # (use Get-EventLog -AsString for a list of available event log names) $Name = 'Application'
# get an instance $Log = [System.Diagnostics.EventLog]$Name
# determine what to do when an event occurs $Action = { # get the original event entry that triggered the event $entry = $event.SourceEventArgs.Entry
# log all events Write-Host"Received from $($entry.Source): $($entry.Message)"
# do something based on a specific event if ($entry.EventId -eq1-and$entry.Source -eq'WinLogon') { Write-Host"Test event was received!"-ForegroundColor Red }
}
# subscribe to its "EntryWritten" event $job = Register-ObjectEvent-InputObject$log-EventName EntryWritten -SourceIdentifier'NewEventHandler'-Action$Action
# now whenever an event is written to the log, $Action is executed # use a loop to keep PowerShell busy. You can abort via CTRL+C
Write-Host"Listening to events"-NoNewline
try { do { Wait-Event-SourceIdentifier NewEventHandler -Timeout1 Write-Host"."-NoNewline
} while ($true) } finally { # this executes when CTRL+C is pressed Unregister-Event-SourceIdentifier NewEventHandler Remove-Job-Name NewEventHandler Write-Host"" Write-Host"Event handler stopped." }
Simply “convert” the event log name into an object of “EventLog” type. The result looks similar to this and provides information about the number of entries and the log file size: 只需要将时间日志名称“转换”为一个 “EventLog“ 类型的对象。结果类似这样,并且提供了条目的数量和日志文件尺寸等信息:
1 2 3 4 5
PS> $systemLogDirect
Max(K) Retain OverflowAction Entries Log ------------------------------------ 20.4800 OverwriteAsNeeded 19.806 System
# make sure this PFX file exists or create one # or load a code-signing cert from other sources # (review the previous tips for hints) $pfxFile = "$home\desktop\tobias.pfx" $cert = Get-PfxCertificate-FilePath$pfxFile
# make sure this folder exists and contains # PowerShell script that you'd like to sign $PathWithScripts = 'c:\myScripts'
# apply signatures to all scripts in the folder Get-ChildItem-Path$PathWithScripts-Filter *.ps1 -Recurse | Set-AuthenticodeSignature-Certificate$cert
# apply signatures to all scripts in the folder Get-ChildItem-Path$PathWithScripts-Filter *.ps1 -Recurse | Set-AuthenticodeSignature-Certificate$cert-TimestampServer http://timestamp.digicert.com