forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraceCommandBase.cs
More file actions
134 lines (120 loc) · 4.7 KB
/
Copy pathTraceCommandBase.cs
File metadata and controls
134 lines (120 loc) · 4.7 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
133
134
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using Dbg = System.Management.Automation.Diagnostics;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// A base class for cmdlets that has helper methods for globbing
/// trace source instances
/// </summary>
public class TraceCommandBase : PSCmdlet
{
/// <summary>
/// Gets the matching PSTraceSource instances for the
/// specified patterns.
/// </summary>
///
/// <param name="patternsToMatch">
/// The patterns used to match the PSTraceSource name.
/// </param>
///
/// <param name="writeErrorIfMatchNotFound">
/// If true and the pattern does not contain wildcard patterns and no
/// match is found, then WriteError will be called.
/// </param>
///
/// <returns>
/// A collection of the matching PSTraceSource instances.
/// </returns>
///
internal Collection<PSTraceSource> GetMatchingTraceSource(
string[] patternsToMatch,
bool writeErrorIfMatchNotFound)
{
Collection<string> ignored = null;
return GetMatchingTraceSource(patternsToMatch, writeErrorIfMatchNotFound, out ignored);
}
/// <summary>
/// Gets the matching PSTraceSource instances for the
/// specified patterns.
/// </summary>
///
/// <param name="patternsToMatch">
/// The patterns used to match the PSTraceSource name.
/// </param>
///
/// <param name="writeErrorIfMatchNotFound">
/// If true and the pattern does not contain wildcard patterns and no
/// match is found, then WriteError will be called.
/// </param>
///
/// <param name="notMatched">
/// The patterns for which a match was not found.
/// </param>
///
/// <returns>
/// A collection of the matching PSTraceSource instances.
/// </returns>
///
internal Collection<PSTraceSource> GetMatchingTraceSource(
string[] patternsToMatch,
bool writeErrorIfMatchNotFound,
out Collection<string> notMatched)
{
notMatched = new Collection<string>();
Collection<PSTraceSource> results = new Collection<PSTraceSource>();
foreach (string patternToMatch in patternsToMatch)
{
bool matchFound = false;
if (String.IsNullOrEmpty(patternToMatch))
{
notMatched.Add(patternToMatch);
continue;
}
WildcardPattern pattern =
WildcardPattern.Get(
patternToMatch,
WildcardOptions.IgnoreCase);
Dictionary<String, PSTraceSource> traceCatalog = PSTraceSource.TraceCatalog;
foreach (PSTraceSource source in traceCatalog.Values)
{
// Try matching by full name
if (pattern.IsMatch(source.FullName))
{
matchFound = true;
results.Add(source);
}
// Try matching by the short name.
else if (pattern.IsMatch(source.Name))
{
matchFound = true;
results.Add(source);
}
}
if (!matchFound)
{
notMatched.Add(patternToMatch);
// Only write an error if no match was found, the pattern doesn't
// contain wildcard characters, and caller wants us to.
if (writeErrorIfMatchNotFound &&
!WildcardPattern.ContainsWildcardCharacters(patternToMatch))
{
ItemNotFoundException itemNotFound =
new ItemNotFoundException(
patternToMatch,
"TraceSourceNotFound",
SessionStateStrings.TraceSourceNotFound);
ErrorRecord errorRecord = new ErrorRecord(itemNotFound.ErrorRecord, itemNotFound);
WriteError(errorRecord);
}
}
}
return results;
}
}
}