forked from proxb/PowerShell_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ServiceStateEvent.ps1
92 lines (86 loc) · 2.96 KB
/
Get-ServiceStateEvent.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Function Get-ServiceStateEvent {
<#
.SYNOPSIS
Finds all events related to a service state change in the event log.
.DESCRIPTION
Finds all events related to a service state change in the event log.
.PARAMETER Computername
Name of the computer to query
.PARAMETER Path
The full path to a specified event log that has been archived or saved to
a filesystem location.
.NOTES
Name: Get-ServiceStateEvent
Author: Boe Prox
Version History:
1.0 //Boe Prox - 07/08/2016
- Initial version
.EXAMPLE
Get-ServiceStateEvent -Computer $Env:Computername
Description
-----------
Displays all events related to service state changes.
#>
[cmdletbinding(
DefaultParameterSetName = 'Computer'
)]
Param (
[parameter(ParameterSetName='Computer')]
[string[]]$Computername = $env:COMPUTERNAME,
[parameter(ParameterSetName='File', ValueFromPipelineByPropertyName = $True)]
[Alias('Fullname')]
[string[]]$Path
)
Begin {
Write-Verbose $PSCmdlet.ParameterSetName
If ($PScmdlet.parametersetname -eq 'File') {
$Query = @"
<QueryList>
<Query Id="0" Path="file://TOREPLACE">
<Select Path="file://TOREPLACE">*[System[(EventID=7036)]]</Select>
</Query>
</QueryList>
"@
} Else {
$Query = @"
<QueryList>
<Query Id="0" Path="System">
<Select Path="System">*[System[(EventID=7036)]]</Select>
</Query>
</QueryList>
"@
}
}
Process {
Switch ($PScmdlet.ParameterSetName) {
'Computer' {
ForEach ($Computer in $Computername) {
Get-WinEvent -ComputerName $Computer -LogName System -FilterXPath $Query | ForEach {
$Properties = $_.Properties
[pscustomobject] @{
Computername = $_.MachineName
TimeCreated = $_.TimeCreated
Servicename = $Properties[0].Value
State = $Properties[1].Value
}
}
}
}
'File' {
ForEach ($Item in $Path) {
$SearchQuery = $Query -Replace 'TOREPLACE',$Item
Write-Verbose $SearchQuery
Get-WinEvent -Path $Item -FilterXPath $SearchQuery | ForEach {
$Properties = $_.Properties
[pscustomobject] @{
Computername = $_.MachineName
TimeCreated = $_.TimeCreated
Servicename = $Properties[0].Value
State = $Properties[1].Value
}
}
}
}
}
}
}