forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetEventSubscriberCommand.cs
More file actions
132 lines (113 loc) · 4.35 KB
/
Copy pathGetEventSubscriberCommand.cs
File metadata and controls
132 lines (113 loc) · 4.35 KB
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
//
// Copyright (C) Microsoft. All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Management.Automation;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Lists all event subscribers.
/// </summary>
[Cmdlet(VerbsCommon.Get, "EventSubscriber", DefaultParameterSetName = "BySource", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135155")]
[OutputType(typeof(PSEventSubscriber))]
public class GetEventSubscriberCommand : PSCmdlet
{
#region parameters
/// <summary>
/// An identifier for this event subscription
/// </summary>
[Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = "BySource")]
[ValidateNotNullOrEmpty()]
public string SourceIdentifier
{
get
{
return _sourceIdentifier;
}
set
{
_sourceIdentifier = value;
if (value != null)
{
_matchPattern = WildcardPattern.Get(value, WildcardOptions.IgnoreCase);
}
}
}
private string _sourceIdentifier = null;
/// <summary>
/// An identifier for this event subscription
/// </summary>
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = "ById")]
[Alias("Id")]
public int SubscriptionId { get; set; } = -1;
/// <summary>
/// Also show supporting events
/// </summary>
[Parameter(Position = 1)]
public SwitchParameter Force { get; set; }
#endregion parameters
private WildcardPattern _matchPattern;
/// <summary>
/// Get the subscribers
/// </summary>
protected override void ProcessRecord()
{
bool foundMatch = false;
// Go through all the received events and write them to the output
// pipeline
List<PSEventSubscriber> subscribers = new List<PSEventSubscriber>(Events.Subscribers);
foreach (PSEventSubscriber subscriber in subscribers)
{
// If they specified a event identifier and we don't match, continue
if ((_sourceIdentifier != null) &&
(!_matchPattern.IsMatch(subscriber.SourceIdentifier)))
{
continue;
}
// If they specified a subscription identifier and we don't match, continue
if ((SubscriptionId >= 0) &&
(subscriber.SubscriptionId != SubscriptionId))
{
continue;
}
// Don't display support events by default
if (subscriber.SupportEvent && (!Force))
{
continue;
}
WriteObject(subscriber);
foundMatch = true;
}
// Generate an error if we couldn't find the subscription identifier,
// and no globbing was done.
if (!foundMatch)
{
bool lookingForSource = (_sourceIdentifier != null) &&
(!WildcardPattern.ContainsWildcardCharacters(_sourceIdentifier));
bool lookingForId = (SubscriptionId >= 0);
if (lookingForSource || lookingForId)
{
object identifier = null;
string error = null;
if (lookingForSource)
{
identifier = _sourceIdentifier;
error = EventingStrings.EventSubscriptionSourceNotFound;
}
else if (lookingForId)
{
identifier = SubscriptionId;
error = EventingStrings.EventSubscriptionNotFound;
}
ErrorRecord errorRecord = new ErrorRecord(
new ArgumentException(String.Format(System.Globalization.CultureInfo.CurrentCulture, error, identifier)),
"INVALID_SOURCE_IDENTIFIER",
ErrorCategory.InvalidArgument,
null);
WriteError(errorRecord);
}
}
}
}
}