Windows-powershell / PowerShell-master /src /Microsoft.PowerShell.Commands.Management /commands /management /RemoveWMIObjectCommand.cs
| // Copyright (c) Microsoft Corporation. | |
| // Licensed under the MIT License. | |
| using System; | |
| using System.Management; | |
| using System.Management.Automation; | |
| namespace Microsoft.PowerShell.Commands | |
| { | |
| /// <summary> | |
| /// A command to Remove WMI Object. | |
| /// </summary> | |
| [ | |
| ] | |
| public class RemoveWmiObject : WmiBaseCmdlet | |
| { | |
| /// <summary> | |
| /// The WMI Object to use. | |
| /// </summary> | |
| [] | |
| public ManagementObject InputObject | |
| { | |
| get { return _inputObject; } | |
| set { _inputObject = value; } | |
| } | |
| /// <summary> | |
| /// The WMI Path to use. | |
| /// </summary> | |
| [] | |
| public string Path | |
| { | |
| get { return _path; } | |
| set { _path = value; } | |
| } | |
| /// <summary> | |
| /// The WMI class to use. | |
| /// </summary> | |
| [] | |
| public string Class | |
| { | |
| get { return _className; } | |
| set { _className = value; } | |
| } | |
| private string _path = null; | |
| private string _className = null; | |
| private ManagementObject _inputObject = null; | |
| /// <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); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |