forked from guyrleech/Ivanti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIvanti UWM EM event processor.ps1
320 lines (265 loc) · 9.63 KB
/
Ivanti UWM EM event processor.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#requires -version 3
<#
Get Ivanti UWM EM event log entries and split into sortable table for durations
@guyrleech 2019
Modification History:
16/10/19 GRL Initial public release
#>
<#
.SYNOPSIS
Get Ivanti UWM EM event log entries and split into sortable table for durations
.PARAMETER computerName
The computer whose AppSense event log is to be queried
.PARAMETER start
The start time/date to process event logs from
.PARAMETER end
The end time/date to process event logs to
.PARAMETER duration
How long to process events for. Must be used with -start. Specify with s,m,h,d,w,y for seconds,minutes,hours,days,weeks and years respectively, e.g. 4h for 4 hours
.PARAMETER last
Process events for the last period where the period is specified as something like 4h where s,m,h,d,w,y are for seconds,minutes,hours,days,weeks and years respectively
.PARAMETER user
Only process events for the specified user
.PARAMETER csv
The path to a csv file which will have the results saved to it. The save will fail if the file already exists
.PARAMETER triggerOnly
Only show events for the trigger summary rather than all events
.PARAMETER delimiter
Specify the delimiter for the csv file, e.g. a semicolon for Dutch users
.PARAMETER noGridView
Do not display an onscreen grid view but instead output the results to the pipeline
.EXAMPLE
& '.\Ivanti UWM EM event processor.ps1' -last 4h
Show all AppSense EM events for the last 4 hours on the local machine for all users
.EXAMPLE
& '.\Ivanti UWM EM event processor.ps1' -start "09:00 10/10/19" -duration 1h -user fred -computername xa02 -triggerOnly
Show trigger duration summaries from computer xa02 for user fred from 0900 for 1 hour on 10th October 2019
#>
[CmdletBinding()]
Param
(
[string]$computerName ,
[string]$start ,
[string]$end ,
[string]$duration ,
[string]$last ,
[string]$user ,
[string]$csv ,
[switch]$triggerOnly ,
[string]$delimiter = ',' ,
[switch]$noGridView ,
## Shouldn't be any need to change these!
[string]$providerName = 'AppSense Environment Manager.' ,
[int[]]$successEventIds = @( 9405 , 9407 , 9409 , 9413 , 9420 , 9422 , 9424 , 9426 , 9428 , 9430 , 9432 , 9434 , 9436 , 9438 , 9440 , 9442 , 9662 ) ,
[int[]]$failEventIds = @( 9406 , 9408 , 9410 , 9414 , 9421 , 9423 , 9425 , 9427 , 9429 , 9431 , 9433 , 9435 , 9437 , 9439 , 9441 , 9443 )
)
if( $PSBoundParameters[ 'last' ] )
{
if( $PSBoundParameters[ 'start' ] -or $PSBoundParameters[ 'end' ] )
{
Throw 'Cannot use -start or -end with -last'
}
## see what last character is as will tell us what units to work with
[int]$multiplier = 0
switch( $last[-1] )
{
's' { $multiplier = 1 }
'm' { $multiplier = 60 }
'h' { $multiplier = 3600 }
'd' { $multiplier = 86400 }
'w' { $multiplier = 86400 * 7 }
'y' { $multiplier = 86400 * 365 }
default { Throw "Unknown multiplier `"$($last[-1])`"" }
}
Remove-Variable -Name 'End'
[datetime]$script:end = Get-Date
if( $last.Length -le 1 )
{
$secondsAgo = $multiplier
}
else
{
$secondsAgo = ( ( $last.Substring( 0 , $last.Length - 1 ) -as [decimal] ) * $multiplier )
}
Remove-Variable -Name 'Start'
[datetime]$script:start = $end.AddSeconds( -$secondsAgo )
}
elseif( $PSBoundParameters[ 'start' ] )
{
## Check time formats as bad ones get stripped by query so search whole event log
$parsed = New-Object -TypeName 'DateTime'
if( ! [datetime]::TryParse( $start , [ref]$parsed ) )
{
Throw "Invalid start time/date `"$start`""
}
else
{
Remove-Variable -Name 'Start'
[datetime]$script:start = $parsed
}
if( $PSBoundParameters[ 'duration' ] )
{
if( $PSBoundParameters[ 'end' ] )
{
Throw 'Cannot use both -duration and -end'
}
[int]$multiplier = 0
switch( $duration[-1] )
{
's' { $multiplier = 1 }
'm' { $multiplier = 60 }
'h' { $multiplier = 3600 }
'd' { $multiplier = 86400 }
'w' { $multiplier = 86400 * 7 }
'y' { $multiplier = 86400 * 365 }
default { Throw "Unknown multiplier `"$($duration[-1])`"" }
}
if( $duration.Length -le 1 )
{
$secondsDuration = $multiplier
}
else
{
$secondsDuration = ( ( $duration.Substring( 0 , $duration.Length - 1 ) -as [decimal] ) * $multiplier )
}
[datetime]$end = $parsed.AddSeconds( $secondsDuration )
}
elseif( ! $PSBoundParameters[ 'end' ] )
{
Remove-Variable -Name 'End'
[datetime]$script:end = Get-Date
}
elseif( ! [datetime]::TryParse( $end , [ref]$parsed ) )
{
Throw "Invalid end time/date `"$start`""
}
else
{
Remove-Variable -Name 'End'
[datetime]$script:end = $parsed
}
}
## else ## no start
if( $start -gt $end )
{
Throw "Start $(Get-Date -Date $start -Format G) is after end $(Get-Date -Date $end -Format G)"
}
if( $start -gt (Get-Date) )
{
Write-Warning "Start $(Get-Date -Date $start -Format G) is in the future by $([math]::Round(($start - (Get-Date)).TotalHours,1)) hours"
}
[hashtable]$filterHashtable = @{ 'ProviderName' = $providerName }
if( $start )
{
$filterHashtable.Add( 'StartTime' , $start )
}
if( $end )
{
$filterHashtable.Add( 'EndTime' , $end )
}
if( $PSBoundParameters[ 'user' ] )
{
if( $user.IndexOf( '\' ) -le 0 )
{
$user = $env:USERDOMAIN + '\' + $user
}
$sid = (New-Object System.Security.Principal.NTAccount( $user )).Translate([System.Security.Principal.SecurityIdentifier]).value
if( $sid )
{
$filterHashtable.Add( 'UserId' , $sid )
}
else
{
Throw "Failed to get SID for user $user"
}
}
[hashtable]$eventArguments = @{ 'FilterHashTable' = $filterHashtable }
if( $PSBoundParameters[ 'computerName' ] )
{
$eventArguments.Add( 'ComputerName' , $computerName )
}
[int]$timeOffset = [int]::MaxValue
## Can't add $eventids to filter as makes filter too big
[array]$results = @( Get-WinEvent @eventArguments | . { Process `
{
$event = $PSItem
[bool]$successEvent = $event.Id -in $successEventIds
[bool]$failEvent = ! $successEvent -and $event.Id -in $failEventIds
if( $successEvent -or $failEvent )
{
[string]$username = ([System.Security.Principal.SecurityIdentifier]($event.UserId)).Translate([System.Security.Principal.NTAccount]).Value
if( [string]::IsNullOrEmpty( $username ) )
{
$username = $event.UserId
}
## events for trigger completion only have 5 properties and are in a different order
[int]$startOffset = $(if( $event.Properties.Count -eq 5 ) { 1 } else { 0 } )
## Times are not necessarily local time it seems but not UTC
if( $timeOffset -eq [int]::MaxValue )
{
$timeOffSet = ($event.TimeCreated).Hour - (Get-Date -Date $event.Properties[3 - $startOffSet].Value).Hour
}
[datetime]$eventStart = (Get-Date -Date $event.Properties[0 + $startOffSet].Value).AddHours( $timeOffset )
[datetime]$eventEnd = (Get-Date -Date $event.Properties[3 - $startOffset].Value).AddHours( $timeOffset )
if( $eventStart -gt $eventEnd )
{
[datetime]$swap = $eventStart
$eventStart = $eventEnd
$eventEnd = $swap
}
if( ! $triggerOnly -or ( $triggerOnly -and $startOffset ) )
{
[pscustomobject][ordered]@{
'User' = $username
'Created' = $event.TimeCreated
'Start' = $eventStart
'End' = $eventEnd
'Node' = $event.Properties[1 - $startOffset].Value
'Success' = $(if( $successEvent ) { 'Yes' } else { 'No' })
'Event Id' = $event.Id -as [int]
'Text' = $(if( $event.Properties.Count -gt 5 ) { $event.Properties[2].Value })
'Duration (ms)' = $event.Properties[4 - $startOffset].Value -as [int]
## Where there is an error code, the properties count is 8 rather than 6
'Error Code' = $(if( $event.Properties.Count -gt 6 ) { $event.Properties[5].Value -as [int] })
'Error' = $(if( $event.Properties.Count -gt 7 ) { $event.Properties[6].Value.Trim() })
}
}
}
## else not an event we are interested in
}})
if( $results -and $results.Count )
{
if( $PSBoundParameters[ 'csv' ] )
{
$results | Export-Csv -Path $csv -NoClobber -NoTypeInformation -Delimiter $delimiter
}
elseif( $noGridView )
{
$results
}
else
{
[string]$title = "$($results.Count) events"
if( $filterHashtable[ 'StartTime' ] )
{
$title += " from $(Get-Date -Date $filterHashtable[ 'StartTime' ] -Format G)"
}
if( $filterHashtable[ 'EndTime' ] )
{
$title += " to $(Get-Date -Date $filterHashtable[ 'EndTime' ] -Format G)"
}
if( $PSBoundParameters[ 'user' ] )
{
$title += " for user $user"
}
[array]$selected = @( $results | Out-GridView -Title $title -PassThru )
if( $selected -and $selected.Count )
{
$selected | Set-Clipboard
}
}
}
else
{
Write-Warning "No relevant events found"
}