File size: 6,702 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | // 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>
[Cmdlet(VerbsCommon.Remove, "WmiObject", DefaultParameterSetName = "class", SupportsShouldProcess = true,
HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113381", RemotingCapability = RemotingCapability.OwnedByCommand)]
public class RemoveWmiObject : WmiBaseCmdlet
{
#region Parameters
/// <summary>
/// The WMI Object to use.
/// </summary>
[Parameter(ValueFromPipeline = true, Mandatory = true, ParameterSetName = "object")]
public ManagementObject InputObject
{
get { return _inputObject; }
set { _inputObject = value; }
}
/// <summary>
/// The WMI Path to use.
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = "path")]
public string Path
{
get { return _path; }
set { _path = value; }
}
/// <summary>
/// The WMI class to use.
/// </summary>
[Parameter(Position = 0, Mandatory = true, ParameterSetName = "class")]
public string Class
{
get { return _className; }
set { _className = value; }
}
#endregion Parameters
#region parameter data
private string _path = null;
private string _className = null;
private ManagementObject _inputObject = null;
#endregion parameter data
#region Command code
/// <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);
}
}
}
}
#endregion Command code
}
}
|