internal void RaiseWmiOperationState(EventArgs baseEventArgs, WmiState state) { WmiJobStateEventArgs wmiJobStateEventArg = new WmiJobStateEventArgs(); wmiJobStateEventArg.WmiState = state; this.WmiOperationState.SafeInvoke <WmiJobStateEventArgs>(this, wmiJobStateEventArg); }
internal WmiAsyncCmdletHelper(PSWmiChildJob childJob, Cmdlet wmiObject, string computerName, ManagementOperationObserver results) { this.cmdCount = 1; this.wmiObject = wmiObject; this.computerName = computerName; this.results = results; this.State = WmiState.NotStarted; this.Job = childJob; }
internal override void StartOperation() { Thread thread; if (this.wmiObject.GetType() != typeof(GetWmiObjectCommand)) { if (this.wmiObject.GetType() != typeof(RemoveWmiObject)) { if (this.wmiObject.GetType() != typeof(InvokeWmiMethod)) { if (this.wmiObject.GetType() != typeof(SetWmiInstance)) { InvalidOperationException invalidOperationException = new InvalidOperationException("This operation is not supported for this cmdlet."); this.internalException = invalidOperationException; this.state = WmiState.Failed; this.RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } else { thread = new Thread(new ThreadStart(this.ConnectSetWmi)); } } else { thread = new Thread(new ThreadStart(this.ConnectInvokeWmi)); } } else { thread = new Thread(new ThreadStart(this.ConnectRemoveWmi)); } } else { thread = new Thread(new ThreadStart(this.ConnectGetWMI)); } thread.IsBackground = true; thread.Start(); }
/// <summary> /// Cancel WMI connection /// </summary> internal override void StopOperation() { _results.Cancel(); _state = WmiState.Stopped; RaiseOperationCompleteEvent(null, OperationState.StopComplete); }
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; } } }
/// <summary> /// Do the actual connection to remote machine for Remove-WMIObject cmdlet and raise operation complete event. /// </summary> private void ConnectRemoveWmi() { RemoveWmiObject removeObject = (RemoveWmiObject)_wmiObject; _state = WmiState.Running; RaiseWmiOperationState(null, WmiState.Running); if (removeObject.InputObject != null) { try { removeObject.InputObject.Delete(_results); } catch (ManagementException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.Runtime.InteropServices.COMException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.UnauthorizedAccessException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } return; } else { ConnectionOptions options = removeObject.GetConnectionOption(); ManagementPath mPath = null; ManagementObject mObject = null; if (removeObject.Path != null) { mPath = new ManagementPath(removeObject.Path); if (String.IsNullOrEmpty(mPath.NamespacePath)) { mPath.NamespacePath = removeObject.Namespace; } else if (removeObject.namespaceSpecified) { InvalidOperationException e = new InvalidOperationException("NamespaceSpecifiedWithPath"); internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } if (mPath.Server != "." && removeObject.serverNameSpecified) { InvalidOperationException e = new InvalidOperationException("ComputerNameSpecifiedWithPath"); internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } if (!(mPath.Server == "." && removeObject.serverNameSpecified)) { _computerName = mPath.Server; } } try { if (removeObject.Path != null) { mPath.Server = _computerName; 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(_computerName, removeObject.Namespace), options); ManagementClass mClass = new ManagementClass(removeObject.Class); mObject = mClass; mObject.Scope = scope; } mObject.Delete(_results); } catch (ManagementException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.Runtime.InteropServices.COMException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.UnauthorizedAccessException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } } }
/// <summary> /// Do the actual connection to remote machine for Set-WMIInstance cmdlet and raise operation complete event. /// </summary> private void ConnectSetWmi() { SetWmiInstance setObject = (SetWmiInstance)_wmiObject; _state = WmiState.Running; RaiseWmiOperationState(null, WmiState.Running); if (setObject.InputObject != null) { ManagementObject mObj = null; try { PutOptions pOptions = new PutOptions(); //Extra check if (setObject.InputObject.GetType() == typeof(ManagementClass)) { //Check if Flag specified is CreateOnly or not if (setObject.flagSpecified && setObject.PutType != PutType.CreateOnly) { InvalidOperationException e = new InvalidOperationException("CreateOnlyFlagNotSpecifiedWithClassPath"); internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } mObj = ((ManagementClass)setObject.InputObject).CreateInstance(); setObject.PutType = PutType.CreateOnly; } else { //Check if Flag specified is Updateonly or UpdateOrCreateOnly or not if (setObject.flagSpecified) { if (!(setObject.PutType == PutType.UpdateOnly || setObject.PutType == PutType.UpdateOrCreate)) { InvalidOperationException e = new InvalidOperationException("NonUpdateFlagSpecifiedWithInstancePath"); internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } } else { setObject.PutType = PutType.UpdateOrCreate; } mObj = (ManagementObject)setObject.InputObject.Clone(); } if (setObject.Arguments != null) { IDictionaryEnumerator en = setObject.Arguments.GetEnumerator(); while (en.MoveNext()) { mObj[en.Key as string] = en.Value; } } pOptions.Type = setObject.PutType; if (mObj != null) { mObj.Put(_results, pOptions); } else { InvalidOperationException exp = new InvalidOperationException(); internalException = exp; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } } catch (ManagementException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.Runtime.InteropServices.COMException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.UnauthorizedAccessException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } } else { ManagementPath mPath = null; //If Class is specified only CreateOnly flag is supported if (setObject.Class != null) { if (setObject.flagSpecified && setObject.PutType != PutType.CreateOnly) { InvalidOperationException exp = new InvalidOperationException("CreateOnlyFlagNotSpecifiedWithClassPath"); internalException = exp; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } setObject.PutType = PutType.CreateOnly; } else { mPath = new ManagementPath(setObject.Path); if (String.IsNullOrEmpty(mPath.NamespacePath)) { mPath.NamespacePath = setObject.Namespace; } else if (setObject.namespaceSpecified) { InvalidOperationException exp = new InvalidOperationException("NamespaceSpecifiedWithPath"); internalException = exp; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } if (mPath.Server != "." && setObject.serverNameSpecified) { InvalidOperationException exp = new InvalidOperationException("ComputerNameSpecifiedWithPath"); internalException = exp; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } if (mPath.IsClass) { if (setObject.flagSpecified && setObject.PutType != PutType.CreateOnly) { InvalidOperationException exp = new InvalidOperationException("CreateOnlyFlagNotSpecifiedWithClassPath"); internalException = exp; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } setObject.PutType = PutType.CreateOnly; } else { if (setObject.flagSpecified) { if (!(setObject.PutType == PutType.UpdateOnly || setObject.PutType == PutType.UpdateOrCreate)) { InvalidOperationException exp = new InvalidOperationException("NonUpdateFlagSpecifiedWithInstancePath"); internalException = exp; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } } else { setObject.PutType = PutType.UpdateOrCreate; } } } //If server name is specified loop through it. if (mPath != null) { if (!(mPath.Server == "." && setObject.serverNameSpecified)) { _computerName = mPath.Server; } } ConnectionOptions options = setObject.GetConnectionOption(); ManagementObject mObject = null; try { if (setObject.Path != null) { mPath.Server = _computerName; ManagementScope mScope = new ManagementScope(mPath, options); if (mPath.IsClass) { ManagementClass mClass = new ManagementClass(mPath); mClass.Scope = mScope; mObject = mClass.CreateInstance(); } else { //This can throw if path does not exist caller should catch it. ManagementObject mInstance = new ManagementObject(mPath); mInstance.Scope = mScope; try { mInstance.Get(); } catch (ManagementException e) { if (e.ErrorCode != ManagementStatus.NotFound) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } int namespaceIndex = setObject.Path.IndexOf(':'); if (namespaceIndex == -1) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } int classIndex = (setObject.Path.Substring(namespaceIndex)).IndexOf('.'); if (classIndex == -1) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } //Get class object and create instance. string newPath = setObject.Path.Substring(0, classIndex + namespaceIndex); ManagementPath classPath = new ManagementPath(newPath); ManagementClass mClass = new ManagementClass(classPath); mClass.Scope = mScope; mInstance = mClass.CreateInstance(); } mObject = mInstance; } } else { ManagementScope scope = new ManagementScope(WMIHelper.GetScopeString(_computerName, setObject.Namespace), options); ManagementClass mClass = new ManagementClass(setObject.Class); mClass.Scope = scope; mObject = mClass.CreateInstance(); } if (setObject.Arguments != null) { IDictionaryEnumerator en = setObject.Arguments.GetEnumerator(); while (en.MoveNext()) { mObject[en.Key as string] = en.Value; } } PutOptions pOptions = new PutOptions(); pOptions.Type = setObject.PutType; if (mObject != null) { mObject.Put(_results, pOptions); } else { InvalidOperationException exp = new InvalidOperationException(); internalException = exp; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } } catch (ManagementException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.Runtime.InteropServices.COMException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.UnauthorizedAccessException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } } }
internal override void StopOperation() { this.results.Cancel(); this.state = WmiState.Stopped; this.RaiseOperationCompleteEvent(null, OperationState.StopComplete); }
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; } }
/// <summary> /// Do WMI connection by creating another thread based on type of request and return immediately. /// </summary> internal override void StartOperation() { Thread thread; if (_wmiObject.GetType() == typeof(GetWmiObjectCommand)) { thread = new Thread(new ThreadStart(ConnectGetWMI)); } else if (_wmiObject.GetType() == typeof(RemoveWmiObject)) { thread = new Thread(new ThreadStart(ConnectRemoveWmi)); } else if (_wmiObject is InvokeWmiMethod) { thread = new Thread(new ThreadStart(ConnectInvokeWmi)); } else if (_wmiObject is SetWmiInstance) { thread = new Thread(new ThreadStart(ConnectSetWmi)); } else { InvalidOperationException exception = new InvalidOperationException("This operation is not supported for this cmdlet."); internalException = exception; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } thread.IsBackground = true; //thread.SetApartmentState( ApartmentState.STA); thread.Start(); }
} // RaiseOperationCompleteEvent ///<summary> /// Raise WMI state changed event ///</summary> internal void RaiseWmiOperationState(EventArgs baseEventArgs, WmiState state) { WmiJobStateEventArgs wmiJobStateEventArgs = new WmiJobStateEventArgs(); wmiJobStateEventArgs.WmiState = state; WmiOperationState.SafeInvoke(this, wmiJobStateEventArgs); }
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; } }
/// <summary> /// Do the actual connection to remote machine for Invoke-WMIMethod cmdlet and raise operation complete event. /// </summary> private void ConnectInvokeWmi() { InvokeWmiMethod invokeObject = (InvokeWmiMethod)_wmiObject; _state = WmiState.Running; RaiseWmiOperationState(null, WmiState.Running); if (invokeObject.InputObject != null) { ManagementBaseObject inputParameters = null; try { inputParameters = invokeObject.InputObject.GetMethodParameters(invokeObject.Name); if (invokeObject.ArgumentList != null) { int inParamCount = invokeObject.ArgumentList.Length; foreach (PropertyData property in inputParameters.Properties) { if (inParamCount == 0) break; property.Value = invokeObject.ArgumentList[invokeObject.ArgumentList.Length - inParamCount]; inParamCount--; } } invokeObject.InputObject.InvokeMethod(_results, invokeObject.Name, inputParameters, null); } catch (ManagementException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.Runtime.InteropServices.COMException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.UnauthorizedAccessException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } return; } else { ConnectionOptions options = invokeObject.GetConnectionOption(); ManagementPath mPath = null; ManagementObject mObject = null; if (invokeObject.Path != null) { mPath = new ManagementPath(invokeObject.Path); if (String.IsNullOrEmpty(mPath.NamespacePath)) { mPath.NamespacePath = invokeObject.Namespace; } else if (invokeObject.namespaceSpecified) { InvalidOperationException e = new InvalidOperationException("NamespaceSpecifiedWithPath"); internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } if (mPath.Server != "." && invokeObject.serverNameSpecified) { InvalidOperationException e = new InvalidOperationException("ComputerNameSpecifiedWithPath"); internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } //If server name is specified loop through it. if (!(mPath.Server == "." && invokeObject.serverNameSpecified)) { _computerName = mPath.Server; } } bool isLocal = false, needToEnablePrivilege = false; PlatformInvokes.TOKEN_PRIVILEGE currentPrivilegeState = new PlatformInvokes.TOKEN_PRIVILEGE(); try { needToEnablePrivilege = NeedToEnablePrivilege(_computerName, invokeObject.Name, ref isLocal); if (needToEnablePrivilege) { if (!(isLocal && PlatformInvokes.EnableTokenPrivilege(ComputerWMIHelper.SE_SHUTDOWN_NAME, ref currentPrivilegeState)) && !(!isLocal && PlatformInvokes.EnableTokenPrivilege(ComputerWMIHelper.SE_REMOTE_SHUTDOWN_NAME, ref currentPrivilegeState))) { string message = StringUtil.Format(ComputerResources.PrivilegeNotEnabled, _computerName, isLocal ? ComputerWMIHelper.SE_SHUTDOWN_NAME : ComputerWMIHelper.SE_REMOTE_SHUTDOWN_NAME); InvalidOperationException e = new InvalidOperationException(message); internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } } if (invokeObject.Path != null) { mPath.Server = _computerName; 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(_computerName, invokeObject.Namespace), options); ManagementClass mClass = new ManagementClass(invokeObject.Class); mObject = mClass; mObject.Scope = scope; } ManagementBaseObject inputParameters = mObject.GetMethodParameters(invokeObject.Name); if (invokeObject.ArgumentList != null) { int inParamCount = invokeObject.ArgumentList.Length; foreach (PropertyData property in inputParameters.Properties) { if (inParamCount == 0) break; property.Value = invokeObject.ArgumentList[invokeObject.ArgumentList.Length - inParamCount]; inParamCount--; } } if (needToEnablePrivilege) { ManagementBaseObject result = mObject.InvokeMethod(invokeObject.Name, inputParameters, null); Dbg.Diagnostics.Assert(result != null, "result cannot be null if the Join method is invoked"); int returnCode = Convert.ToInt32(result["ReturnValue"], CultureInfo.CurrentCulture); if (returnCode != 0) { var e = new Win32Exception(returnCode); internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } else { ShutdownComplete.SafeInvoke(this, null); } } else { mObject.InvokeMethod(_results, invokeObject.Name, inputParameters, null); } } catch (ManagementException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.Runtime.InteropServices.COMException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.UnauthorizedAccessException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } finally { // Restore the previous privilege state if something unexpected happened if (needToEnablePrivilege) { PlatformInvokes.RestoreTokenPrivilege( isLocal ? ComputerWMIHelper.SE_SHUTDOWN_NAME : ComputerWMIHelper.SE_REMOTE_SHUTDOWN_NAME, ref currentPrivilegeState); } } } }
/// <summary> /// Do the actual connection to remote machine for Get-WMIObject cmdlet and raise operation complete event. /// </summary> private void ConnectGetWMI() { GetWmiObjectCommand getObject = (GetWmiObjectCommand)_wmiObject; _state = WmiState.Running; RaiseWmiOperationState(null, WmiState.Running); ConnectionOptions options = getObject.GetConnectionOption(); if (getObject.List.IsPresent) { if (!getObject.ValidateClassFormat()) { ArgumentException e = new ArgumentException( String.Format( Thread.CurrentThread.CurrentCulture, "Class", getObject.Class)); internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } try { if (getObject.Recurse.IsPresent) { ArrayList namespaceArray = new ArrayList(); ArrayList sinkArray = new ArrayList(); ArrayList connectArray = new ArrayList(); // Optimization for remote namespace int currentNamesapceCount = 0; namespaceArray.Add(getObject.Namespace); bool topNamespace = true; while (currentNamesapceCount < namespaceArray.Count) { string connectNamespace = (string)namespaceArray[currentNamesapceCount]; ManagementScope scope = new ManagementScope(WMIHelper.GetScopeString(_computerName, connectNamespace), options); scope.Connect(); ManagementClass namespaceClass = new ManagementClass(scope, new ManagementPath("__Namespace"), new ObjectGetOptions()); foreach (ManagementBaseObject obj in namespaceClass.GetInstances()) { if (!getObject.IsLocalizedNamespace((string)obj["Name"])) { namespaceArray.Add(connectNamespace + "\\" + obj["Name"]); } } if (topNamespace) { topNamespace = false; sinkArray.Add(_results); } else { sinkArray.Add(_job.GetNewSink()); } connectArray.Add(scope); currentNamesapceCount++; } if ((sinkArray.Count != namespaceArray.Count) || (connectArray.Count != namespaceArray.Count)) // not expected throw exception { internalException = new InvalidOperationException(); _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); return; } currentNamesapceCount = 0; while (currentNamesapceCount < namespaceArray.Count) { string connectNamespace = (string)namespaceArray[currentNamesapceCount]; ManagementObjectSearcher searcher = getObject.GetObjectList((ManagementScope)connectArray[currentNamesapceCount]); if (searcher == null) { currentNamesapceCount++; continue; } if (topNamespace) { topNamespace = false; searcher.Get(_results); } else { searcher.Get((ManagementOperationObserver)sinkArray[currentNamesapceCount]); } currentNamesapceCount++; } } else { ManagementScope scope = new ManagementScope(WMIHelper.GetScopeString(_computerName, getObject.Namespace), options); scope.Connect(); ManagementObjectSearcher searcher = getObject.GetObjectList(scope); if (searcher == null) throw new ManagementException(); searcher.Get(_results); } } catch (ManagementException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.Runtime.InteropServices.COMException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.UnauthorizedAccessException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } return; } string queryString = string.IsNullOrEmpty(getObject.Query) ? GetWmiQueryString() : getObject.Query; ObjectQuery query = new ObjectQuery(queryString.ToString()); try { ManagementScope scope = new ManagementScope(WMIHelper.GetScopeString(_computerName, getObject.Namespace), options); EnumerationOptions enumOptions = new EnumerationOptions(); enumOptions.UseAmendedQualifiers = getObject.Amended; enumOptions.DirectRead = getObject.DirectRead; ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query, enumOptions); // Execute the WMI command for each count value. for (int i = 0; i < _cmdCount; ++i) { searcher.Get(_results); } } catch (ManagementException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.Runtime.InteropServices.COMException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } catch (System.UnauthorizedAccessException e) { internalException = e; _state = WmiState.Failed; RaiseOperationCompleteEvent(null, OperationState.StopComplete); } }
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; } }