File size: 12,903 Bytes
8c763fb | 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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#region Using directives
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Management.Automation;
using Microsoft.PowerShell.Commands;
#endregion
namespace Microsoft.Management.Infrastructure.CimCmdlets
{
/// <summary>
/// Enables the user to subscribe to indications using Filter Expression or
/// Query Expression.
/// -SourceIdentifier is a name given to the subscription
/// The Cmdlet should return a PS EventSubscription object that can be used to
/// cancel the subscription
/// Should we have the second parameter set with a -Query?
/// </summary>
[Alias("rcie")]
[Cmdlet(VerbsLifecycle.Register, "CimIndicationEvent", DefaultParameterSetName = CimBaseCommand.ClassNameComputerSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=227960")]
public class RegisterCimIndicationCommand : ObjectEventRegistrationBase
{
#region parameters
/// <summary>
/// <para>
/// The following is the definition of the input parameter "Namespace".
/// Specifies the NameSpace under which to look for the specified class name.
/// </para>
/// <para>
/// Default value is root\cimv2
/// </para>
/// </summary>
[Parameter]
public string Namespace { get; set; }
/// <summary>
/// The following is the definition of the input parameter "ClassName".
/// Specifies the Class Name to register the indication on.
/// </summary>
[Parameter(
Mandatory = true,
Position = 0,
ParameterSetName = CimBaseCommand.ClassNameSessionSet)]
[Parameter(Mandatory = true,
Position = 0,
ParameterSetName = CimBaseCommand.ClassNameComputerSet)]
public string ClassName
{
get
{
return className;
}
set
{
className = value;
this.SetParameter(value, nameClassName);
}
}
private string className;
/// <summary>
/// The following is the definition of the input parameter "Query".
/// The Query Expression to pass.
/// </summary>
[Parameter(
Mandatory = true,
Position = 0,
ParameterSetName = CimBaseCommand.QueryExpressionSessionSet)]
[Parameter(
Mandatory = true,
Position = 0,
ParameterSetName = CimBaseCommand.QueryExpressionComputerSet)]
public string Query
{
get
{
return query;
}
set
{
query = value;
this.SetParameter(value, nameQuery);
}
}
private string query;
/// <summary>
/// <para>
/// The following is the definition of the input parameter "QueryDialect".
/// Specifies the dialect used by the query Engine that interprets the Query
/// string.
/// </para>
/// </summary>
[Parameter(ParameterSetName = CimBaseCommand.QueryExpressionComputerSet)]
[Parameter(ParameterSetName = CimBaseCommand.QueryExpressionSessionSet)]
public string QueryDialect
{
get
{
return queryDialect;
}
set
{
queryDialect = value;
this.SetParameter(value, nameQueryDialect);
}
}
private string queryDialect;
/// <summary>
/// The following is the definition of the input parameter "OperationTimeoutSec".
/// Enables the user to specify the operation timeout in Seconds. This value
/// overwrites the value specified by the CimSession Operation timeout.
/// </summary>
[Alias(CimBaseCommand.AliasOT)]
[Parameter]
public uint OperationTimeoutSec { get; set; }
/// <summary>
/// The following is the definition of the input parameter "Session".
/// Uses a CimSession context.
/// </summary>
[Parameter(
Mandatory = true,
ParameterSetName = CimBaseCommand.QueryExpressionSessionSet)]
[Parameter(
Mandatory = true,
ParameterSetName = CimBaseCommand.ClassNameSessionSet)]
public CimSession CimSession
{
get
{
return cimSession;
}
set
{
cimSession = value;
this.SetParameter(value, nameCimSession);
}
}
private CimSession cimSession;
/// <summary>
/// The following is the definition of the input parameter "ComputerName".
/// Specifies the computer on which the commands associated with this session
/// will run. The default value is LocalHost.
/// </summary>
[Alias(CimBaseCommand.AliasCN, CimBaseCommand.AliasServerName)]
[Parameter(ParameterSetName = CimBaseCommand.QueryExpressionComputerSet)]
[Parameter(ParameterSetName = CimBaseCommand.ClassNameComputerSet)]
public string ComputerName
{
get
{
return computername;
}
set
{
computername = value;
this.SetParameter(value, nameComputerName);
}
}
private string computername;
#endregion
/// <summary>
/// Returns the object that generates events to be monitored.
/// </summary>
protected override object GetSourceObject()
{
CimIndicationWatcher watcher = null;
string parameterSetName = null;
try
{
parameterSetName = this.parameterBinder.GetParameterSet();
}
finally
{
this.parameterBinder.reset();
}
string tempQueryExpression = string.Empty;
switch (parameterSetName)
{
case CimBaseCommand.QueryExpressionSessionSet:
case CimBaseCommand.QueryExpressionComputerSet:
tempQueryExpression = this.Query;
break;
case CimBaseCommand.ClassNameSessionSet:
case CimBaseCommand.ClassNameComputerSet:
// validate the classname
this.CheckArgument();
tempQueryExpression = string.Create(CultureInfo.CurrentCulture, $"Select * from {this.ClassName}");
break;
}
switch (parameterSetName)
{
case CimBaseCommand.QueryExpressionSessionSet:
case CimBaseCommand.ClassNameSessionSet:
{
watcher = new CimIndicationWatcher(this.CimSession, this.Namespace, this.QueryDialect, tempQueryExpression, this.OperationTimeoutSec);
}
break;
case CimBaseCommand.QueryExpressionComputerSet:
case CimBaseCommand.ClassNameComputerSet:
{
watcher = new CimIndicationWatcher(this.ComputerName, this.Namespace, this.QueryDialect, tempQueryExpression, this.OperationTimeoutSec);
}
break;
}
watcher?.SetCmdlet(this);
return watcher;
}
/// <summary>
/// Returns the event name to be monitored on the input object.
/// </summary>
protected override string GetSourceObjectEventName()
{
return "CimIndicationArrived";
}
/// <summary>
/// EndProcessing method.
/// </summary>
protected override void EndProcessing()
{
DebugHelper.WriteLogEx();
base.EndProcessing();
// Register for the "Unsubscribed" event so that we can stop the
// Cimindication event watcher.
PSEventSubscriber newSubscriber = NewSubscriber;
if (newSubscriber != null)
{
DebugHelper.WriteLog("RegisterCimIndicationCommand::EndProcessing subscribe to Unsubscribed event", 4);
newSubscriber.Unsubscribed += newSubscriber_Unsubscribed;
}
}
/// <summary>
/// <para>
/// Handler to handle unsubscribe event
/// </para>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void newSubscriber_Unsubscribed(
object sender, PSEventUnsubscribedEventArgs e)
{
DebugHelper.WriteLogEx();
CimIndicationWatcher watcher = sender as CimIndicationWatcher;
watcher?.Stop();
}
#region private members
/// <summary>
/// Check argument value.
/// </summary>
private void CheckArgument()
{
this.className = ValidationHelper.ValidateArgumentIsValidName(nameClassName, this.className);
}
/// <summary>
/// Parameter binder used to resolve parameter set name.
/// </summary>
private readonly ParameterBinder parameterBinder = new(
parameters, parameterSets);
/// <summary>
/// Set the parameter.
/// </summary>
/// <param name="parameterName"></param>
private void SetParameter(object value, string parameterName)
{
if (value == null)
{
return;
}
this.parameterBinder.SetParameter(parameterName, true);
}
#region const string of parameter names
internal const string nameClassName = "ClassName";
internal const string nameQuery = "Query";
internal const string nameQueryDialect = "QueryDialect";
internal const string nameCimSession = "CimSession";
internal const string nameComputerName = "ComputerName";
#endregion
/// <summary>
/// Static parameter definition entries.
/// </summary>
private static readonly Dictionary<string, HashSet<ParameterDefinitionEntry>> parameters = new()
{
{
nameClassName, new HashSet<ParameterDefinitionEntry> {
new ParameterDefinitionEntry(CimBaseCommand.ClassNameSessionSet, true),
new ParameterDefinitionEntry(CimBaseCommand.ClassNameComputerSet, true),
}
},
{
nameQuery, new HashSet<ParameterDefinitionEntry> {
new ParameterDefinitionEntry(CimBaseCommand.QueryExpressionSessionSet, true),
new ParameterDefinitionEntry(CimBaseCommand.QueryExpressionComputerSet, true),
}
},
{
nameQueryDialect, new HashSet<ParameterDefinitionEntry> {
new ParameterDefinitionEntry(CimBaseCommand.QueryExpressionSessionSet, false),
new ParameterDefinitionEntry(CimBaseCommand.QueryExpressionComputerSet, false),
}
},
{
nameCimSession, new HashSet<ParameterDefinitionEntry> {
new ParameterDefinitionEntry(CimBaseCommand.QueryExpressionSessionSet, true),
new ParameterDefinitionEntry(CimBaseCommand.ClassNameSessionSet, true),
}
},
{
nameComputerName, new HashSet<ParameterDefinitionEntry> {
new ParameterDefinitionEntry(CimBaseCommand.QueryExpressionComputerSet, false),
new ParameterDefinitionEntry(CimBaseCommand.ClassNameComputerSet, false),
}
},
};
/// <summary>
/// Static parameter set entries.
/// </summary>
private static readonly Dictionary<string, ParameterSetEntry> parameterSets = new()
{
{ CimBaseCommand.QueryExpressionSessionSet, new ParameterSetEntry(2) },
{ CimBaseCommand.QueryExpressionComputerSet, new ParameterSetEntry(1) },
{ CimBaseCommand.ClassNameSessionSet, new ParameterSetEntry(2) },
{ CimBaseCommand.ClassNameComputerSet, new ParameterSetEntry(1, true) },
};
#endregion
}
}
|