/// <summary>
        /// Remove an object given either path,class name or pipeline input.
        /// </summary>
        protected override void ProcessRecord()
        {
            if (this.AsJob)
            {
                RunAsJob("Remove-WMIObject");
                return;
            }
            if (_inputObject != null)
            {
                try
                {
                    if (!ShouldProcess(_inputObject["__PATH"].ToString()))
                    {
                        return;
                    }
                    _inputObject.Delete();
                }
                catch (ManagementException e)
                {
                    ErrorRecord errorRecord = new ErrorRecord(e, "RemoveWMIManagementException", ErrorCategory.InvalidOperation, null);
                    WriteError(errorRecord);
                }
                catch (System.Runtime.InteropServices.COMException e)
                {
                    ErrorRecord errorRecord = new ErrorRecord(e, "RemoveWMICOMException", ErrorCategory.InvalidOperation, null);
                    WriteError(errorRecord);
                }
                return;
            }
            else
            {
                ConnectionOptions options = GetConnectionOption();
                ManagementPath    mPath   = null;
                ManagementObject  mObject = null;
                if (_path != null)
                {
                    mPath = new ManagementPath(_path);
                    if (String.IsNullOrEmpty(mPath.NamespacePath))
                    {
                        mPath.NamespacePath = this.Namespace;
                    }
                    else if (namespaceSpecified)
                    {
                        //ThrowTerminatingError
                        ThrowTerminatingError(new ErrorRecord(
                                                  new InvalidOperationException(),
                                                  "NamespaceSpecifiedWithPath",
                                                  ErrorCategory.InvalidOperation,
                                                  this.Namespace));
                    }

                    if (mPath.Server != "." && serverNameSpecified)
                    {
                        //ThrowTerminatingError
                        ThrowTerminatingError(new ErrorRecord(
                                                  new InvalidOperationException(),
                                                  "ComputerNameSpecifiedWithPath",
                                                  ErrorCategory.InvalidOperation,
                                                  this.ComputerName));
                    }
                    if (!(mPath.Server == "." && serverNameSpecified))
                    {
                        string[] serverName = new string[] { mPath.Server };
                        ComputerName = serverName;
                    }
                }
                foreach (string name in ComputerName)
                {
                    try
                    {
                        if (_path != null)
                        {
                            mPath.Server = name;
                            if (mPath.IsClass)
                            {
                                ManagementClass mClass = new ManagementClass(mPath);
                                mObject = mClass;
                            }
                            else
                            {
                                ManagementObject mInstance = new ManagementObject(mPath);
                                mObject = mInstance;
                            }
                            ManagementScope mScope = new ManagementScope(mPath, options);
                            mObject.Scope = mScope;
                        }
                        else
                        {
                            ManagementScope scope  = new ManagementScope(WMIHelper.GetScopeString(name, this.Namespace), options);
                            ManagementClass mClass = new ManagementClass(_className);
                            mObject       = mClass;
                            mObject.Scope = scope;
                        }
                        if (!ShouldProcess(mObject["__PATH"].ToString()))
                        {
                            continue;
                        }
                        mObject.Delete();
                    }
                    catch (ManagementException e)
                    {
                        ErrorRecord errorRecord = new ErrorRecord(e, "RemoveWMIManagementException", ErrorCategory.InvalidOperation, null);
                        WriteError(errorRecord);
                    }
                    catch (System.Runtime.InteropServices.COMException e)
                    {
                        ErrorRecord errorRecord = new ErrorRecord(e, "RemoveWMICOMException", ErrorCategory.InvalidOperation, null);
                        WriteError(errorRecord);
                    }
                }
            }
        }
        /// <summary>
        /// Gets the properties of an item at the specified path
        /// </summary>
        protected override void BeginProcessing()
        {
            ConnectionOptions options = GetConnectionOption();

            if (this.AsJob)
            {
                RunAsJob("Get-WMIObject");
                return;
            }
            else
            {
                if (List.IsPresent)
                {
                    if (!this.ValidateClassFormat())
                    {
                        ErrorRecord errorRecord = new ErrorRecord(
                            new ArgumentException(
                                String.Format(
                                    Thread.CurrentThread.CurrentCulture,
                                    "Class", this.Class)),
                            "INVALID_QUERY_IDENTIFIER",
                            ErrorCategory.InvalidArgument,
                            null);
                        errorRecord.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiFilterInvalidClass", this.Class);

                        WriteError(errorRecord);
                        return;
                    }
                    foreach (string name in ComputerName)
                    {
                        if (this.Recurse.IsPresent)
                        {
                            Queue namespaceElement = new Queue();
                            namespaceElement.Enqueue(this.Namespace);
                            while (namespaceElement.Count > 0)
                            {
                                string          connectNamespace = (string)namespaceElement.Dequeue();
                                ManagementScope scope            = new ManagementScope(WMIHelper.GetScopeString(name, connectNamespace), options);
                                try
                                {
                                    scope.Connect();
                                }
                                catch (ManagementException e)
                                {
                                    ErrorRecord errorRecord = new ErrorRecord(
                                        e,
                                        "INVALID_NAMESPACE_IDENTIFIER",
                                        ErrorCategory.ObjectNotFound,
                                        null);
                                    errorRecord.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiNamespaceConnect", connectNamespace, e.Message);
                                    WriteError(errorRecord);
                                    continue;
                                }
                                catch (System.Runtime.InteropServices.COMException e)
                                {
                                    ErrorRecord errorRecord = new ErrorRecord(
                                        e,
                                        "INVALID_NAMESPACE_IDENTIFIER",
                                        ErrorCategory.ObjectNotFound,
                                        null);
                                    errorRecord.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiNamespaceConnect", connectNamespace, e.Message);
                                    WriteError(errorRecord);
                                    continue;
                                }
                                catch (System.UnauthorizedAccessException e)
                                {
                                    ErrorRecord errorRecord = new ErrorRecord(
                                        e,
                                        "INVALID_NAMESPACE_IDENTIFIER",
                                        ErrorCategory.ObjectNotFound,
                                        null);
                                    errorRecord.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiNamespaceConnect", connectNamespace, e.Message);
                                    WriteError(errorRecord);
                                    continue;
                                }

                                ManagementClass namespaceClass = new ManagementClass(scope, new ManagementPath("__Namespace"), new ObjectGetOptions());
                                foreach (ManagementBaseObject obj in namespaceClass.GetInstances())
                                {
                                    if (!IsLocalizedNamespace((string)obj["Name"]))
                                    {
                                        namespaceElement.Enqueue(connectNamespace + "\\" + obj["Name"]);
                                    }
                                }

                                ManagementObjectSearcher searcher = this.GetObjectList(scope);
                                if (searcher == null)
                                {
                                    continue;
                                }
                                foreach (ManagementBaseObject obj in searcher.Get())
                                {
                                    WriteObject(obj);
                                }
                            }
                        }
                        else
                        {
                            ManagementScope scope = new ManagementScope(WMIHelper.GetScopeString(name, this.Namespace), options);
                            try
                            {
                                scope.Connect();
                            }
                            catch (ManagementException e)
                            {
                                ErrorRecord errorRecord = new ErrorRecord(
                                    e,
                                    "INVALID_NAMESPACE_IDENTIFIER",
                                    ErrorCategory.ObjectNotFound,
                                    null);
                                errorRecord.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiNamespaceConnect", this.Namespace, e.Message);
                                WriteError(errorRecord);
                                continue;
                            }
                            catch (System.Runtime.InteropServices.COMException e)
                            {
                                ErrorRecord errorRecord = new ErrorRecord(
                                    e,
                                    "INVALID_NAMESPACE_IDENTIFIER",
                                    ErrorCategory.ObjectNotFound,
                                    null);
                                errorRecord.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiNamespaceConnect", this.Namespace, e.Message);
                                WriteError(errorRecord);
                                continue;
                            }
                            catch (System.UnauthorizedAccessException e)
                            {
                                ErrorRecord errorRecord = new ErrorRecord(
                                    e,
                                    "INVALID_NAMESPACE_IDENTIFIER",
                                    ErrorCategory.ObjectNotFound,
                                    null);
                                errorRecord.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiNamespaceConnect", this.Namespace, e.Message);
                                WriteError(errorRecord);
                                continue;
                            }
                            ManagementObjectSearcher searcher = this.GetObjectList(scope);
                            if (searcher == null)
                            {
                                continue;
                            }
                            foreach (ManagementBaseObject obj in searcher.Get())
                            {
                                WriteObject(obj);
                            }
                        }
                    }
                    return;
                }

                // When -List is not specified and -Recurse is specified, we need the -Class parameter to compose the right query string
                if (this.Recurse.IsPresent && string.IsNullOrEmpty(Class))
                {
                    string      errorMsg = string.Format(CultureInfo.InvariantCulture, WmiResources.WmiParameterMissing, "-Class");
                    ErrorRecord er       = new ErrorRecord(new InvalidOperationException(errorMsg), "InvalidOperationException", ErrorCategory.InvalidOperation, null);
                    WriteError(er);
                    return;
                }

                string      queryString = string.IsNullOrEmpty(this.Query) ? GetQueryString() : this.Query;
                ObjectQuery query       = new ObjectQuery(queryString.ToString());

                foreach (string name in ComputerName)
                {
                    try
                    {
                        ManagementScope    scope       = new ManagementScope(WMIHelper.GetScopeString(name, this.Namespace), options);
                        EnumerationOptions enumOptions = new EnumerationOptions();
                        enumOptions.UseAmendedQualifiers = Amended;
                        enumOptions.DirectRead           = DirectRead;
                        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query, enumOptions);
                        foreach (ManagementBaseObject obj in searcher.Get())
                        {
                            WriteObject(obj);
                        }
                    }
                    catch (ManagementException e)
                    {
                        ErrorRecord errorRecord = null;
                        if (e.ErrorCode.Equals(ManagementStatus.InvalidClass))
                        {
                            string className = GetClassNameFromQuery(queryString);
                            string errorMsg  = String.Format(CultureInfo.InvariantCulture, WmiResources.WmiQueryFailure,
                                                             e.Message, className);
                            errorRecord = new ErrorRecord(new ManagementException(errorMsg), "GetWMIManagementException", ErrorCategory.InvalidType, null);
                        }
                        else if (e.ErrorCode.Equals(ManagementStatus.InvalidQuery))
                        {
                            string errorMsg = String.Format(CultureInfo.InvariantCulture, WmiResources.WmiQueryFailure,
                                                            e.Message, queryString);
                            errorRecord = new ErrorRecord(new ManagementException(errorMsg), "GetWMIManagementException", ErrorCategory.InvalidArgument, null);
                        }
                        else if (e.ErrorCode.Equals(ManagementStatus.InvalidNamespace))
                        {
                            string errorMsg = String.Format(CultureInfo.InvariantCulture, WmiResources.WmiQueryFailure,
                                                            e.Message, this.Namespace);
                            errorRecord = new ErrorRecord(new ManagementException(errorMsg), "GetWMIManagementException", ErrorCategory.InvalidArgument, null);
                        }
                        else
                        {
                            errorRecord = new ErrorRecord(e, "GetWMIManagementException", ErrorCategory.InvalidOperation, null);
                        }

                        WriteError(errorRecord);
                        continue;
                    }
                    catch (System.Runtime.InteropServices.COMException e)
                    {
                        ErrorRecord errorRecord = new ErrorRecord(e, "GetWMICOMException", ErrorCategory.InvalidOperation, null);
                        WriteError(errorRecord);
                        continue;
                    }
                } // foreach computerName
            }
        }         // BeginProcessing
Example #3
0
        protected override void BeginProcessing()
        {
            ErrorRecord errorRecord;

            object[]          message;
            string            queryString;
            ConnectionOptions connectionOption = base.GetConnectionOption();

            if (!base.AsJob)
            {
                if (!this.list.IsPresent)
                {
                    SwitchParameter recurse = this.Recurse;
                    if (!recurse.IsPresent || !string.IsNullOrEmpty(this.wmiClass))
                    {
                        if (string.IsNullOrEmpty(this.Query))
                        {
                            queryString = this.GetQueryString();
                        }
                        else
                        {
                            queryString = this.Query;
                        }
                        string      str          = queryString;
                        ObjectQuery objectQuery  = new ObjectQuery(str.ToString());
                        string[]    computerName = base.ComputerName;
                        for (int i = 0; i < (int)computerName.Length; i++)
                        {
                            string str1 = computerName[i];
                            try
                            {
                                ManagementScope    managementScope   = new ManagementScope(WMIHelper.GetScopeString(str1, base.Namespace), connectionOption);
                                EnumerationOptions enumerationOption = new EnumerationOptions();
                                enumerationOption.UseAmendedQualifiers = this.amended;
                                enumerationOption.DirectRead           = this.directRead;
                                ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(managementScope, objectQuery, enumerationOption);
                                foreach (ManagementBaseObject managementBaseObject in managementObjectSearcher.Get())
                                {
                                    var exists = managementBaseObject.ObjectExits;
                                    if (exists)
                                    {
                                        base.WriteObject(managementBaseObject);
                                    }
                                }
                            }
                            catch (ManagementException managementException1)
                            {
                                ManagementException managementException = managementException1;
                                if (!managementException.ErrorCode.Equals(ManagementStatus.InvalidClass))
                                {
                                    if (!managementException.ErrorCode.Equals(ManagementStatus.InvalidQuery))
                                    {
                                        if (!managementException.ErrorCode.Equals(ManagementStatus.InvalidNamespace))
                                        {
                                            errorRecord = new ErrorRecord(managementException, "GetWMIManagementException", ErrorCategory.InvalidOperation, null);
                                        }
                                        else
                                        {
                                            message    = new object[2];
                                            message[0] = managementException.Message;
                                            message[1] = base.Namespace;
                                            string str2 = string.Format(CultureInfo.InvariantCulture, WmiResources.WmiQueryFailure, message);
                                            errorRecord = new ErrorRecord(new ManagementException(str2), "GetWMIManagementException", ErrorCategory.InvalidArgument, null);
                                        }
                                    }
                                    else
                                    {
                                        message    = new object[2];
                                        message[0] = managementException.Message;
                                        message[1] = str;
                                        string str3 = string.Format(CultureInfo.InvariantCulture, WmiResources.WmiQueryFailure, message);
                                        errorRecord = new ErrorRecord(new ManagementException(str3), "GetWMIManagementException", ErrorCategory.InvalidArgument, null);
                                    }
                                }
                                else
                                {
                                    string classNameFromQuery = this.GetClassNameFromQuery(str);
                                    message    = new object[2];
                                    message[0] = managementException.Message;
                                    message[1] = classNameFromQuery;
                                    string str4 = string.Format(CultureInfo.InvariantCulture, WmiResources.WmiQueryFailure, message);
                                    errorRecord = new ErrorRecord(new ManagementException(str4), "GetWMIManagementException", ErrorCategory.InvalidType, null);
                                }
                                base.WriteError(errorRecord);
                            }
                            catch (COMException cOMException1)
                            {
                                COMException cOMException = cOMException1;
                                ErrorRecord  errorRecord1 = new ErrorRecord(cOMException, "GetWMICOMException", ErrorCategory.InvalidOperation, null);
                                base.WriteError(errorRecord1);
                            }
                            catch (Exception ex)
                            {
                                ErrorRecord errorRecord1 = new ErrorRecord(ex, "GetWMICOMException", ErrorCategory.InvalidOperation, null);
                                base.WriteError(errorRecord1);
                            }
                        }
                        return;
                    }
                    else
                    {
                        object[] objArray = new object[1];
                        objArray[0] = "-Class";
                        string      str5         = string.Format(CultureInfo.InvariantCulture, WmiResources.WmiParameterMissing, objArray);
                        ErrorRecord errorRecord2 = new ErrorRecord(new InvalidOperationException(str5), "InvalidOperationException", ErrorCategory.InvalidOperation, null);
                        base.WriteError(errorRecord2);
                        return;
                    }
                }
                else
                {
                    if (this.ValidateClassFormat())
                    {
                        string[] strArrays = base.ComputerName;
                        for (int j = 0; j < (int)strArrays.Length; j++)
                        {
                            string          str6            = strArrays[j];
                            SwitchParameter switchParameter = this.Recurse;
                            if (!switchParameter.IsPresent)
                            {
                                ManagementScope managementScope1 = new ManagementScope(WMIHelper.GetScopeString(str6, base.Namespace), connectionOption);
                                try
                                {
                                    managementScope1.Connect();
                                }
                                catch (ManagementException managementException3)
                                {
                                    ManagementException managementException2 = managementException3;
                                    ErrorRecord         errorDetail          = new ErrorRecord(managementException2, "INVALID_NAMESPACE_IDENTIFIER", ErrorCategory.ObjectNotFound, null);
                                    object[]            @namespace           = new object[2];
                                    @namespace[0]            = base.Namespace;
                                    @namespace[1]            = managementException2.Message;
                                    errorDetail.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiNamespaceConnect", @namespace);
                                    base.WriteError(errorDetail);
                                    goto Label0;
                                }
                                catch (COMException cOMException3)
                                {
                                    COMException cOMException2 = cOMException3;
                                    ErrorRecord  errorDetail1  = new ErrorRecord(cOMException2, "INVALID_NAMESPACE_IDENTIFIER", ErrorCategory.ObjectNotFound, null);
                                    object[]     namespace1    = new object[2];
                                    namespace1[0]             = base.Namespace;
                                    namespace1[1]             = cOMException2.Message;
                                    errorDetail1.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiNamespaceConnect", namespace1);
                                    base.WriteError(errorDetail1);
                                    goto Label0;
                                }
                                catch (UnauthorizedAccessException unauthorizedAccessException1)
                                {
                                    UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1;
                                    ErrorRecord errorDetail2 = new ErrorRecord(unauthorizedAccessException, "INVALID_NAMESPACE_IDENTIFIER", ErrorCategory.ObjectNotFound, null);
                                    object[]    message1     = new object[2];
                                    message1[0] = base.Namespace;
                                    message1[1] = unauthorizedAccessException.Message;
                                    errorDetail2.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiNamespaceConnect", message1);
                                    base.WriteError(errorDetail2);
                                    goto Label0;
                                }
                                ManagementObjectSearcher objectList = this.GetObjectList(managementScope1);
                                if (objectList != null)
                                {
                                    foreach (ManagementBaseObject managementBaseObject1 in objectList.Get())
                                    {
                                        base.WriteObject(managementBaseObject1);
                                    }
                                }
                            }
                            else
                            {
                                Queue queues = new Queue();
                                queues.Enqueue(base.Namespace);
                                while (queues.Count > 0)
                                {
                                    string          str7             = (string)queues.Dequeue();
                                    ManagementScope managementScope2 = new ManagementScope(WMIHelper.GetScopeString(str6, str7), connectionOption);
                                    try
                                    {
                                        managementScope2.Connect();
                                    }
                                    catch (ManagementException managementException5)
                                    {
                                        ManagementException managementException4 = managementException5;
                                        ErrorRecord         errorRecord3         = new ErrorRecord(managementException4, "INVALID_NAMESPACE_IDENTIFIER", ErrorCategory.ObjectNotFound, null);
                                        object[]            objArray1            = new object[2];
                                        objArray1[0] = str7;
                                        objArray1[1] = managementException4.Message;
                                        errorRecord3.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiNamespaceConnect", objArray1);
                                        base.WriteError(errorRecord3);
                                        continue;
                                    }
                                    catch (COMException cOMException5)
                                    {
                                        COMException cOMException4 = cOMException5;
                                        ErrorRecord  errorDetail3  = new ErrorRecord(cOMException4, "INVALID_NAMESPACE_IDENTIFIER", ErrorCategory.ObjectNotFound, null);
                                        object[]     message2      = new object[2];
                                        message2[0] = str7;
                                        message2[1] = cOMException4.Message;
                                        errorDetail3.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiNamespaceConnect", message2);
                                        base.WriteError(errorDetail3);
                                        continue;
                                    }
                                    catch (UnauthorizedAccessException unauthorizedAccessException3)
                                    {
                                        UnauthorizedAccessException unauthorizedAccessException2 = unauthorizedAccessException3;
                                        ErrorRecord errorRecord4 = new ErrorRecord(unauthorizedAccessException2, "INVALID_NAMESPACE_IDENTIFIER", ErrorCategory.ObjectNotFound, null);
                                        object[]    objArray2    = new object[2];
                                        objArray2[0] = str7;
                                        objArray2[1] = unauthorizedAccessException2.Message;
                                        errorRecord4.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiNamespaceConnect", objArray2);
                                        base.WriteError(errorRecord4);
                                        continue;
                                    }
                                    ManagementClass managementClass = new ManagementClass(managementScope2, new ManagementPath("__Namespace"), new ObjectGetOptions());
                                    foreach (ManagementBaseObject instance in managementClass.GetInstances())
                                    {
                                        if (this.IsLocalizedNamespace((string)instance["Name"]))
                                        {
                                            continue;
                                        }
                                        queues.Enqueue(string.Concat(str7, "\\", instance["Name"]));
                                    }
                                    ManagementObjectSearcher objectList1 = this.GetObjectList(managementScope2);
                                    if (objectList1 == null)
                                    {
                                        continue;
                                    }
                                    foreach (ManagementBaseObject managementBaseObject2 in objectList1.Get())
                                    {
                                        base.WriteObject(managementBaseObject2);
                                    }
                                }
                            }
Label0:
                            continue;
                        }
                        return;
                    }
                    else
                    {
                        message    = new object[1];
                        message[0] = this.Class;
                        ErrorRecord errorDetail4 = new ErrorRecord(new ArgumentException(string.Format(Thread.CurrentThread.CurrentCulture, "Class", message)), "INVALID_QUERY_IDENTIFIER", ErrorCategory.InvalidArgument, null);
                        object[]    @class       = new object[1];
                        @class[0] = this.Class;
                        errorDetail4.ErrorDetails = new ErrorDetails(this, "WmiResources", "WmiFilterInvalidClass", @class);
                        base.WriteError(errorDetail4);
                        return;
                    }
                }
            }
            else
            {
                base.RunAsJob("Get-WMIObject");
                return;
            }
        }
        /// <summary>
        /// Invoke WMI method given either path,class name or pipeline input.
        /// </summary>
        protected override void ProcessRecord()
        {
            if (this.AsJob)
            {
                RunAsJob("Invoke-WMIMethod");
                return;
            }

            if (_inputObject != null)
            {
                object result = null;
                ManagementBaseObject inputParameters = null;
                try
                {
                    inputParameters = _inputObject.GetMethodParameters(_methodName);
                    if (_argumentList != null)
                    {
                        int inParamCount = _argumentList.Length;
                        foreach (PropertyData property in inputParameters.Properties)
                        {
                            if (inParamCount == 0)
                            {
                                break;
                            }
                            property.Value = _argumentList[_argumentList.Length - inParamCount];
                            inParamCount--;
                        }
                    }

                    if (!ShouldProcess(
                            StringUtil.Format(WmiResources.WmiMethodNameForConfirmation,
                                              _inputObject["__CLASS"].ToString(),
                                              this.Name)
                            ))
                    {
                        return;
                    }

                    result = _inputObject.InvokeMethod(_methodName, inputParameters, null);
                }
                catch (ManagementException e)
                {
                    ErrorRecord errorRecord = new ErrorRecord(e, "InvokeWMIManagementException", ErrorCategory.InvalidOperation, null);
                    WriteError(errorRecord);
                }
                catch (System.Runtime.InteropServices.COMException e)
                {
                    ErrorRecord errorRecord = new ErrorRecord(e, "InvokeWMICOMException", ErrorCategory.InvalidOperation, null);
                    WriteError(errorRecord);
                }

                if (result != null)
                {
                    WriteObject(result);
                }

                return;
            }
            else
            {
                ConnectionOptions options = GetConnectionOption();
                ManagementPath    mPath   = null;
                object            result  = null;
                ManagementObject  mObject = null;
                if (_path != null)
                {
                    mPath = new ManagementPath(_path);
                    if (string.IsNullOrEmpty(mPath.NamespacePath))
                    {
                        mPath.NamespacePath = this.Namespace;
                    }
                    else if (namespaceSpecified)
                    {
                        //ThrowTerminatingError
                        ThrowTerminatingError(new ErrorRecord(
                                                  new InvalidOperationException(),
                                                  "NamespaceSpecifiedWithPath",
                                                  ErrorCategory.InvalidOperation,
                                                  this.Namespace));
                    }

                    if (mPath.Server != "." && serverNameSpecified)
                    {
                        //ThrowTerminatingError
                        ThrowTerminatingError(new ErrorRecord(
                                                  new InvalidOperationException(),
                                                  "ComputerNameSpecifiedWithPath",
                                                  ErrorCategory.InvalidOperation,
                                                  ComputerName));
                    }
                    //If server name is specified loop through it.
                    if (!(mPath.Server == "." && serverNameSpecified))
                    {
                        string[] serverName = new string[] { mPath.Server };
                        ComputerName = serverName;
                    }
                }

                foreach (string name in ComputerName)
                {
                    result = null;
                    try
                    {
                        if (_path != null)
                        {
                            mPath.Server = name;
                            if (mPath.IsClass)
                            {
                                ManagementClass mClass = new ManagementClass(mPath);
                                mObject = mClass;
                            }
                            else
                            {
                                ManagementObject mInstance = new ManagementObject(mPath);
                                mObject = mInstance;
                            }

                            ManagementScope mScope = new ManagementScope(mPath, options);
                            mObject.Scope = mScope;
                        }
                        else
                        {
                            ManagementScope scope  = new ManagementScope(WMIHelper.GetScopeString(name, this.Namespace), options);
                            ManagementClass mClass = new ManagementClass(_className);
                            mObject       = mClass;
                            mObject.Scope = scope;
                        }

                        ManagementBaseObject inputParameters = mObject.GetMethodParameters(_methodName);
                        if (_argumentList != null)
                        {
                            int inParamCount = _argumentList.Length;
                            foreach (PropertyData property in inputParameters.Properties)
                            {
                                if (inParamCount == 0)
                                {
                                    break;
                                }
                                object argument = PSObject.Base(_argumentList[_argumentList.Length - inParamCount]);
                                if (property.IsArray)
                                {
                                    property.Value = MakeBaseObjectArray(argument);
                                }
                                else
                                {
                                    property.Value = argument;
                                }

                                inParamCount--;
                            }
                        }

                        if (!ShouldProcess(
                                StringUtil.Format(WmiResources.WmiMethodNameForConfirmation,
                                                  mObject["__CLASS"].ToString(),
                                                  this.Name)
                                ))
                        {
                            return;
                        }

                        result = mObject.InvokeMethod(_methodName, inputParameters, null);
                    }
                    catch (ManagementException e)
                    {
                        ErrorRecord errorRecord = new ErrorRecord(e, "InvokeWMIManagementException", ErrorCategory.InvalidOperation, null);
                        WriteError(errorRecord);
                    }
                    catch (System.Runtime.InteropServices.COMException e)
                    {
                        ErrorRecord errorRecord = new ErrorRecord(e, "InvokeWMICOMException", ErrorCategory.InvalidOperation, null);
                        WriteError(errorRecord);
                    }

                    if (result != null)
                    {
                        WriteObject(result);
                    }
                }
            }
        }//ProcessRecord
Example #5
0
        protected override void ProcessRecord()
        {
            ManagementObject managementObject;

            if (!base.AsJob)
            {
                if (this.inputObject == null)
                {
                    ConnectionOptions connectionOption = base.GetConnectionOption();
                    ManagementPath    managementPath   = null;
                    object            obj = null;
                    if (this.path != null)
                    {
                        managementPath = new ManagementPath(this.path);
                        if (!string.IsNullOrEmpty(managementPath.NamespacePath))
                        {
                            if (this.namespaceSpecified)
                            {
                                base.ThrowTerminatingError(new ErrorRecord(new InvalidOperationException(), "NamespaceSpecifiedWithPath", ErrorCategory.InvalidOperation, base.Namespace));
                            }
                        }
                        else
                        {
                            managementPath.NamespacePath = base.Namespace;
                        }
                        if (managementPath.Server != "." && this.serverNameSpecified)
                        {
                            base.ThrowTerminatingError(new ErrorRecord(new InvalidOperationException(), "ComputerNameSpecifiedWithPath", ErrorCategory.InvalidOperation, base.ComputerName));
                        }
                        if (!(managementPath.Server == ".") || !this.serverNameSpecified)
                        {
                            string[] server = new string[1];
                            server[0] = managementPath.Server;
                            string[] strArrays = server;
                            base.ComputerName = strArrays;
                        }
                    }
                    string[] computerName = base.ComputerName;
                    for (int i = 0; i < (int)computerName.Length; i++)
                    {
                        string str = computerName[i];
                        obj = null;
                        try
                        {
                            if (this.path == null)
                            {
                                ManagementScope managementScope = new ManagementScope(WMIHelper.GetScopeString(str, base.Namespace), connectionOption);
                                ManagementClass managementClass = new ManagementClass(this.className);
                                managementObject       = managementClass;
                                managementObject.Scope = managementScope;
                            }
                            else
                            {
                                managementPath.Server = str;
                                if (!managementPath.IsClass)
                                {
                                    ManagementObject managementObject1 = new ManagementObject(managementPath);
                                    managementObject = managementObject1;
                                }
                                else
                                {
                                    ManagementClass managementClass1 = new ManagementClass(managementPath);
                                    managementObject = managementClass1;
                                }
                                ManagementScope managementScope1 = new ManagementScope(managementPath, connectionOption);
                                managementObject.Scope = managementScope1;
                            }
                            ManagementBaseObject methodParameters = managementObject.GetMethodParameters(this.methodName);
                            if (this.argumentList != null)
                            {
                                int length = (int)this.argumentList.Length;
                                foreach (PropertyData property in methodParameters.Properties)
                                {
                                    if (length == 0)
                                    {
                                        break;
                                    }
                                    object obj1 = PSObject.Base(this.argumentList[(int)this.argumentList.Length - length]);
                                    if (!property.IsArray)
                                    {
                                        property.Value = obj1;
                                    }
                                    else
                                    {
                                        property.Value = InvokeWmiMethod.MakeBaseObjectArray(obj1);
                                    }
                                    length--;
                                }
                            }
                            if (base.ShouldProcess(StringUtil.Format(WmiResources.WmiMethodNameForConfirmation, managementObject["__CLASS"].ToString(), this.Name)))
                            {
                                obj = managementObject.InvokeMethod(this.methodName, methodParameters, null);
                            }
                            else
                            {
                                return;
                            }
                        }
                        catch (ManagementException managementException1)
                        {
                            ManagementException managementException = managementException1;
                            ErrorRecord         errorRecord         = new ErrorRecord(managementException, "InvokeWMIManagementException", ErrorCategory.InvalidOperation, null);
                            base.WriteError(errorRecord);
                        }
                        catch (COMException cOMException1)
                        {
                            COMException cOMException = cOMException1;
                            ErrorRecord  errorRecord1 = new ErrorRecord(cOMException, "InvokeWMICOMException", ErrorCategory.InvalidOperation, null);
                            base.WriteError(errorRecord1);
                        }
                        if (obj != null)
                        {
                            base.WriteObject(obj);
                        }
                    }
                    return;
                }
                else
                {
                    object obj2 = null;
                    try
                    {
                        ManagementBaseObject managementBaseObject = this.inputObject.GetMethodParameters(this.methodName);
                        if (this.argumentList != null)
                        {
                            int num = (int)this.argumentList.Length;
                            foreach (PropertyData propertyDatum in managementBaseObject.Properties)
                            {
                                if (num == 0)
                                {
                                    break;
                                }
                                propertyDatum.Value = this.argumentList[(int)this.argumentList.Length - num];
                                num--;
                            }
                        }
                        if (base.ShouldProcess(StringUtil.Format(WmiResources.WmiMethodNameForConfirmation, this.inputObject["__CLASS"].ToString(), this.Name)))
                        {
                            obj2 = this.inputObject.InvokeMethod(this.methodName, managementBaseObject, null);
                        }
                        else
                        {
                            return;
                        }
                    }
                    catch (ManagementException managementException3)
                    {
                        ManagementException managementException2 = managementException3;
                        ErrorRecord         errorRecord2         = new ErrorRecord(managementException2, "InvokeWMIManagementException", ErrorCategory.InvalidOperation, null);
                        base.WriteError(errorRecord2);
                    }
                    catch (COMException cOMException3)
                    {
                        COMException cOMException2 = cOMException3;
                        ErrorRecord  errorRecord3  = new ErrorRecord(cOMException2, "InvokeWMICOMException", ErrorCategory.InvalidOperation, null);
                        base.WriteError(errorRecord3);
                    }
                    if (obj2 != null)
                    {
                        base.WriteObject(obj2);
                    }
                }
                return;
            }
            else
            {
                base.RunAsJob("Invoke-WMIMethod");
                return;
            }
        }
Example #6
0
        internal ManagementObject SetWmiInstanceGetObject(ManagementPath mPath, string serverName)
        {
            ConnectionOptions connectionOption = this.GetConnectionOption();
            ManagementObject  value            = null;

            if (base.GetType() == typeof(SetWmiInstance))
            {
                SetWmiInstance setWmiInstance = (SetWmiInstance)this;
                if (setWmiInstance.Path == null)
                {
                    ManagementScope managementScope = new ManagementScope(WMIHelper.GetScopeString(serverName, setWmiInstance.Namespace), connectionOption);
                    ManagementClass managementClass = new ManagementClass(setWmiInstance.Class);
                    managementClass.Scope = managementScope;
                    value = managementClass.CreateInstance();
                }
                else
                {
                    mPath.Server = serverName;
                    ManagementScope managementScope1 = new ManagementScope(mPath, connectionOption);
                    if (!mPath.IsClass)
                    {
                        ManagementObject managementObject = new ManagementObject(mPath);
                        managementObject.Scope = managementScope1;
                        try
                        {
                            managementObject.Get();
                        }
                        catch (ManagementException managementException1)
                        {
                            ManagementException managementException = managementException1;
                            if (managementException.ErrorCode == ManagementStatus.NotFound)
                            {
                                int num = setWmiInstance.Path.IndexOf(':');
                                if (num != -1)
                                {
                                    int num1 = setWmiInstance.Path.Substring(num).IndexOf('.');
                                    if (num1 != -1)
                                    {
                                        string          str              = setWmiInstance.Path.Substring(0, num1 + num);
                                        ManagementPath  managementPath   = new ManagementPath(str);
                                        ManagementClass managementClass1 = new ManagementClass(managementPath);
                                        managementClass1.Scope = managementScope1;
                                        managementObject       = managementClass1.CreateInstance();
                                    }
                                    else
                                    {
                                        throw;
                                    }
                                }
                                else
                                {
                                    throw;
                                }
                            }
                            else
                            {
                                throw;
                            }
                        }
                        value = managementObject;
                    }
                    else
                    {
                        ManagementClass managementClass2 = new ManagementClass(mPath);
                        managementClass2.Scope = managementScope1;
                        value = managementClass2.CreateInstance();
                    }
                }
                if (setWmiInstance.Arguments != null)
                {
                    IDictionaryEnumerator enumerator = setWmiInstance.Arguments.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        value[enumerator.Key as string] = enumerator.Value;
                    }
                }
            }
            return(value);
        }
        private void ConnectSetWmi()
        {
            ManagementObject value;
            SetWmiInstance   setWmiInstance = (SetWmiInstance)this.wmiObject;

            this.state = WmiState.Running;
            this.RaiseWmiOperationState(null, WmiState.Running);
            if (setWmiInstance.InputObject == null)
            {
                ManagementPath managementPath = null;
                if (setWmiInstance.Class == null)
                {
                    managementPath = new ManagementPath(setWmiInstance.Path);
                    if (!string.IsNullOrEmpty(managementPath.NamespacePath))
                    {
                        if (setWmiInstance.namespaceSpecified)
                        {
                            InvalidOperationException invalidOperationException = new InvalidOperationException("NamespaceSpecifiedWithPath");
                            this.internalException = invalidOperationException;
                            this.state             = WmiState.Failed;
                            this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                            return;
                        }
                    }
                    else
                    {
                        managementPath.NamespacePath = setWmiInstance.Namespace;
                    }
                    if (!(managementPath.Server != ".") || !setWmiInstance.serverNameSpecified)
                    {
                        if (!managementPath.IsClass)
                        {
                            if (!setWmiInstance.flagSpecified)
                            {
                                setWmiInstance.PutType = PutType.UpdateOrCreate;
                            }
                            else
                            {
                                if (setWmiInstance.PutType != PutType.UpdateOnly && setWmiInstance.PutType != PutType.UpdateOrCreate)
                                {
                                    InvalidOperationException invalidOperationException1 = new InvalidOperationException("NonUpdateFlagSpecifiedWithInstancePath");
                                    this.internalException = invalidOperationException1;
                                    this.state             = WmiState.Failed;
                                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                                    return;
                                }
                            }
                        }
                        else
                        {
                            if (!setWmiInstance.flagSpecified || setWmiInstance.PutType == PutType.CreateOnly)
                            {
                                setWmiInstance.PutType = PutType.CreateOnly;
                            }
                            else
                            {
                                InvalidOperationException invalidOperationException2 = new InvalidOperationException("CreateOnlyFlagNotSpecifiedWithClassPath");
                                this.internalException = invalidOperationException2;
                                this.state             = WmiState.Failed;
                                this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                                return;
                            }
                        }
                    }
                    else
                    {
                        InvalidOperationException invalidOperationException3 = new InvalidOperationException("ComputerNameSpecifiedWithPath");
                        this.internalException = invalidOperationException3;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                        return;
                    }
                }
                else
                {
                    if (setWmiInstance.flagSpecified && setWmiInstance.PutType != PutType.CreateOnly)
                    {
                        InvalidOperationException invalidOperationException4 = new InvalidOperationException("CreateOnlyFlagNotSpecifiedWithClassPath");
                        this.internalException = invalidOperationException4;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                        return;
                    }
                    setWmiInstance.PutType = PutType.CreateOnly;
                }
                if (managementPath != null && (!(managementPath.Server == ".") || !setWmiInstance.serverNameSpecified))
                {
                    this.computerName = managementPath.Server;
                }
                ConnectionOptions connectionOption = setWmiInstance.GetConnectionOption();
                try
                {
                    if (setWmiInstance.Path == null)
                    {
                        ManagementScope managementScope = new ManagementScope(WMIHelper.GetScopeString(this.computerName, setWmiInstance.Namespace), connectionOption);
                        ManagementClass managementClass = new ManagementClass(setWmiInstance.Class);
                        managementClass.Scope = managementScope;
                        value = managementClass.CreateInstance();
                    }
                    else
                    {
                        managementPath.Server = this.computerName;
                        ManagementScope managementScope1 = new ManagementScope(managementPath, connectionOption);
                        if (!managementPath.IsClass)
                        {
                            ManagementObject managementObject = new ManagementObject(managementPath);
                            managementObject.Scope = managementScope1;
                            try
                            {
                                managementObject.Get();
                            }
                            catch (ManagementException managementException1)
                            {
                                ManagementException managementException = managementException1;
                                if (managementException.ErrorCode == ManagementStatus.NotFound)
                                {
                                    int num = setWmiInstance.Path.IndexOf(':');
                                    if (num != -1)
                                    {
                                        int num1 = setWmiInstance.Path.Substring(num).IndexOf('.');
                                        if (num1 != -1)
                                        {
                                            string          str              = setWmiInstance.Path.Substring(0, num1 + num);
                                            ManagementPath  managementPath1  = new ManagementPath(str);
                                            ManagementClass managementClass1 = new ManagementClass(managementPath1);
                                            managementClass1.Scope = managementScope1;
                                            managementObject       = managementClass1.CreateInstance();
                                        }
                                        else
                                        {
                                            this.internalException = managementException;
                                            this.state             = WmiState.Failed;
                                            this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        this.internalException = managementException;
                                        this.state             = WmiState.Failed;
                                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                                        return;
                                    }
                                }
                                else
                                {
                                    this.internalException = managementException;
                                    this.state             = WmiState.Failed;
                                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                                    return;
                                }
                            }
                            value = managementObject;
                        }
                        else
                        {
                            ManagementClass managementClass2 = new ManagementClass(managementPath);
                            managementClass2.Scope = managementScope1;
                            value = managementClass2.CreateInstance();
                        }
                    }
                    if (setWmiInstance.Arguments != null)
                    {
                        IDictionaryEnumerator enumerator = setWmiInstance.Arguments.GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            value[enumerator.Key as string] = enumerator.Value;
                        }
                    }
                    PutOptions putOption = new PutOptions();
                    putOption.Type = setWmiInstance.PutType;
                    if (value == null)
                    {
                        InvalidOperationException invalidOperationException5 = new InvalidOperationException();
                        this.internalException = invalidOperationException5;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                    else
                    {
                        value.Put(this.results, putOption);
                    }
                }
                catch (ManagementException managementException3)
                {
                    ManagementException managementException2 = managementException3;
                    this.internalException = managementException2;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (COMException cOMException1)
                {
                    COMException cOMException = cOMException1;
                    this.internalException = cOMException;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (UnauthorizedAccessException unauthorizedAccessException1)
                {
                    UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1;
                    this.internalException = unauthorizedAccessException;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
            }
            else
            {
                ManagementObject value1 = null;
                try
                {
                    PutOptions putType = new PutOptions();
                    if (setWmiInstance.InputObject.GetType() != typeof(ManagementClass))
                    {
                        if (!setWmiInstance.flagSpecified)
                        {
                            setWmiInstance.PutType = PutType.UpdateOrCreate;
                        }
                        else
                        {
                            if (setWmiInstance.PutType != PutType.UpdateOnly && setWmiInstance.PutType != PutType.UpdateOrCreate)
                            {
                                InvalidOperationException invalidOperationException6 = new InvalidOperationException("NonUpdateFlagSpecifiedWithInstancePath");
                                this.internalException = invalidOperationException6;
                                this.state             = WmiState.Failed;
                                this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                                return;
                            }
                        }
                        value1 = (ManagementObject)setWmiInstance.InputObject.Clone();
                    }
                    else
                    {
                        if (!setWmiInstance.flagSpecified || setWmiInstance.PutType == PutType.CreateOnly)
                        {
                            value1 = ((ManagementClass)setWmiInstance.InputObject).CreateInstance();
                            setWmiInstance.PutType = PutType.CreateOnly;
                        }
                        else
                        {
                            InvalidOperationException invalidOperationException7 = new InvalidOperationException("CreateOnlyFlagNotSpecifiedWithClassPath");
                            this.internalException = invalidOperationException7;
                            this.state             = WmiState.Failed;
                            this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                            return;
                        }
                    }
                    if (setWmiInstance.Arguments != null)
                    {
                        IDictionaryEnumerator dictionaryEnumerator = setWmiInstance.Arguments.GetEnumerator();
                        while (dictionaryEnumerator.MoveNext())
                        {
                            value1[dictionaryEnumerator.Key as string] = dictionaryEnumerator.Value;
                        }
                    }
                    putType.Type = setWmiInstance.PutType;
                    if (value1 == null)
                    {
                        InvalidOperationException invalidOperationException8 = new InvalidOperationException();
                        this.internalException = invalidOperationException8;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                    else
                    {
                        value1.Put(this.results, putType);
                    }
                }
                catch (ManagementException managementException5)
                {
                    ManagementException managementException4 = managementException5;
                    this.internalException = managementException4;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (COMException cOMException3)
                {
                    COMException cOMException2 = cOMException3;
                    this.internalException = cOMException2;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (UnauthorizedAccessException unauthorizedAccessException3)
                {
                    UnauthorizedAccessException unauthorizedAccessException2 = unauthorizedAccessException3;
                    this.internalException = unauthorizedAccessException2;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
            }
        }
        private void ConnectGetWMI()
        {
            string wmiQueryString;
            GetWmiObjectCommand getWmiObjectCommand = (GetWmiObjectCommand)this.wmiObject;

            this.state = WmiState.Running;
            this.RaiseWmiOperationState(null, WmiState.Running);
            ConnectionOptions connectionOption = getWmiObjectCommand.GetConnectionOption();
            SwitchParameter   list             = getWmiObjectCommand.List;

            if (!list.IsPresent)
            {
                if (string.IsNullOrEmpty(getWmiObjectCommand.Query))
                {
                    wmiQueryString = this.GetWmiQueryString();
                }
                else
                {
                    wmiQueryString = getWmiObjectCommand.Query;
                }
                string      str         = wmiQueryString;
                ObjectQuery objectQuery = new ObjectQuery(str.ToString());
                try
                {
                    ManagementScope    managementScope   = new ManagementScope(WMIHelper.GetScopeString(this.computerName, getWmiObjectCommand.Namespace), connectionOption);
                    EnumerationOptions enumerationOption = new EnumerationOptions();
                    enumerationOption.UseAmendedQualifiers = getWmiObjectCommand.Amended;
                    enumerationOption.DirectRead           = getWmiObjectCommand.DirectRead;
                    ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(managementScope, objectQuery, enumerationOption);
                    for (int i = 0; i < this.cmdCount; i++)
                    {
                        managementObjectSearcher.Get(this.results);
                    }
                }
                catch (ManagementException managementException1)
                {
                    ManagementException managementException = managementException1;
                    this.internalException = managementException;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (COMException cOMException1)
                {
                    COMException cOMException = cOMException1;
                    this.internalException = cOMException;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (UnauthorizedAccessException unauthorizedAccessException1)
                {
                    UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1;
                    this.internalException = unauthorizedAccessException;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                return;
            }
            else
            {
                if (getWmiObjectCommand.ValidateClassFormat())
                {
                    try
                    {
                        SwitchParameter recurse = getWmiObjectCommand.Recurse;
                        if (!recurse.IsPresent)
                        {
                            ManagementScope managementScope1 = new ManagementScope(WMIHelper.GetScopeString(this.computerName, getWmiObjectCommand.Namespace), connectionOption);
                            managementScope1.Connect();
                            ManagementObjectSearcher objectList = getWmiObjectCommand.GetObjectList(managementScope1);
                            if (objectList != null)
                            {
                                objectList.Get(this.results);
                            }
                            else
                            {
                                throw new ManagementException();
                            }
                        }
                        else
                        {
                            ArrayList arrayLists  = new ArrayList();
                            ArrayList arrayLists1 = new ArrayList();
                            ArrayList arrayLists2 = new ArrayList();
                            int       num         = 0;
                            arrayLists.Add(getWmiObjectCommand.Namespace);
                            bool flag = true;
                            while (num < arrayLists.Count)
                            {
                                string          item             = (string)arrayLists[num];
                                ManagementScope managementScope2 = new ManagementScope(WMIHelper.GetScopeString(this.computerName, item), connectionOption);
                                managementScope2.Connect();
                                ManagementClass managementClass = new ManagementClass(managementScope2, new ManagementPath("__Namespace"), new ObjectGetOptions());
                                foreach (ManagementBaseObject instance in managementClass.GetInstances())
                                {
                                    if (getWmiObjectCommand.IsLocalizedNamespace((string)instance["Name"]))
                                    {
                                        continue;
                                    }
                                    arrayLists.Add(string.Concat(item, "\\", instance["Name"]));
                                }
                                if (!flag)
                                {
                                    arrayLists1.Add(this.Job.GetNewSink());
                                }
                                else
                                {
                                    flag = false;
                                    arrayLists1.Add(this.results);
                                }
                                arrayLists2.Add(managementScope2);
                                num++;
                            }
                            if (arrayLists1.Count != arrayLists.Count || arrayLists2.Count != arrayLists.Count)
                            {
                                this.internalException = new InvalidOperationException();
                                this.state             = WmiState.Failed;
                                this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                                return;
                            }
                            else
                            {
                                num = 0;
                                while (num < arrayLists.Count)
                                {
                                    ManagementObjectSearcher objectList1 = getWmiObjectCommand.GetObjectList((ManagementScope)arrayLists2[num]);
                                    if (objectList1 != null)
                                    {
                                        if (!flag)
                                        {
                                            objectList1.Get((ManagementOperationObserver)arrayLists1[num]);
                                        }
                                        else
                                        {
                                            flag = false;
                                            objectList1.Get(this.results);
                                        }
                                        num++;
                                    }
                                    else
                                    {
                                        num++;
                                    }
                                }
                            }
                        }
                    }
                    catch (ManagementException managementException3)
                    {
                        ManagementException managementException2 = managementException3;
                        this.internalException = managementException2;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                    catch (COMException cOMException3)
                    {
                        COMException cOMException2 = cOMException3;
                        this.internalException = cOMException2;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                    catch (UnauthorizedAccessException unauthorizedAccessException3)
                    {
                        UnauthorizedAccessException unauthorizedAccessException2 = unauthorizedAccessException3;
                        this.internalException = unauthorizedAccessException2;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                    return;
                }
                else
                {
                    object[] @class = new object[1];
                    @class[0] = getWmiObjectCommand.Class;
                    ArgumentException argumentException = new ArgumentException(string.Format(Thread.CurrentThread.CurrentCulture, "Class", @class));
                    this.internalException = argumentException;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    return;
                }
            }
        }
        private void ConnectRemoveWmi()
        {
            ManagementObject managementObject;
            RemoveWmiObject  removeWmiObject = (RemoveWmiObject)this.wmiObject;

            this.state = WmiState.Running;
            this.RaiseWmiOperationState(null, WmiState.Running);
            if (removeWmiObject.InputObject == null)
            {
                ConnectionOptions connectionOption = removeWmiObject.GetConnectionOption();
                ManagementPath    managementPath   = null;
                if (removeWmiObject.Path != null)
                {
                    managementPath = new ManagementPath(removeWmiObject.Path);
                    if (!string.IsNullOrEmpty(managementPath.NamespacePath))
                    {
                        if (removeWmiObject.namespaceSpecified)
                        {
                            InvalidOperationException invalidOperationException = new InvalidOperationException("NamespaceSpecifiedWithPath");
                            this.internalException = invalidOperationException;
                            this.state             = WmiState.Failed;
                            this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                            return;
                        }
                    }
                    else
                    {
                        managementPath.NamespacePath = removeWmiObject.Namespace;
                    }
                    if (!(managementPath.Server != ".") || !removeWmiObject.serverNameSpecified)
                    {
                        if (!(managementPath.Server == ".") || !removeWmiObject.serverNameSpecified)
                        {
                            this.computerName = managementPath.Server;
                        }
                    }
                    else
                    {
                        InvalidOperationException invalidOperationException1 = new InvalidOperationException("ComputerNameSpecifiedWithPath");
                        this.internalException = invalidOperationException1;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                        return;
                    }
                }
                try
                {
                    if (removeWmiObject.Path == null)
                    {
                        ManagementScope managementScope = new ManagementScope(WMIHelper.GetScopeString(this.computerName, removeWmiObject.Namespace), connectionOption);
                        ManagementClass managementClass = new ManagementClass(removeWmiObject.Class);
                        managementObject       = managementClass;
                        managementObject.Scope = managementScope;
                    }
                    else
                    {
                        managementPath.Server = this.computerName;
                        if (!managementPath.IsClass)
                        {
                            ManagementObject managementObject1 = new ManagementObject(managementPath);
                            managementObject = managementObject1;
                        }
                        else
                        {
                            ManagementClass managementClass1 = new ManagementClass(managementPath);
                            managementObject = managementClass1;
                        }
                        ManagementScope managementScope1 = new ManagementScope(managementPath, connectionOption);
                        managementObject.Scope = managementScope1;
                    }
                    managementObject.Delete(this.results);
                }
                catch (ManagementException managementException1)
                {
                    ManagementException managementException = managementException1;
                    this.internalException = managementException;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (COMException cOMException1)
                {
                    COMException cOMException = cOMException1;
                    this.internalException = cOMException;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (UnauthorizedAccessException unauthorizedAccessException1)
                {
                    UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1;
                    this.internalException = unauthorizedAccessException;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                return;
            }
            else
            {
                try
                {
                    removeWmiObject.InputObject.Delete(this.results);
                }
                catch (ManagementException managementException3)
                {
                    ManagementException managementException2 = managementException3;
                    this.internalException = managementException2;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (COMException cOMException3)
                {
                    COMException cOMException2 = cOMException3;
                    this.internalException = cOMException2;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (UnauthorizedAccessException unauthorizedAccessException3)
                {
                    UnauthorizedAccessException unauthorizedAccessException2 = unauthorizedAccessException3;
                    this.internalException = unauthorizedAccessException2;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                return;
            }
        }
        private void ConnectInvokeWmi()
        {
            ManagementObject managementObject;
            object           obj;
            string           str;
            InvokeWmiMethod  invokeWmiMethod = (InvokeWmiMethod)this.wmiObject;

            this.state = WmiState.Running;
            this.RaiseWmiOperationState(null, WmiState.Running);
            if (invokeWmiMethod.InputObject == null)
            {
                ConnectionOptions connectionOption = invokeWmiMethod.GetConnectionOption();
                ManagementPath    managementPath   = null;
                if (invokeWmiMethod.Path != null)
                {
                    managementPath = new ManagementPath(invokeWmiMethod.Path);
                    if (!string.IsNullOrEmpty(managementPath.NamespacePath))
                    {
                        if (invokeWmiMethod.namespaceSpecified)
                        {
                            InvalidOperationException invalidOperationException = new InvalidOperationException("NamespaceSpecifiedWithPath");
                            this.internalException = invalidOperationException;
                            this.state             = WmiState.Failed;
                            this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                            return;
                        }
                    }
                    else
                    {
                        managementPath.NamespacePath = invokeWmiMethod.Namespace;
                    }
                    if (!(managementPath.Server != ".") || !invokeWmiMethod.serverNameSpecified)
                    {
                        if (!(managementPath.Server == ".") || !invokeWmiMethod.serverNameSpecified)
                        {
                            this.computerName = managementPath.Server;
                        }
                    }
                    else
                    {
                        InvalidOperationException invalidOperationException1 = new InvalidOperationException("ComputerNameSpecifiedWithPath");
                        this.internalException = invalidOperationException1;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                        return;
                    }
                }
                bool flag            = false;
                bool enablePrivilege = false;
                Win32Native.TOKEN_PRIVILEGE tOKENPRIVILEGE = new Win32Native.TOKEN_PRIVILEGE();
                try
                {
                    try
                    {
                        enablePrivilege = this.NeedToEnablePrivilege(this.computerName, invokeWmiMethod.Name, ref flag);
                        if (!enablePrivilege || flag && ComputerWMIHelper.EnableTokenPrivilege("SeShutdownPrivilege", ref tOKENPRIVILEGE) || !flag && ComputerWMIHelper.EnableTokenPrivilege("SeRemoteShutdownPrivilege", ref tOKENPRIVILEGE))
                        {
                            if (invokeWmiMethod.Path == null)
                            {
                                ManagementScope managementScope = new ManagementScope(WMIHelper.GetScopeString(this.computerName, invokeWmiMethod.Namespace), connectionOption);
                                ManagementClass managementClass = new ManagementClass(invokeWmiMethod.Class);
                                managementObject       = managementClass;
                                managementObject.Scope = managementScope;
                            }
                            else
                            {
                                managementPath.Server = this.computerName;
                                if (!managementPath.IsClass)
                                {
                                    ManagementObject managementObject1 = new ManagementObject(managementPath);
                                    managementObject = managementObject1;
                                }
                                else
                                {
                                    ManagementClass managementClass1 = new ManagementClass(managementPath);
                                    managementObject = managementClass1;
                                }
                                ManagementScope managementScope1 = new ManagementScope(managementPath, connectionOption);
                                managementObject.Scope = managementScope1;
                            }
                            ManagementBaseObject methodParameters = managementObject.GetMethodParameters(invokeWmiMethod.Name);
                            if (invokeWmiMethod.ArgumentList != null)
                            {
                                int length = (int)invokeWmiMethod.ArgumentList.Length;
                                foreach (PropertyData property in methodParameters.Properties)
                                {
                                    if (length == 0)
                                    {
                                        break;
                                    }
                                    property.Value = invokeWmiMethod.ArgumentList[(int)invokeWmiMethod.ArgumentList.Length - length];
                                    length--;
                                }
                            }
                            if (!enablePrivilege)
                            {
                                managementObject.InvokeMethod(this.results, invokeWmiMethod.Name, methodParameters, null);
                            }
                            else
                            {
                                ManagementBaseObject managementBaseObject = managementObject.InvokeMethod(invokeWmiMethod.Name, methodParameters, null);
                                int num = Convert.ToInt32(managementBaseObject["ReturnValue"], CultureInfo.CurrentCulture);
                                if (num == 0)
                                {
                                    this.ShutdownComplete.SafeInvoke <EventArgs>(this, null);
                                }
                                else
                                {
                                    Win32Exception win32Exception = new Win32Exception(num);
                                    this.internalException = win32Exception;
                                    this.state             = WmiState.Failed;
                                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                                }
                            }
                        }
                        else
                        {
                            string privilegeNotEnabled = ComputerResources.PrivilegeNotEnabled;
                            string str1 = this.computerName;
                            if (flag)
                            {
                                obj = "SeShutdownPrivilege";
                            }
                            else
                            {
                                obj = "SeRemoteShutdownPrivilege";
                            }
                            string str2 = StringUtil.Format(privilegeNotEnabled, str1, obj);
                            InvalidOperationException invalidOperationException2 = new InvalidOperationException(str2);
                            this.internalException = invalidOperationException2;
                            this.state             = WmiState.Failed;
                            this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                            return;
                        }
                    }
                    catch (ManagementException managementException1)
                    {
                        ManagementException managementException = managementException1;
                        this.internalException = managementException;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                    catch (COMException cOMException1)
                    {
                        COMException cOMException = cOMException1;
                        this.internalException = cOMException;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                    catch (UnauthorizedAccessException unauthorizedAccessException1)
                    {
                        UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1;
                        this.internalException = unauthorizedAccessException;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                }
                finally
                {
                    if (enablePrivilege)
                    {
                        if (flag)
                        {
                            str = "SeShutdownPrivilege";
                        }
                        else
                        {
                            str = "SeRemoteShutdownPrivilege";
                        }
                        ComputerWMIHelper.RestoreTokenPrivilege(str, ref tOKENPRIVILEGE);
                    }
                }
                return;
            }
            else
            {
                try
                {
                    ManagementBaseObject methodParameters1 = invokeWmiMethod.InputObject.GetMethodParameters(invokeWmiMethod.Name);
                    if (invokeWmiMethod.ArgumentList != null)
                    {
                        int length1 = (int)invokeWmiMethod.ArgumentList.Length;
                        foreach (PropertyData argumentList in methodParameters1.Properties)
                        {
                            if (length1 == 0)
                            {
                                break;
                            }
                            argumentList.Value = invokeWmiMethod.ArgumentList[(int)invokeWmiMethod.ArgumentList.Length - length1];
                            length1--;
                        }
                    }
                    invokeWmiMethod.InputObject.InvokeMethod(this.results, invokeWmiMethod.Name, methodParameters1, null);
                }
                catch (ManagementException managementException3)
                {
                    ManagementException managementException2 = managementException3;
                    this.internalException = managementException2;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (COMException cOMException3)
                {
                    COMException cOMException2 = cOMException3;
                    this.internalException = cOMException2;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (UnauthorizedAccessException unauthorizedAccessException3)
                {
                    UnauthorizedAccessException unauthorizedAccessException2 = unauthorizedAccessException3;
                    this.internalException = unauthorizedAccessException2;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                return;
            }
        }
Example #11
0
        protected override void ProcessRecord()
        {
            ManagementObject managementObject;

            if (!base.AsJob)
            {
                if (this.inputObject == null)
                {
                    ConnectionOptions connectionOption = base.GetConnectionOption();
                    ManagementPath    managementPath   = null;
                    if (this.path != null)
                    {
                        managementPath = new ManagementPath(this.path);
                        if (!string.IsNullOrEmpty(managementPath.NamespacePath))
                        {
                            if (this.namespaceSpecified)
                            {
                                base.ThrowTerminatingError(new ErrorRecord(new InvalidOperationException(), "NamespaceSpecifiedWithPath", ErrorCategory.InvalidOperation, base.Namespace));
                            }
                        }
                        else
                        {
                            managementPath.NamespacePath = base.Namespace;
                        }
                        if (managementPath.Server != "." && this.serverNameSpecified)
                        {
                            base.ThrowTerminatingError(new ErrorRecord(new InvalidOperationException(), "ComputerNameSpecifiedWithPath", ErrorCategory.InvalidOperation, base.ComputerName));
                        }
                        if (!(managementPath.Server == ".") || !this.serverNameSpecified)
                        {
                            string[] server = new string[1];
                            server[0] = managementPath.Server;
                            string[] strArrays = server;
                            base.ComputerName = strArrays;
                        }
                    }
                    string[] computerName = base.ComputerName;
                    for (int i = 0; i < (int)computerName.Length; i++)
                    {
                        string str = computerName[i];
                        try
                        {
                            if (this.path == null)
                            {
                                ManagementScope managementScope = new ManagementScope(WMIHelper.GetScopeString(str, base.Namespace), connectionOption);
                                ManagementClass managementClass = new ManagementClass(this.className);
                                managementObject       = managementClass;
                                managementObject.Scope = managementScope;
                            }
                            else
                            {
                                managementPath.Server = str;
                                if (!managementPath.IsClass)
                                {
                                    ManagementObject managementObject1 = new ManagementObject(managementPath);
                                    managementObject = managementObject1;
                                }
                                else
                                {
                                    ManagementClass managementClass1 = new ManagementClass(managementPath);
                                    managementObject = managementClass1;
                                }
                                ManagementScope managementScope1 = new ManagementScope(managementPath, connectionOption);
                                managementObject.Scope = managementScope1;
                            }
                            if (base.ShouldProcess(managementObject["__PATH"].ToString()))
                            {
                                managementObject.Delete();
                            }
                        }
                        catch (ManagementException managementException1)
                        {
                            ManagementException managementException = managementException1;
                            ErrorRecord         errorRecord         = new ErrorRecord(managementException, "RemoveWMIManagementException", ErrorCategory.InvalidOperation, null);
                            base.WriteError(errorRecord);
                        }
                        catch (COMException cOMException1)
                        {
                            COMException cOMException = cOMException1;
                            ErrorRecord  errorRecord1 = new ErrorRecord(cOMException, "RemoveWMICOMException", ErrorCategory.InvalidOperation, null);
                            base.WriteError(errorRecord1);
                        }
                    }
                    return;
                }
                else
                {
                    try
                    {
                        if (base.ShouldProcess(this.inputObject["__PATH"].ToString()))
                        {
                            this.inputObject.Delete();
                        }
                    }
                    catch (ManagementException managementException3)
                    {
                        ManagementException managementException2 = managementException3;
                        ErrorRecord         errorRecord2         = new ErrorRecord(managementException2, "RemoveWMIManagementException", ErrorCategory.InvalidOperation, null);
                        base.WriteError(errorRecord2);
                    }
                    catch (COMException cOMException3)
                    {
                        COMException cOMException2 = cOMException3;
                        ErrorRecord  errorRecord3  = new ErrorRecord(cOMException2, "RemoveWMICOMException", ErrorCategory.InvalidOperation, null);
                        base.WriteError(errorRecord3);
                    }
                    return;
                }
            }
            else
            {
                base.RunAsJob("Remove-WMIObject");
                return;
            }
        }