forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWmiActivities.cs
More file actions
242 lines (212 loc) · 9.73 KB
/
Copy pathWmiActivities.cs
File metadata and controls
242 lines (212 loc) · 9.73 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
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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
#pragma warning disable 1634, 1691
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Activities;
using System.Management;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Tracing;
using System.IO;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Globalization;
namespace Microsoft.PowerShell.Activities
{
/// <summary>
/// Workflow activity wrapping the Get-Wmiobject cmdlet
/// </summary>
public sealed class GetWmiObject : WmiActivity
{
/// <summary>
/// Sets the default display name of the activity
/// </summary>
public GetWmiObject()
{
this.DisplayName = "Get-WmiObject";
}
/// <summary>
/// Specifies the name of a WMI class. When this parameter is used, the cmdlet
/// retrieves instances of the WMI class.
/// </summary>summary>
[BehaviorCategory]
[DefaultValue(null)]
[OverloadGroup("Class")]
public InArgument<string> Class { get; set; }
/// <summary>
/// Specifies the WMI class property or set of properties to retrieve.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
public InArgument<string[]> Property { get; set; }
/// <summary>
/// Specifies a Where clause to use as a filter. Uses the syntax of the WMI Query Language (WQL).
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[OverloadGroup("Class")]
public InArgument<string> Filter { get; set; }
/// <summary>
/// Specifies a WMI Query Language (WQL) statement to run. Event queries are not supported by this parameter.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[OverloadGroup("Query")]
public InArgument<string> Query { get; set; }
/// <summary>
/// Indicates whether the objects that are returned from WMI should contain amended
/// information. Typically, amended information is localizable information, such as object
/// and property descriptions, that is attached to the WMI object.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
public bool Amended { get; set; }
/// <summary>
/// Specifies whether direct access to the WMI provider is requested for the specified
/// class without any regard to its base class or to its derived classes.
///</summary>
[BehaviorCategory]
[DefaultValue(null)]
public bool DirectRead { get; set; }
/// <summary>
/// Execute the logic for the activity
/// </summary>
/// <param name="context">The native activity context to use in this activity</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context)
{
System.Management.Automation.PowerShell command;
command = GetWmiCommandCore(context, "Get-WmiObject");
if (Class.Get(context) != null)
{
command.AddParameter("Class", Class.Get(context));
Tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Setting parameter {1} to {2}.",
context.ActivityInstanceId, "Class", Class.Get(context)));
}
if (Property.Get(context) != null)
{
command.AddParameter("Property", Property.Get(context));
Tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Setting parameter {1} to {2}.",
context.ActivityInstanceId, "Property", Property.Get(context)));
}
if (Filter.Get(context) != null)
{
command.AddParameter("Filter", Filter.Get(context));
Tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Setting parameter {1} to {2}.",
context.ActivityInstanceId, "Filter", Filter.Get(context)));
}
if (Amended)
{
command.AddParameter("Amended", Amended);
Tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Setting parameter {1} to {2}.",
context.ActivityInstanceId, "Amended", Amended));
}
if (DirectRead)
{
command.AddParameter("DirectRead", DirectRead);
Tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Setting parameter {1} to {2}.",
context.ActivityInstanceId, "DirectRead", DirectRead));
}
if (Query.Get(context) != null)
{
command.AddParameter("Query", Query.Get(context));
Tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Setting parameter {1} to {2}.",
context.ActivityInstanceId, "Query", Query.Get(context)));
}
return new ActivityImplementationContext() { PowerShellInstance = command };
}
}
/// <summary>
/// Wraps the Invoke-WmiMethod cmdlet
/// </summary>
public sealed class InvokeWmiMethod : WmiActivity
{
/// <summary>
/// Sets the default display name of the activity
/// </summary>
public InvokeWmiMethod()
{
this.DisplayName = "Invoke-WmiMethod";
}
/// <summary>
/// A WMI path specification which should be of the form "Win32_Printer.DeviceID='TestPrinter'"
/// this will select instances of objects on which to call the method.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[OverloadGroup("path")]
public InArgument<string> Path { get; set; }
/// <summary>
/// The name of the WMI class to use for when static methods.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[OverloadGroup("class")]
public InArgument<string> Class { get; set; }
/// <summary>
/// THe name of the instance or static method to call
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
public InArgument<string> Name { get; set; }
/// <summary>
/// The collection of arguments to use when calling the method
/// </summary>
[BehaviorCategory]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<PSDataCollection<PSObject>> ArgumentList { get; set; }
/// <summary>
/// Implements the logic of this activity
/// </summary>
/// <param name="context">The activity context to refer to</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context)
{
System.Management.Automation.PowerShell command;
command = GetWmiCommandCore(context, "Invoke-WmiMethod");
if (!String.IsNullOrEmpty(Path.Get(context)))
{
command.AddParameter("Path", Path.Get(context));
Tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Setting parameter {1} to {2}.",
context.ActivityInstanceId, "Path", Path.Get(context)));
}
if (!String.IsNullOrEmpty(Class.Get(context)))
{
command.AddParameter("Class", Class.Get(context));
Tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Setting parameter {1} to {2}.",
context.ActivityInstanceId, "Class", Class.Get(context)));
}
if (!String.IsNullOrEmpty(Name.Get(context)))
{
command.AddParameter("Name", Name.Get(context));
Tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture,
"PowerShell activity ID={0}: Setting parameter {1} to {2}.",
context.ActivityInstanceId, "Name", Name.Get(context)));
}
if (ArgumentList.Get(context) != null)
{
Collection<PSObject> argCollection = ArgumentList.Get(context).ReadAll();
if (argCollection.Count > 0)
{
command.AddParameter("ArgumentList", argCollection);
Tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture,
"PowerShell activity ID={0}: Setting parameter {1} to '{2}'.",
context.ActivityInstanceId, "ArgumentList", string.Join("','", argCollection)));
}
}
return new ActivityImplementationContext() { PowerShellInstance = command };
}
}
}