-
-
Save developerprofiles/45757d0d6fbb399d2dfec9d4e404aeb7 to your computer and use it in GitHub Desktop.
A PowerShell function and format file to query the event log using Get-WinEvent for restart related events.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function Get-Restart { | |
| [cmdletbinding()] | |
| [outputtype("RestartEvent")] | |
| Param( | |
| [Parameter(Position = 0, ValueFromPipeline)] | |
| [ValidateNotNullOrEmpty()] | |
| [Alias("CN")] | |
| [string]$Computername = $env:COMPUTERNAME, | |
| [Parameter(HelpMessage = "Find restart events since this date and time.")] | |
| [ValidateNotNullOrEmpty()] | |
| [Alias("Since")] | |
| [datetime]$After, | |
| [int64]$MaxEvents, | |
| [PSCredential]$Credential | |
| ) | |
| Begin { | |
| Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)" | |
| $filter = @{ | |
| Logname = "System" | |
| ID = 1074 | |
| } | |
| if ($After) { | |
| Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Getting restart events after $After" | |
| $filter.Add("StartTime", $After) | |
| } | |
| $splat = @{ | |
| ErrorAction = "Stop" | |
| FilterHash = $Filter | |
| } | |
| if ($MaxEvents -gt 0) { | |
| Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Limiting search to $MaxEvents event(s)" | |
| $splat.Add("MaxEvents", $MaxEvents) | |
| } | |
| if ($Credential.UserName) { | |
| Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Adding a credential for $($Credential.UserName)" | |
| $splat.Add("Credential", $Credential) | |
| } | |
| } #begin | |
| Process { | |
| Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Getting restart events on $($Computername.ToUpper())" | |
| $splat.Computername = $Computername | |
| Try { | |
| $entries = Get-WinEvent @splat | |
| } | |
| Catch { | |
| Throw $_ | |
| } | |
| if ($entries) { | |
| #process entries into custom objects | |
| foreach ($entry in $entries) { | |
| #resolve the user SID | |
| Try { | |
| Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Translating $($entry.UserId)" | |
| $user = $entry.UserId.translate([System.Security.Principal.NTAccount]).value | |
| } | |
| Catch { | |
| $user = $entry.properties[-1].value | |
| #$entry.userid | |
| } | |
| [pscustomobject]@{ | |
| PSTypeName = "RestartEvent" | |
| Computername = $entry.machinename.ToUpper() | |
| Datetime = $entry.TimeCreated | |
| Username = $user | |
| Category = $entry.properties[4].value | |
| Process = $entry.properties[0].value.split()[0].trim() | |
| } | |
| } #foreach item | |
| } | |
| } #process | |
| End { | |
| Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)" | |
| } #end | |
| } #close Get-Restart | |
| #add custom formatting | |
| Update-FormatData $PSScriptRoot\restartevent.format.ps1xml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <!-- | |
| Format type data generated 05/06/2021 17:33:49 by PROSPERO\Jeff | |
| This file was created using the New-PSFormatXML command that is part | |
| of the PSScriptTools module. | |
| https://github.com/jdhitsolutions/PSScriptTools | |
| --> | |
| <Configuration> | |
| <ViewDefinitions> | |
| <View> | |
| <!--Created 05/06/2021 17:33:49 by PROSPERO\Jeff--> | |
| <Name>default</Name> | |
| <ViewSelectedBy> | |
| <TypeName>RestartEvent</TypeName> | |
| </ViewSelectedBy> | |
| <GroupBy> | |
| <PropertyName>Computername</PropertyName> | |
| <Label>Computername</Label> | |
| </GroupBy> | |
| <TableControl> | |
| <!--Delete the AutoSize node if you want to use the defined widths. | |
| <AutoSize />--> | |
| <TableHeaders> | |
| <TableColumnHeader> | |
| <Label>When</Label> | |
| <Width>24</Width> | |
| <Alignment>left</Alignment> | |
| </TableColumnHeader> | |
| <TableColumnHeader> | |
| <Label>Username</Label> | |
| <Width>30</Width> | |
| <Alignment>left</Alignment> | |
| </TableColumnHeader> | |
| <TableColumnHeader> | |
| <!-- The width must be wide enough to accomodate the ANSI escape sequences--> | |
| <Label>Category</Label> | |
| <Width>15</Width> | |
| <Alignment>left</Alignment> | |
| </TableColumnHeader> | |
| <TableColumnHeader> | |
| <Label>Process</Label> | |
| <Alignment>left</Alignment> | |
| </TableColumnHeader> | |
| </TableHeaders> | |
| <TableRowEntries> | |
| <TableRowEntry> | |
| <TableColumnItems> | |
| <TableColumnItem> | |
| <PropertyName>Datetime</PropertyName> | |
| </TableColumnItem> | |
| <TableColumnItem> | |
| <ScriptBlock> | |
| if ($host.name -match 'Console|Code' -AND $_.Username -match " ") { | |
| "$([char]27)[38;5;207m$($_.Username)$([char]27)[0m" | |
| } | |
| else { | |
| $_.Username | |
| } | |
| </ScriptBlock> | |
| </TableColumnItem> | |
| <TableColumnItem> | |
| <ScriptBlock> | |
| <!-- colorize the category using ANSI--> | |
| if ($host.name -match 'Console|Code') { | |
| Switch ($_.Category) { | |
| "power off" { $ansi = "[38;5;200m"} | |
| "restart" { $ansi = "[92m"} | |
| "shutdown" { $ansi = "[38;5;214m" } | |
| default { $ansi = "[37m"} | |
| } | |
| "$([char]27)$Ansi$($_.category)$([char]27)[0m" | |
| } | |
| else { | |
| $_.Category | |
| } | |
| </ScriptBlock> | |
| </TableColumnItem> | |
| <TableColumnItem> | |
| <PropertyName>Process</PropertyName> | |
| </TableColumnItem> | |
| </TableColumnItems> | |
| </TableRowEntry> | |
| </TableRowEntries> | |
| </TableControl> | |
| </View> | |
| </ViewDefinitions> | |
| </Configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. I sent a message as issue to you a few months ago (to developerprofile account, I guess it yours). I have a question about a repository that you have forked. I am going to delete my github account (not this account) but you (and other users) have forked my repository. If I ask, would you delete the repository? If you reply this comment can write what repository that would you delete. Looking forward to your reply because it's important for me. Also I have contacted with Github Support too and they said me that I should direct contact with you. Thx.