Пример #1
0
        public void InvokeCimMethodOnCimInstance(CimInstance cimInstance, XOperationContextBase context, CmdletOperationBase operation)
        {
            DebugHelper.WriteLogEx();
            CimInvokeCimMethod.CimInvokeCimMethodContext cimInvokeCimMethodContext = context as CimInvokeCimMethod.CimInvokeCimMethodContext;
            object[] methodName = new object[1];
            methodName[0] = cimInvokeCimMethodContext.MethodName;
            string str = string.Format(CultureInfo.CurrentUICulture, "Invoke-CimMethod: {0}", methodName);

            if (operation.ShouldProcess(cimInstance.ToString(), str))
            {
                CimSessionProxy cimSessionProxy = base.CreateCimSessionProxy(cimInvokeCimMethodContext.Proxy);
                cimSessionProxy.InvokeMethodAsync(cimInvokeCimMethodContext.Namespace, cimInstance, cimInvokeCimMethodContext.MethodName, cimInvokeCimMethodContext.ParametersCollection);
                return;
            }
            else
            {
                return;
            }
        }
        /// <summary>
        /// <para>
        /// Invoke cimmethod on given <see cref="CimInstance"/>
        /// </para>
        /// </summary>
        /// <param name="cimInstance"></param>
        public void InvokeCimMethodOnCimInstance(CimInstance cimInstance, XOperationContextBase context, CmdletOperationBase operation)
        {
            DebugHelper.WriteLogEx();
            CimInvokeCimMethodContext cimInvokeCimMethodContext = context as CimInvokeCimMethodContext;

            Debug.Assert(cimInvokeCimMethodContext != null, "CimInvokeCimMethod::InvokeCimMethodOnCimInstance should has CimInvokeCimMethodContext != NULL.");

            string action = string.Format(CultureInfo.CurrentUICulture, actionTemplate, cimInvokeCimMethodContext.MethodName);

            if (!operation.ShouldProcess(cimInstance.ToString(), action))
            {
                return;
            }

            CimSessionProxy proxy = CreateCimSessionProxy(cimInvokeCimMethodContext.Proxy);

            proxy.InvokeMethodAsync(
                cimInvokeCimMethodContext.Namespace,
                cimInstance,
                cimInvokeCimMethodContext.MethodName,
                cimInvokeCimMethodContext.ParametersCollection);
        }
        /// <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;
            }
        }
Пример #4
0
        public void InvokeCimMethod(InvokeCimMethodCommand cmdlet)
        {
            string @namespace;
            IEnumerable <string>   computerNames     = ConstValue.GetComputerNames(cmdlet.ComputerName);
            List <CimSessionProxy> cimSessionProxies = new List <CimSessionProxy>();

            object[] methodName = new object[1];
            methodName[0] = cmdlet.MethodName;
            string str = string.Format(CultureInfo.CurrentUICulture, "Invoke-CimMethod: {0}", methodName);
            string parameterSetName = cmdlet.ParameterSetName;
            string str1             = parameterSetName;

            if (parameterSetName != null)
            {
                switch (str1)
                {
                case "CimInstanceComputerSet":
                {
                    IEnumerator <string> enumerator = computerNames.GetEnumerator();
                    using (enumerator)
                    {
                        while (enumerator.MoveNext())
                        {
                            string current = enumerator.Current;
                            cimSessionProxies.Add(this.CreateSessionProxy(current, cmdlet.CimInstance, cmdlet));
                        }
                    }
                }
                break;

                case "ClassNameComputerSet":
                case "CimClassComputerSet":
                case "ResourceUriComputerSet":
                case "QueryComputerSet":
                {
                    IEnumerator <string> enumerator1 = computerNames.GetEnumerator();
                    using (enumerator1)
                    {
                        while (enumerator1.MoveNext())
                        {
                            string current1 = enumerator1.Current;
                            cimSessionProxies.Add(this.CreateSessionProxy(current1, cmdlet));
                        }
                    }
                }
                break;

                case "ClassNameSessionSet":
                case "CimClassSessionSet":
                case "QuerySessionSet":
                case "CimInstanceSessionSet":
                case "ResourceUriSessionSet":
                {
                    CimSession[] cimSession = cmdlet.CimSession;
                    for (int i = 0; i < (int)cimSession.Length; i++)
                    {
                        CimSession      cimSession1     = cimSession[i];
                        CimSessionProxy cimSessionProxy = this.CreateSessionProxy(cimSession1, cmdlet);
                        cimSessionProxies.Add(cimSessionProxy);
                    }
                }
                break;
                }
            }
            CimMethodParametersCollection cimMethodParametersCollection = this.CreateParametersCollection(cmdlet.Arguments, cmdlet.CimClass, cmdlet.CimInstance, cmdlet.MethodName);
            string parameterSetName1 = cmdlet.ParameterSetName;
            string str2 = parameterSetName1;

            if (parameterSetName1 != null)
            {
                if (str2 == "ClassNameComputerSet" || str2 == "ClassNameSessionSet" || str2 == "ResourceUriSessionSet" || str2 == "ResourceUriComputerSet")
                {
                    object[] className = new object[1];
                    className[0] = cmdlet.ClassName;
                    string str3 = string.Format(CultureInfo.CurrentUICulture, "{0}", className);
                    if (cmdlet.ResourceUri == null)
                    {
                        @namespace = ConstValue.GetNamespace(cmdlet.Namespace);
                    }
                    else
                    {
                        @namespace = cmdlet.Namespace;
                    }
                    List <CimSessionProxy> .Enumerator enumerator2 = cimSessionProxies.GetEnumerator();
                    try
                    {
                        while (enumerator2.MoveNext())
                        {
                            CimSessionProxy cimSessionProxy1 = enumerator2.Current;
                            if (cmdlet.ShouldProcess(str3, str))
                            {
                                cimSessionProxy1.InvokeMethodAsync(@namespace, cmdlet.ClassName, cmdlet.MethodName, cimMethodParametersCollection);
                            }
                            else
                            {
                                return;
                            }
                        }
                        return;
                    }
                    finally
                    {
                        enumerator2.Dispose();
                    }
                }
                else if (str2 == "CimClassComputerSet" || str2 == "CimClassSessionSet")
                {
                    object[] objArray = new object[1];
                    objArray[0] = cmdlet.CimClass.CimSystemProperties.ClassName;
                    string str4 = string.Format(CultureInfo.CurrentUICulture, "{0}", objArray);
                    @namespace = ConstValue.GetNamespace(cmdlet.CimClass.CimSystemProperties.Namespace);
                    List <CimSessionProxy> .Enumerator enumerator3 = cimSessionProxies.GetEnumerator();
                    try
                    {
                        while (enumerator3.MoveNext())
                        {
                            CimSessionProxy current2 = enumerator3.Current;
                            if (cmdlet.ShouldProcess(str4, str))
                            {
                                current2.InvokeMethodAsync(@namespace, cmdlet.CimClass.CimSystemProperties.ClassName, cmdlet.MethodName, cimMethodParametersCollection);
                            }
                            else
                            {
                                return;
                            }
                        }
                        return;
                    }
                    finally
                    {
                        enumerator3.Dispose();
                    }
                }
                else if (str2 == "QueryComputerSet" || str2 == "QuerySessionSet")
                {
                    @namespace = ConstValue.GetNamespace(cmdlet.Namespace);
                    List <CimSessionProxy> .Enumerator enumerator4 = cimSessionProxies.GetEnumerator();
                    try
                    {
                        while (enumerator4.MoveNext())
                        {
                            CimSessionProxy cimSessionProxy2 = enumerator4.Current;
                            CimInvokeCimMethod.CimInvokeCimMethodContext cimInvokeCimMethodContext = new CimInvokeCimMethod.CimInvokeCimMethodContext(@namespace, cmdlet.MethodName, cimMethodParametersCollection, cimSessionProxy2);
                            cimSessionProxy2.ContextObject = cimInvokeCimMethodContext;
                            cimSessionProxy2.QueryInstancesAsync(@namespace, ConstValue.GetQueryDialectWithDefault(cmdlet.QueryDialect), cmdlet.Query);
                        }
                        return;
                    }
                    finally
                    {
                        enumerator4.Dispose();
                    }
                }
                else if (str2 == "CimInstanceComputerSet" || str2 == "CimInstanceSessionSet")
                {
                    string str5 = cmdlet.CimInstance.ToString();
                    if (cmdlet.ResourceUri == null)
                    {
                        @namespace = ConstValue.GetNamespace(cmdlet.CimInstance.CimSystemProperties.Namespace);
                    }
                    else
                    {
                        @namespace = cmdlet.Namespace;
                    }
                    List <CimSessionProxy> .Enumerator enumerator5 = cimSessionProxies.GetEnumerator();
                    try
                    {
                        while (enumerator5.MoveNext())
                        {
                            CimSessionProxy current3 = enumerator5.Current;
                            if (cmdlet.ShouldProcess(str5, str))
                            {
                                current3.InvokeMethodAsync(@namespace, cmdlet.CimInstance, cmdlet.MethodName, cimMethodParametersCollection);
                            }
                            else
                            {
                                return;
                            }
                        }
                        return;
                    }
                    finally
                    {
                        enumerator5.Dispose();
                    }
                }
                return;
            }
        }