// 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 { /// /// 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? /// [Cmdlet(VerbsLifecycle.Register, "CimIndicationEvent", DefaultParameterSetName = CimBaseCommand.ClassNameComputerSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=227960")] public class RegisterCimIndicationCommand : ObjectEventRegistrationBase { #region parameters /// /// /// The following is the definition of the input parameter "Namespace". /// Specifies the NameSpace under which to look for the specified class name. /// /// /// Default value is root\cimv2 /// /// [Parameter] public string Namespace { get { return nameSpace; } set { nameSpace = value; } } private string nameSpace; /// /// The following is the definition of the input parameter "ClassName". /// Specifies the Class Name to register the indication on. /// [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; /// /// The following is the definition of the input parameter "Query". /// The Query Expression to pass. /// [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; /// /// /// The following is the definition of the input parameter "QueryDialect". /// Specifies the dialect used by the query Engine that interprets the Query /// string. /// /// [Parameter(ParameterSetName = CimBaseCommand.QueryExpressionComputerSet)] [Parameter(ParameterSetName = CimBaseCommand.QueryExpressionSessionSet)] public string QueryDialect { get { return queryDialect; } set { queryDialect = value; this.SetParameter(value, nameQueryDialect); } } private string queryDialect; /// /// 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. /// [Alias(CimBaseCommand.AliasOT)] [Parameter] public UInt32 OperationTimeoutSec { get { return operationTimeout; } set { operationTimeout = value; } } private UInt32 operationTimeout; /// /// The following is the definition of the input parameter "Session". /// Uses a CimSession context. /// [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; /// /// 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. /// [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 /// /// Returns the object that generates events to be monitored. /// 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.Format(CultureInfo.CurrentCulture, "Select * from {0}", 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; } if (watcher != null) { watcher.SetCmdlet(this); } return watcher; } /// /// Returns the event name to be monitored on the input object. /// protected override string GetSourceObjectEventName() { return "CimIndicationArrived"; } /// /// EndProcessing method. /// 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 += new PSEventUnsubscribedEventHandler(newSubscriber_Unsubscribed); } } /// /// /// Handler to handle unsubscribe event /// /// /// /// private static void newSubscriber_Unsubscribed( object sender, PSEventUnsubscribedEventArgs e) { DebugHelper.WriteLogEx(); CimIndicationWatcher watcher = sender as CimIndicationWatcher; if (watcher != null) { watcher.Stop(); } } #region private members /// /// Check argument value. /// private void CheckArgument() { this.className = ValidationHelper.ValidateArgumentIsValidName(nameClassName, this.className); } /// /// Parameter binder used to resolve parameter set name. /// private ParameterBinder parameterBinder = new ParameterBinder( parameters, parameterSets); /// /// Set the parameter. /// /// 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 /// /// Static parameter definition entries. /// static Dictionary> parameters = new Dictionary> { { nameClassName, new HashSet { new ParameterDefinitionEntry(CimBaseCommand.ClassNameSessionSet, true), new ParameterDefinitionEntry(CimBaseCommand.ClassNameComputerSet, true), } }, { nameQuery, new HashSet { new ParameterDefinitionEntry(CimBaseCommand.QueryExpressionSessionSet, true), new ParameterDefinitionEntry(CimBaseCommand.QueryExpressionComputerSet, true), } }, { nameQueryDialect, new HashSet { new ParameterDefinitionEntry(CimBaseCommand.QueryExpressionSessionSet, false), new ParameterDefinitionEntry(CimBaseCommand.QueryExpressionComputerSet, false), } }, { nameCimSession, new HashSet { new ParameterDefinitionEntry(CimBaseCommand.QueryExpressionSessionSet, true), new ParameterDefinitionEntry(CimBaseCommand.ClassNameSessionSet, true), } }, { nameComputerName, new HashSet { new ParameterDefinitionEntry(CimBaseCommand.QueryExpressionComputerSet, false), new ParameterDefinitionEntry(CimBaseCommand.ClassNameComputerSet, false), } }, }; /// /// Static parameter set entries. /// static Dictionary parameterSets = new Dictionary { { CimBaseCommand.QueryExpressionSessionSet, new ParameterSetEntry(2) }, { CimBaseCommand.QueryExpressionComputerSet, new ParameterSetEntry(1) }, { CimBaseCommand.ClassNameSessionSet, new ParameterSetEntry(2) }, { CimBaseCommand.ClassNameComputerSet, new ParameterSetEntry(1, true) }, }; #endregion } }