Ejemplo n.º 1
0
        /// <summary>
        /// <para>
        /// Base on parametersetName to retrieve <see cref="CimClass"/>
        /// </para>
        /// </summary>
        /// <param name="cmdlet"><see cref="GetCimClassCommand"/> object.</param>
        public void GetCimClass(GetCimClassCommand cmdlet)
        {
            List <CimSessionProxy> proxys = new();
            string nameSpace = ConstValue.GetNamespace(cmdlet.Namespace);
            string className = (cmdlet.ClassName == null) ? @"*" : cmdlet.ClassName;
            CimGetCimClassContext context = new(
                cmdlet.ClassName,
                cmdlet.MethodName,
                cmdlet.PropertyName,
                cmdlet.QualifierName);

            switch (cmdlet.ParameterSetName)
            {
            case CimBaseCommand.ComputerSetName:
            {
                IEnumerable <string> computerNames = ConstValue.GetComputerNames(
                    cmdlet.ComputerName);
                foreach (string computerName in computerNames)
                {
                    CimSessionProxy proxy = CreateSessionProxy(computerName, cmdlet);
                    proxy.ContextObject = context;
                    proxys.Add(proxy);
                }
            }

            break;

            case CimBaseCommand.SessionSetName:
            {
                foreach (CimSession session in cmdlet.CimSession)
                {
                    CimSessionProxy proxy = CreateSessionProxy(session, cmdlet);
                    proxy.ContextObject = context;
                    proxys.Add(proxy);
                }
            }

            break;

            default:
                return;
            }

            if (WildcardPattern.ContainsWildcardCharacters(className))
            {
                // retrieve all classes and then filter based on
                // classname, propertyname, methodname, and qualifiername
                foreach (CimSessionProxy proxy in proxys)
                {
                    proxy.EnumerateClassesAsync(nameSpace);
                }
            }
            else
            {
                foreach (CimSessionProxy proxy in proxys)
                {
                    proxy.GetClassAsync(nameSpace, className);
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CimIndicationWatcher"/> class.
 /// </summary>
 /// <param name="cimSession"></param>
 /// <param name="nameSpace"></param>
 /// <param name="queryExpression"></param>
 /// <param name="operationTimeout"></param>
 public CimIndicationWatcher(
     CimSession cimSession,
     string theNamespace,
     string queryDialect,
     string queryExpression,
     UInt32 operationTimeout)
 {
     ValidationHelper.ValidateNoNullorWhiteSpaceArgument(queryExpression, queryExpressionParameterName);
     ValidationHelper.ValidateNoNullArgument(cimSession, cimSessionParameterName);
     theNamespace = ConstValue.GetNamespace(theNamespace);
     Initialize(null, cimSession, theNamespace, queryDialect, queryExpression, operationTimeout);
 }
        /// <summary>
        /// <para>
        /// Base on parametersetName to retrieve associated ciminstances
        /// </para>
        /// </summary>
        /// <param name="cmdlet"><see cref="GetCimInstanceCommand"/> object.</param>
        public void GetCimAssociatedInstance(GetCimAssociatedInstanceCommand cmdlet)
        {
            IEnumerable <string> computerNames = ConstValue.GetComputerNames(cmdlet.ComputerName);
            // use the namespace from parameter
            string nameSpace = cmdlet.Namespace;

            if ((nameSpace == null) && (cmdlet.ResourceUri == null))
            {
                // try to use namespace of ciminstance, then fall back to default namespace
                nameSpace = ConstValue.GetNamespace(cmdlet.CimInstance.CimSystemProperties.Namespace);
            }

            List <CimSessionProxy> proxys = new();

            switch (cmdlet.ParameterSetName)
            {
            case CimBaseCommand.ComputerSetName:
                foreach (string computerName in computerNames)
                {
                    CimSessionProxy proxy = CreateSessionProxy(computerName, cmdlet.CimInstance, cmdlet);
                    proxys.Add(proxy);
                }

                break;

            case CimBaseCommand.SessionSetName:
                foreach (CimSession session in cmdlet.CimSession)
                {
                    CimSessionProxy proxy = CreateSessionProxy(session, cmdlet);
                    proxys.Add(proxy);
                }

                break;

            default:
                return;
            }

            foreach (CimSessionProxy proxy in proxys)
            {
                proxy.EnumerateAssociatedInstancesAsync(
                    nameSpace,
                    cmdlet.CimInstance,
                    cmdlet.Association,
                    cmdlet.ResultClassName,
                    null,
                    null);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// <para>
        /// Set <see cref="CimSessionProxy"/> properties
        /// </para>
        /// </summary>
        /// <param name="proxy"></param>
        /// <param name="cmdlet"></param>
        private static void SetSessionProxyProperties(
            ref CimSessionProxy proxy,
            CimBaseCommand cmdlet)
        {
            if (cmdlet is GetCimInstanceCommand)
            {
                GetCimInstanceCommand getCimInstance = cmdlet as GetCimInstanceCommand;
                proxy.KeyOnly          = getCimInstance.KeyOnly;
                proxy.Shallow          = getCimInstance.Shallow;
                proxy.OperationTimeout = getCimInstance.OperationTimeoutSec;
                if (getCimInstance.ResourceUri != null)
                {
                    proxy.ResourceUri = getCimInstance.ResourceUri;
                }
            }
            else if (cmdlet is RemoveCimInstanceCommand)
            {
                RemoveCimInstanceCommand removeCimInstance = cmdlet as RemoveCimInstanceCommand;
                proxy.OperationTimeout = removeCimInstance.OperationTimeoutSec;
                if (removeCimInstance.ResourceUri != null)
                {
                    proxy.ResourceUri = removeCimInstance.ResourceUri;
                }

                CimRemoveCimInstanceContext context = new(
                    ConstValue.GetNamespace(removeCimInstance.Namespace),
                    proxy);
                proxy.ContextObject = context;
            }
            else if (cmdlet is SetCimInstanceCommand)
            {
                SetCimInstanceCommand setCimInstance = cmdlet as SetCimInstanceCommand;
                proxy.OperationTimeout = setCimInstance.OperationTimeoutSec;
                if (setCimInstance.ResourceUri != null)
                {
                    proxy.ResourceUri = setCimInstance.ResourceUri;
                }

                CimSetCimInstanceContext context = new(
                    ConstValue.GetNamespace(setCimInstance.Namespace),
                    setCimInstance.Property,
                    proxy,
                    cmdlet.ParameterSetName,
                    setCimInstance.PassThru);
                proxy.ContextObject = context;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// <para>
        /// Base on parametersetName to retrieve ciminstances
        /// </para>
        /// </summary>
        /// <param name="cmdlet"><see cref="GetCimInstanceCommand"/> object.</param>
        public void InvokeCimMethod(InvokeCimMethodCommand cmdlet)
        {
            IEnumerable <string> computerNames = ConstValue.GetComputerNames(cmdlet.ComputerName);
            string nameSpace;
            List <CimSessionProxy> proxys = new();
            string action = string.Format(CultureInfo.CurrentUICulture, actionTemplate, cmdlet.MethodName);

            switch (cmdlet.ParameterSetName)
            {
            case CimBaseCommand.CimInstanceComputerSet:
                foreach (string computerName in computerNames)
                {
                    proxys.Add(CreateSessionProxy(computerName, cmdlet.CimInstance, cmdlet));
                }

                break;

            case CimBaseCommand.ClassNameComputerSet:
            case CimBaseCommand.CimClassComputerSet:
            case CimBaseCommand.ResourceUriComputerSet:
            case CimBaseCommand.QueryComputerSet:
                foreach (string computerName in computerNames)
                {
                    proxys.Add(CreateSessionProxy(computerName, cmdlet));
                }

                break;

            case CimBaseCommand.ClassNameSessionSet:
            case CimBaseCommand.CimClassSessionSet:
            case CimBaseCommand.QuerySessionSet:
            case CimBaseCommand.CimInstanceSessionSet:
            case CimBaseCommand.ResourceUriSessionSet:
                foreach (CimSession session in cmdlet.CimSession)
                {
                    CimSessionProxy proxy = CreateSessionProxy(session, cmdlet);
                    proxys.Add(proxy);
                }

                break;

            default:
                break;
            }

            CimMethodParametersCollection paramsCollection =
                CreateParametersCollection(cmdlet.Arguments, cmdlet.CimClass, cmdlet.CimInstance, cmdlet.MethodName);

            // Invoke methods
            switch (cmdlet.ParameterSetName)
            {
            case CimBaseCommand.ClassNameComputerSet:
            case CimBaseCommand.ClassNameSessionSet:
            case CimBaseCommand.ResourceUriSessionSet:
            case CimBaseCommand.ResourceUriComputerSet:
            {
                string target = string.Format(CultureInfo.CurrentUICulture, targetClass, cmdlet.ClassName);
                if (cmdlet.ResourceUri != null)
                {
                    nameSpace = cmdlet.Namespace;
                }
                else
                {
                    nameSpace = ConstValue.GetNamespace(cmdlet.Namespace);
                }

                foreach (CimSessionProxy proxy in proxys)
                {
                    if (!cmdlet.ShouldProcess(target, action))
                    {
                        return;
                    }

                    proxy.InvokeMethodAsync(
                        nameSpace,
                        cmdlet.ClassName,
                        cmdlet.MethodName,
                        paramsCollection);
                }
            }

            break;

            case CimBaseCommand.CimClassComputerSet:
            case CimBaseCommand.CimClassSessionSet:
            {
                string target = string.Format(CultureInfo.CurrentUICulture, targetClass, cmdlet.CimClass.CimSystemProperties.ClassName);
                nameSpace = ConstValue.GetNamespace(cmdlet.CimClass.CimSystemProperties.Namespace);
                foreach (CimSessionProxy proxy in proxys)
                {
                    if (!cmdlet.ShouldProcess(target, action))
                    {
                        return;
                    }

                    proxy.InvokeMethodAsync(
                        nameSpace,
                        cmdlet.CimClass.CimSystemProperties.ClassName,
                        cmdlet.MethodName,
                        paramsCollection);
                }
            }

            break;

            case CimBaseCommand.QueryComputerSet:
            case CimBaseCommand.QuerySessionSet:
                nameSpace = ConstValue.GetNamespace(cmdlet.Namespace);
                foreach (CimSessionProxy proxy in proxys)
                {
                    // create context object
                    CimInvokeCimMethodContext context = new(
                        nameSpace,
                        cmdlet.MethodName,
                        paramsCollection,
                        proxy);
                    proxy.ContextObject = context;
                    // firstly query instance and then invoke method upon returned instances
                    proxy.QueryInstancesAsync(nameSpace, ConstValue.GetQueryDialectWithDefault(cmdlet.QueryDialect), cmdlet.Query);
                }

                break;

            case CimBaseCommand.CimInstanceComputerSet:
            case CimBaseCommand.CimInstanceSessionSet:
            {
                string target = cmdlet.CimInstance.ToString();
                if (cmdlet.ResourceUri != null)
                {
                    nameSpace = cmdlet.Namespace;
                }
                else
                {
                    nameSpace = ConstValue.GetNamespace(cmdlet.CimInstance.CimSystemProperties.Namespace);
                }

                foreach (CimSessionProxy proxy in proxys)
                {
                    if (!cmdlet.ShouldProcess(target, action))
                    {
                        return;
                    }

                    proxy.InvokeMethodAsync(
                        nameSpace,
                        cmdlet.CimInstance,
                        cmdlet.MethodName,
                        paramsCollection);
                }
            }

            break;

            default:
                break;
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// <para>
        /// Base on parametersetName to retrieve ciminstances
        /// </para>
        /// </summary>
        /// <param name="cmdlet"><see cref="GetCimInstanceCommand"/> object.</param>
        public void RemoveCimInstance(RemoveCimInstanceCommand cmdlet)
        {
            DebugHelper.WriteLogEx();

            IEnumerable <string> computerNames = ConstValue.GetComputerNames(
                GetComputerName(cmdlet));
            List <CimSessionProxy> proxys = new();

            switch (cmdlet.ParameterSetName)
            {
            case CimBaseCommand.CimInstanceComputerSet:
                foreach (string computerName in computerNames)
                {
                    proxys.Add(CreateSessionProxy(computerName, cmdlet.CimInstance, cmdlet));
                }

                break;

            case CimBaseCommand.CimInstanceSessionSet:
                foreach (CimSession session in GetCimSession(cmdlet))
                {
                    proxys.Add(CreateSessionProxy(session, cmdlet));
                }

                break;

            default:
                break;
            }

            switch (cmdlet.ParameterSetName)
            {
            case CimBaseCommand.CimInstanceComputerSet:
            case CimBaseCommand.CimInstanceSessionSet:
                string nameSpace = null;
                if (cmdlet.ResourceUri != null)
                {
                    nameSpace = GetCimInstanceParameter(cmdlet).CimSystemProperties.Namespace;
                }
                else
                {
                    nameSpace = ConstValue.GetNamespace(GetCimInstanceParameter(cmdlet).CimSystemProperties.Namespace);
                }

                string target = cmdlet.CimInstance.ToString();
                foreach (CimSessionProxy proxy in proxys)
                {
                    if (!cmdlet.ShouldProcess(target, action))
                    {
                        return;
                    }

                    proxy.DeleteInstanceAsync(nameSpace, cmdlet.CimInstance);
                }

                break;

            case CimBaseCommand.QueryComputerSet:
            case CimBaseCommand.QuerySessionSet:
                GetCimInstanceInternal(cmdlet);
                break;

            default:
                break;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// <para>
        /// Base on parametersetName to create ciminstances,
        /// either remotely or locally
        /// </para>
        /// </summary>
        /// <param name="cmdlet"><see cref="GetCimInstanceCommand"/> object.</param>
        public void NewCimInstance(NewCimInstanceCommand cmdlet)
        {
            DebugHelper.WriteLogEx();

            string      nameSpace;
            CimInstance cimInstance = null;

            try
            {
                switch (cmdlet.ParameterSetName)
                {
                case CimBaseCommand.ClassNameComputerSet:
                case CimBaseCommand.ClassNameSessionSet:
                {
                    nameSpace   = ConstValue.GetNamespace(cmdlet.Namespace);
                    cimInstance = CreateCimInstance(cmdlet.ClassName,
                                                    nameSpace,
                                                    cmdlet.Key,
                                                    cmdlet.Property,
                                                    cmdlet);
                }

                break;

                case CimBaseCommand.ResourceUriSessionSet:
                case CimBaseCommand.ResourceUriComputerSet:
                {
                    nameSpace   = cmdlet.Namespace;       // passing null is ok for resourceUri set
                    cimInstance = CreateCimInstance("DummyClass",
                                                    nameSpace,
                                                    cmdlet.Key,
                                                    cmdlet.Property,
                                                    cmdlet);
                }

                break;

                case CimBaseCommand.CimClassComputerSet:
                case CimBaseCommand.CimClassSessionSet:
                {
                    nameSpace   = ConstValue.GetNamespace(cmdlet.CimClass.CimSystemProperties.Namespace);
                    cimInstance = CreateCimInstance(cmdlet.CimClass,
                                                    cmdlet.Property,
                                                    cmdlet);
                }

                break;

                default:
                    return;
                }
            }
            catch (ArgumentNullException e)
            {
                cmdlet.ThrowTerminatingError(e, action);
                return;
            }
            catch (ArgumentException e)
            {
                cmdlet.ThrowTerminatingError(e, action);
                return;
            }

            // return if create client only ciminstance
            if (cmdlet.ClientOnly)
            {
                cmdlet.CmdletOperation.WriteObject(cimInstance, null);
                return;
            }

            string target = cimInstance.ToString();

            if (!cmdlet.ShouldProcess(target, action))
            {
                return;
            }

            // create ciminstance on server
            List <CimSessionProxy> proxys = new();

            switch (cmdlet.ParameterSetName)
            {
            case CimBaseCommand.ClassNameComputerSet:
            case CimBaseCommand.CimClassComputerSet:
            case CimBaseCommand.ResourceUriComputerSet:
            {
                IEnumerable <string> computerNames = ConstValue.GetComputerNames(
                    cmdlet.ComputerName);
                foreach (string computerName in computerNames)
                {
                    proxys.Add(CreateSessionProxy(computerName, cmdlet));
                }
            }

            break;

            case CimBaseCommand.CimClassSessionSet:
            case CimBaseCommand.ClassNameSessionSet:
            case CimBaseCommand.ResourceUriSessionSet:
                foreach (CimSession session in cmdlet.CimSession)
                {
                    proxys.Add(CreateSessionProxy(session, cmdlet));
                }

                break;
            }

            foreach (CimSessionProxy proxy in proxys)
            {
                proxy.ContextObject = new CimNewCimInstanceContext(proxy, nameSpace);
                proxy.CreateInstanceAsync(nameSpace, cimInstance);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// <para>
        /// Base on parametersetName to set ciminstances
        /// </para>
        /// </summary>
        /// <param name="cmdlet"><see cref="SetCimInstanceCommand"/> object.</param>
        public void SetCimInstance(SetCimInstanceCommand cmdlet)
        {
            IEnumerable <string> computerNames = ConstValue.GetComputerNames(
                GetComputerName(cmdlet));
            List <CimSessionProxy> proxys = new();

            switch (cmdlet.ParameterSetName)
            {
            case CimBaseCommand.CimInstanceComputerSet:
                foreach (string computerName in computerNames)
                {
                    // create CimSessionProxySetCimInstance object internally
                    proxys.Add(CreateSessionProxy(computerName, cmdlet.CimInstance, cmdlet, cmdlet.PassThru));
                }

                break;

            case CimBaseCommand.CimInstanceSessionSet:
                foreach (CimSession session in GetCimSession(cmdlet))
                {
                    // create CimSessionProxySetCimInstance object internally
                    proxys.Add(CreateSessionProxy(session, cmdlet, cmdlet.PassThru));
                }

                break;

            default:
                break;
            }

            switch (cmdlet.ParameterSetName)
            {
            case CimBaseCommand.CimInstanceComputerSet:
            case CimBaseCommand.CimInstanceSessionSet:
                string nameSpace = ConstValue.GetNamespace(GetCimInstanceParameter(cmdlet).CimSystemProperties.Namespace);
                string target    = cmdlet.CimInstance.ToString();
                foreach (CimSessionProxy proxy in proxys)
                {
                    if (!cmdlet.ShouldProcess(target, action))
                    {
                        return;
                    }

                    Exception   exception = null;
                    CimInstance instance  = cmdlet.CimInstance;
                    // For CimInstance parameter sets, Property is an optional parameter
                    if (cmdlet.Property != null)
                    {
                        if (!SetProperty(cmdlet.Property, ref instance, ref exception))
                        {
                            cmdlet.ThrowTerminatingError(exception, action);
                            return;
                        }
                    }

                    proxy.ModifyInstanceAsync(nameSpace, instance);
                }

                break;

            case CimBaseCommand.QueryComputerSet:
            case CimBaseCommand.QuerySessionSet:
                GetCimInstanceInternal(cmdlet);
                break;

            default:
                break;
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// <para>
        /// Refactor to be reused by Get-CimInstance;Remove-CimInstance;Set-CimInstance
        /// </para>
        /// </summary>
        /// <param name="cmdlet"></param>
        protected void GetCimInstanceInternal(CimBaseCommand cmdlet)
        {
            IEnumerable <string> computerNames = ConstValue.GetComputerNames(
                GetComputerName(cmdlet));
            string nameSpace;
            List <CimSessionProxy> proxys       = new();
            bool        isGetCimInstanceCommand = cmdlet is GetCimInstanceCommand;
            CimInstance targetCimInstance       = null;

            switch (cmdlet.ParameterSetName)
            {
            case CimBaseCommand.CimInstanceComputerSet:
                foreach (string computerName in computerNames)
                {
                    targetCimInstance = GetCimInstanceParameter(cmdlet);
                    CimSessionProxy proxy = CreateSessionProxy(computerName, targetCimInstance, cmdlet);
                    if (isGetCimInstanceCommand)
                    {
                        SetPreProcess(proxy, cmdlet as GetCimInstanceCommand);
                    }

                    proxys.Add(proxy);
                }

                break;

            case CimBaseCommand.ClassNameComputerSet:
            case CimBaseCommand.QueryComputerSet:
            case CimBaseCommand.ResourceUriComputerSet:
                foreach (string computerName in computerNames)
                {
                    CimSessionProxy proxy = CreateSessionProxy(computerName, cmdlet);
                    if (isGetCimInstanceCommand)
                    {
                        SetPreProcess(proxy, cmdlet as GetCimInstanceCommand);
                    }

                    proxys.Add(proxy);
                }

                break;

            case CimBaseCommand.ClassNameSessionSet:
            case CimBaseCommand.CimInstanceSessionSet:
            case CimBaseCommand.QuerySessionSet:
            case CimBaseCommand.ResourceUriSessionSet:
                foreach (CimSession session in GetCimSession(cmdlet))
                {
                    CimSessionProxy proxy = CreateSessionProxy(session, cmdlet);
                    if (isGetCimInstanceCommand)
                    {
                        SetPreProcess(proxy, cmdlet as GetCimInstanceCommand);
                    }

                    proxys.Add(proxy);
                }

                break;

            default:
                break;
            }

            switch (cmdlet.ParameterSetName)
            {
            case CimBaseCommand.ClassNameComputerSet:
            case CimBaseCommand.ClassNameSessionSet:
                nameSpace = ConstValue.GetNamespace(GetNamespace(cmdlet));
                if (IsClassNameQuerySet(cmdlet))
                {
                    string query = CreateQuery(cmdlet);
                    DebugHelper.WriteLogEx(@"Query = {0}", 1, query);
                    foreach (CimSessionProxy proxy in proxys)
                    {
                        proxy.QueryInstancesAsync(nameSpace,
                                                  ConstValue.GetQueryDialectWithDefault(GetQueryDialect(cmdlet)),
                                                  query);
                    }
                }
                else
                {
                    foreach (CimSessionProxy proxy in proxys)
                    {
                        proxy.EnumerateInstancesAsync(nameSpace, GetClassName(cmdlet));
                    }
                }

                break;

            case CimBaseCommand.CimInstanceComputerSet:
            case CimBaseCommand.CimInstanceSessionSet:
            {
                CimInstance instance = GetCimInstanceParameter(cmdlet);
                nameSpace = ConstValue.GetNamespace(instance.CimSystemProperties.Namespace);
                foreach (CimSessionProxy proxy in proxys)
                {
                    proxy.GetInstanceAsync(nameSpace, instance);
                }
            }

            break;

            case CimBaseCommand.QueryComputerSet:
            case CimBaseCommand.QuerySessionSet:
                nameSpace = ConstValue.GetNamespace(GetNamespace(cmdlet));
                foreach (CimSessionProxy proxy in proxys)
                {
                    proxy.QueryInstancesAsync(nameSpace,
                                              ConstValue.GetQueryDialectWithDefault(GetQueryDialect(cmdlet)),
                                              GetQuery(cmdlet));
                }

                break;

            case CimBaseCommand.ResourceUriSessionSet:
            case CimBaseCommand.ResourceUriComputerSet:
                foreach (CimSessionProxy proxy in proxys)
                {
                    proxy.EnumerateInstancesAsync(GetNamespace(cmdlet), GetClassName(cmdlet));
                }

                break;

            default:
                break;
            }
        }