Example #1
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 List <CimSessionProxy>();

            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);
            }
        }
Example #2
0
 internal void TestCimSession(string computerName, CimSessionProxy proxy)
 {
     DebugHelper.WriteLogEx();
     base.SubscribeEventAndAddProxytoCache(proxy);
     proxy.TestConnectionAsync();
 }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RemoveCimInstanceCommand"/> class.
 /// </summary>
 public RemoveCimInstanceCommand()
     : base(parameters, parameterSets)
 {
     DebugHelper.WriteLogEx();
 }
Example #4
0
 /// <summary>
 /// constructor.
 /// </summary>
 public NewCimSessionOptionCommand()
     : base(parameters, parameterSets)
 {
     DebugHelper.WriteLogEx();
 }
Example #5
0
        /// <summary>
        /// Create WSMANSessionOptions.
        /// </summary>
        /// <returns></returns>
        internal WSManSessionOptions CreateWSMANSessionOptions()
        {
            WSManSessionOptions wsmanoptions = new WSManSessionOptions();

            if (this.noEncryptionSet)
            {
                wsmanoptions.NoEncryption = true;
                this.noEncryptionSet      = false;
            }
            else
            {
                wsmanoptions.NoEncryption = false;
            }

            if (this.skipCACheckSet)
            {
                wsmanoptions.CertCACheck = false;
                this.skipCACheckSet      = false;
            }
            else
            {
                wsmanoptions.CertCACheck = true;
            }

            if (this.skipCNCheckSet)
            {
                wsmanoptions.CertCNCheck = false;
                this.skipCNCheckSet      = false;
            }
            else
            {
                wsmanoptions.CertCNCheck = true;
            }

            if (this.skipRevocationCheckSet)
            {
                wsmanoptions.CertRevocationCheck = false;
                this.skipRevocationCheckSet      = false;
            }
            else
            {
                wsmanoptions.CertRevocationCheck = true;
            }

            if (this.encodeportinserviceprincipalnameSet)
            {
                wsmanoptions.EncodePortInServicePrincipalName = this.EncodePortInServicePrincipalName;
                this.encodeportinserviceprincipalnameSet      = false;
            }
            else
            {
                wsmanoptions.EncodePortInServicePrincipalName = false;
            }

            if (this.encodingSet)
            {
                wsmanoptions.PacketEncoding = this.Encoding;
            }
            else
            {
                wsmanoptions.PacketEncoding = PacketEncoding.Utf8;
            }

            if (this.HttpPrefix != null)
            {
                wsmanoptions.HttpUrlPrefix = this.HttpPrefix;
            }

            if (this.maxenvelopesizekbSet)
            {
                wsmanoptions.MaxEnvelopeSize = this.MaxEnvelopeSizeKB;
            }
            else
            {
                wsmanoptions.MaxEnvelopeSize = 0;
            }

            if (!string.IsNullOrWhiteSpace(this.ProxyCertificateThumbprint))
            {
                CimCredential credentials = new CimCredential(CertificateAuthenticationMechanism.Default, this.ProxyCertificateThumbprint);
                wsmanoptions.AddProxyCredentials(credentials);
            }

            if (this.proxyauthenticationSet)
            {
                this.proxyauthenticationSet = false;
                DebugHelper.WriteLogEx("create credential", 1);
                CimCredential credentials = CreateCimCredentials(this.ProxyCredential, this.ProxyAuthentication, @"New-CimSessionOption", @"ProxyAuthentication");
                if (credentials != null)
                {
                    try
                    {
                        DebugHelper.WriteLogEx("Add proxy credential", 1);
                        wsmanoptions.AddProxyCredentials(credentials);
                    }
                    catch (Exception ex)
                    {
                        DebugHelper.WriteLogEx(ex.ToString(), 1);
                        throw ex;
                    }
                }
            }

            if (this.proxytypeSet)
            {
                wsmanoptions.ProxyType = this.ProxyType;
                this.proxytypeSet      = false;
            }
            else
            {
                wsmanoptions.ProxyType = Options.ProxyType.WinHttp;
            }

            if (this.usesslSet)
            {
                wsmanoptions.UseSsl = this.UseSsl;
                this.usesslSet      = false;
            }
            else
            {
                wsmanoptions.UseSsl = false;
            }

            wsmanoptions.DestinationPort = 0;
            return(wsmanoptions);
        }
Example #6
0
 /// <summary>
 /// Override the OnNext method.
 /// </summary>
 /// <param name="value"></param>
 public override void OnNext(CimSubscriptionResult value)
 {
     DebugHelper.WriteLogEx();
     base.OnNextCore(value);
 }
Example #7
0
 /// <summary>
 /// Override the OnNext method.
 /// </summary>
 /// <param name="value"></param>
 public override void OnNext(CimInstance value)
 {
     DebugHelper.WriteLogEx();
 }
Example #8
0
 /// <summary>
 /// constructor.
 /// </summary>
 public GetCimClassCommand()
     : base(parameters, parameterSets)
 {
     DebugHelper.WriteLogEx();
 }
        /// <summary>
        /// Get the parameter set name based on current binding results.
        /// </summary>
        /// <returns></returns>
        internal string GetParameterSet()
        {
            DebugHelper.WriteLogEx();

            string        boundParameterSetName   = null;
            string        defaultParameterSetName = null;
            List <string> noMandatoryParameterSet = new List <string>();

            // Looking for parameter set which have mandatory parameters
            foreach (string parameterSetName in this.parameterSetEntries.Keys)
            {
                ParameterSetEntry entry = this.parameterSetEntries[parameterSetName];
                DebugHelper.WriteLogEx(
                    "parameterset name = {0}, {1}/{2} mandatory parameters.",
                    1,
                    parameterSetName,
                    entry.SetMandatoryParameterCount,
                    entry.MandatoryParameterCount);

                // Ignore the parameter set which has no mandatory parameter firstly
                if (entry.MandatoryParameterCount == 0)
                {
                    if (entry.IsDefaultParameterSet)
                    {
                        defaultParameterSetName = parameterSetName;
                    }

                    if (entry.IsValueSet)
                    {
                        noMandatoryParameterSet.Add(parameterSetName);
                    }

                    continue;
                }

                if ((entry.SetMandatoryParameterCount == entry.MandatoryParameterCount) &&
                    this.parametersetNamesList.Contains(parameterSetName))
                {
                    if (boundParameterSetName != null)
                    {
                        throw new PSArgumentException(CimCmdletStrings.UnableToResolveParameterSetName);
                    }

                    boundParameterSetName = parameterSetName;
                }
            }

            // Looking for parameter set which has no mandatory parameters
            if (boundParameterSetName == null)
            {
                // throw if there are > 1 parameter set
                if (noMandatoryParameterSet.Count > 1)
                {
                    throw new PSArgumentException(CimCmdletStrings.UnableToResolveParameterSetName);
                }
                else if (noMandatoryParameterSet.Count == 1)
                {
                    boundParameterSetName = noMandatoryParameterSet[0];
                }
            }

            // Looking for default parameter set
            if (boundParameterSetName == null)
            {
                boundParameterSetName = defaultParameterSetName;
            }

            // throw if still can not find the parameter set name
            if (boundParameterSetName == null)
            {
                throw new PSArgumentException(CimCmdletStrings.UnableToResolveParameterSetName);
            }

            return(boundParameterSetName);
        }
        /// <summary>
        /// <para>
        /// A given parameter's value was set by cmdlet caller,
        /// check and change the status of parameter set,
        /// throw exception if confliction occurred
        /// </para>
        /// </summary>
        /// <param name="parameterName"></param>
        /// <exception cref="PSArgumentException">Throw if conflict parameter was set.</exception>
        internal void SetParameter(string parameterName, bool isBeginProcess)
        {
            DebugHelper.WriteLogEx("ParameterName = {0}, isBeginProcess = {1}", 0, parameterName, isBeginProcess);

            if (this.parameterNamesList.Contains(parameterName))
            {
                DebugHelper.WriteLogEx("ParameterName {0} is already bound ", 1, parameterName);
                return;
            }
            else
            {
                this.parameterNamesList.Add(parameterName);
                if (isBeginProcess)
                {
                    this.parameterNamesListAtBeginProcess.Add(parameterName);
                }
            }

            if (this.parametersetNamesList.Count == 0)
            {
                List <string> nameset = new List <string>();
                foreach (ParameterDefinitionEntry parameterDefinitionEntry in this.parameterDefinitionEntries[parameterName])
                {
                    DebugHelper.WriteLogEx("parameterset name = '{0}'; mandatory = '{1}'", 1, parameterDefinitionEntry.ParameterSetName, parameterDefinitionEntry.IsMandatory);
                    ParameterSetEntry psEntry = this.parameterSetEntries[parameterDefinitionEntry.ParameterSetName];
                    if (psEntry == null)
                    {
                        continue;
                    }

                    if (parameterDefinitionEntry.IsMandatory)
                    {
                        psEntry.SetMandatoryParameterCount++;
                        if (isBeginProcess)
                        {
                            psEntry.SetMandatoryParameterCountAtBeginProcess++;
                        }

                        DebugHelper.WriteLogEx("parameterset name = '{0}'; SetMandatoryParameterCount = '{1}'", 1, parameterDefinitionEntry.ParameterSetName, psEntry.SetMandatoryParameterCount);
                    }

                    if (!psEntry.IsValueSet)
                    {
                        psEntry.IsValueSet = true;
                        if (isBeginProcess)
                        {
                            psEntry.IsValueSetAtBeginProcess = true;
                        }
                    }

                    nameset.Add(parameterDefinitionEntry.ParameterSetName);
                }

                this.parametersetNamesList = nameset;
                if (isBeginProcess)
                {
                    this.parametersetNamesListAtBeginProcess = nameset;
                }
            }
            else
            {
                List <string> nameset = new List <string>();
                foreach (ParameterDefinitionEntry entry in this.parameterDefinitionEntries[parameterName])
                {
                    if (this.parametersetNamesList.Contains(entry.ParameterSetName))
                    {
                        nameset.Add(entry.ParameterSetName);
                        if (entry.IsMandatory)
                        {
                            ParameterSetEntry psEntry = this.parameterSetEntries[entry.ParameterSetName];
                            psEntry.SetMandatoryParameterCount++;
                            if (isBeginProcess)
                            {
                                psEntry.SetMandatoryParameterCountAtBeginProcess++;
                            }

                            DebugHelper.WriteLogEx("parameterset name = '{0}'; SetMandatoryParameterCount = '{1}'",
                                                   1,
                                                   entry.ParameterSetName,
                                                   psEntry.SetMandatoryParameterCount);
                        }
                    }
                }

                if (nameset.Count == 0)
                {
                    throw new PSArgumentException(CimCmdletStrings.UnableToResolveParameterSetName);
                }
                else
                {
                    this.parametersetNamesList = nameset;
                    if (isBeginProcess)
                    {
                        this.parametersetNamesListAtBeginProcess = nameset;
                    }
                }
            }
        }
Example #11
0
        /// <summary>
        /// <para>
        /// Create <see cref="CimMethodParametersCollection"/> with given key properties.
        /// And/or <see cref="CimClass"/> object.
        /// </para>
        /// </summary>
        /// <param name="parameters"></param>
        /// <param name="cimClass"></param>
        /// <param name="cimInstance"></param>
        /// <param name="methodName"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">See CimProperty.Create.</exception>
        /// <exception cref="ArgumentException">CimProperty.Create.</exception>
        private CimMethodParametersCollection CreateParametersCollection(
            IDictionary parameters,
            CimClass cimClass,
            CimInstance cimInstance,
            string methodName)
        {
            DebugHelper.WriteLogEx();

            CimMethodParametersCollection collection = null;

            if (parameters == null)
            {
                return(collection);
            }
            else if (parameters.Count == 0)
            {
                return(collection);
            }

            collection = new CimMethodParametersCollection();
            IDictionaryEnumerator enumerator = parameters.GetEnumerator();

            while (enumerator.MoveNext())
            {
                string parameterName = enumerator.Key.ToString();

                CimFlags parameterFlags = CimFlags.In;
                object   parameterValue = GetBaseObject(enumerator.Value);

                DebugHelper.WriteLog(@"Create parameter name= {0}, value= {1}, flags= {2}.", 4,
                                     parameterName,
                                     parameterValue,
                                     parameterFlags);

                CimMethodParameter   parameter   = null;
                CimMethodDeclaration declaration = null;
                string className = null;
                if (cimClass != null)
                {
                    className   = cimClass.CimSystemProperties.ClassName;
                    declaration = cimClass.CimClassMethods[methodName];
                    if (declaration == null)
                    {
                        throw new ArgumentException(string.Format(
                                                        CultureInfo.CurrentUICulture, Strings.InvalidMethod, methodName, className));
                    }
                }
                else if (cimInstance != null)
                {
                    className   = cimInstance.CimClass.CimSystemProperties.ClassName;
                    declaration = cimInstance.CimClass.CimClassMethods[methodName];
                }

                if (declaration != null)
                {
                    CimMethodParameterDeclaration paramDeclaration = declaration.Parameters[parameterName];
                    if (paramDeclaration == null)
                    {
                        throw new ArgumentException(string.Format(
                                                        CultureInfo.CurrentUICulture, Strings.InvalidMethodParameter, parameterName, methodName, className));
                    }

                    parameter = CimMethodParameter.Create(
                        parameterName,
                        parameterValue,
                        paramDeclaration.CimType,
                        parameterFlags);
                    // FIXME: check in/out qualifier
                    // parameterFlags = paramDeclaration.Qualifiers;
                }
                else
                {
                    if (parameterValue == null)
                    {
                        // try the best to get the type while value is null
                        parameter = CimMethodParameter.Create(
                            parameterName,
                            parameterValue,
                            CimType.String,
                            parameterFlags);
                    }
                    else
                    {
                        CimType referenceType   = CimType.Unknown;
                        object  referenceObject = GetReferenceOrReferenceArrayObject(parameterValue, ref referenceType);
                        if (referenceObject != null)
                        {
                            parameter = CimMethodParameter.Create(
                                parameterName,
                                referenceObject,
                                referenceType,
                                parameterFlags);
                        }
                        else
                        {
                            parameter = CimMethodParameter.Create(
                                parameterName,
                                parameterValue,
                                parameterFlags);
                        }
                    }
                }

                if (parameter != null)
                {
                    collection.Add(parameter);
                }
            }

            return(collection);
        }
Example #12
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 List <CimSessionProxy>();
            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)
                    {
                        this.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)
                    {
                        this.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)
                    {
                        this.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;
            }
        }