forked from StartAutomating/Eventful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReceive-Event.ps1
153 lines (141 loc) · 5.6 KB
/
Receive-Event.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
function Receive-Event
{
<#
.Synopsis
Receives Events
.Description
Receives Events and output from Event Subscriptions.
.Link
Send-Event
.Link
Watch-Event
.Example
Get-EventSource -Subscriber | Receive-Event
.Example
Receive-Event -SourceIdentifier * -First 1 # Receives the most recent event with any source identifier.
#>
[CmdletBinding(DefaultParameterSetName='Instance')]
[OutputType([PSObject], [Management.Automation.PSEventArgs])]
param(
# The event subscription ID.
[Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='SubscriptionID')]
[int[]]
$SubscriptionID,
# The event ID.
[Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='EventIdentifier')]
[int[]]
$EventIdentifier,
# The event source identifier.
[Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='SourceIdentifier')]
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='SubscriptionID')]
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='EventIdentifier')]
[string[]]
$SourceIdentifier,
# If provided, will return the first N events
[int]
$First,
# If provided, will skip the first N events.
[int]
$Skip,
# The input object.
# If the Input Object was a job, it will receive the results of the job.
[Parameter(ValueFromPipeline)]
[PSObject]
$InputObject,
# If set, will remove events from the system after they have been returned,
# and will not keep results from Jobs or Event Handlers.
[switch]
$Clear
)
begin {
#region Prepare Accumulation
# We will accumulate the events we output in case we need to -Clear them.
$accumulated = [Collections.Arraylist]::new()
filter accumulate {
if (-not $skip -or ($accumulated.Count -ge $skip)) {
$_
}
if (-not $First -or ($accumulated.Count -lt ($First + $skip))) {
$null = $accumulated.Add($_)
}
}
#endregion Prepare Accumulation
}
process {
#region Passthru Events
if ($PSCmdlet.ParameterSetName -eq 'EventIdentifier' -and
$_ -is [Management.Automation.PSEventArgs]) {
$_ | accumulate # pass events thru and accumulate them for later.
return
}
#endregion PassThru Events
#region Receiving Events by SourceIdentifier
if ($PSCmdlet.ParameterSetName -in 'SourceIdentifier', 'EventIdentifier') {
:nextEvent for ($ec = $PSCmdlet.Events.ReceivedEvents.Count -1 ; $ec -ge 0; $ec--) {
$evt = $PSCmdlet.Events.ReceivedEvents[$ec]
if ($SourceIdentifier) {
foreach ($sid in $sourceIdentifier) {
if ($evt.SourceIdentifier -eq $sid -or $evt.SourceIdentifier -like $sid) {
$evt | accumulate
}
if ($First -and $accumulated.Count -ge ($First + $Skip)) {
break nextEvent
}
}
}
if ($EventIdentifier) {
foreach ($eid in $EventIdentifier) {
if ($evt.EventIdentifier -eq $eid) {
$evt | accumulate
}
if ($First -and $accumulated.Count -ge ($First + $Skip)) {
break nextEvent
}
}
}
}
return
}
#endregion Receiving Events by SourceIdentifier
#region Receiving Events by SubscriptionID
if ($PSCmdlet.ParameterSetName -eq 'SubscriptionID') {
$SubscriptionID |
# Find all event subscriptions with that subscription ID
Get-EventSubscriber -SubscriptionId { $_ } -ErrorAction SilentlyContinue |
# that have an .Action.
Where-Object { $_.Action } |
# Then pipe that action to
Select-Object -ExpandProperty Action |
# Receive-Job (-Keep results by default unless -Clear is passed).
Receive-Job -Keep:(-not $Clear)
return
}
#endregion Receiving Events by SubscriptionID
#region Receiving Events by InputObject
if ($InputObject) { # If the input object was a job,
if ($InputObject -is [Management.Automation.Job]) {
# Receive-Job (-Keep results by default unless -Clear is passed).
$InputObject | Receive-Job -Keep:(-not $Clear)
} else {
# Otherwise, find event subscribers
Get-EventSubscriber |
# whose source is this input object
Where-Object Source -EQ $InputObject |
# that have an .Action.
Where-Object { $_.Action } |
# Then pipe that action to
Select-Object -ExpandProperty Action |
# Receive-Job (-Keep results by default unless -Clear is passed).
Receive-Job -Keep:(-not $Clear)
}
}
#endregion Receiving Events by InputObject
}
end {
#region -Clear accumulation (if requested)
if ($accumulated.Count -and $Clear) { # If we accumulated events, and said to -Clear them,
$accumulated | Remove-Event # remove those events.
}
#region -Clear accumulation (if requested)
}
}