File size: 11,101 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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#region Using directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
#endregion
namespace Microsoft.Management.Infrastructure.CimCmdlets
{
/// <summary>
/// Containing all necessary information originated from
/// the parameters of <see cref="InvokeCimMethodCommand"/>
/// </summary>
internal class CimSetCimInstanceContext : XOperationContextBase
{
/// <summary>
/// Initializes a new instance of the <see cref="CimSetCimInstanceContext"/> class.
/// </summary>
/// <param name="theNamespace"></param>
/// <param name="theCollection"></param>
/// <param name="theProxy"></param>
internal CimSetCimInstanceContext(string theNamespace,
IDictionary theProperty,
CimSessionProxy theProxy,
string theParameterSetName,
bool passThru)
{
this.proxy = theProxy;
this.Property = theProperty;
this.nameSpace = theNamespace;
this.ParameterSetName = theParameterSetName;
this.PassThru = passThru;
}
/// <summary>
/// <para>property value</para>
/// </summary>
internal IDictionary Property { get; }
/// <summary>
/// <para>parameter set name</para>
/// </summary>
internal string ParameterSetName { get; }
/// <summary>
/// <para>PassThru value</para>
/// </summary>
internal bool PassThru { get; }
}
/// <summary>
/// <para>
/// Implements operations of set-ciminstance cmdlet.
/// </para>
/// </summary>
internal sealed class CimSetCimInstance : CimGetInstance
{
/// <summary>
/// Initializes a new instance of the <see cref="CimSetCimInstance"/> class.
/// </summary>
public CimSetCimInstance()
: base()
{
}
/// <summary>
/// <para>
/// Base on parametersetName to set ciminstances
/// </para>
/// </summary>
/// <param name="cmdlet"><see cref="SetCimInstanceCommand"/> object.</param>
public void SetCimInstance(SetCimInstanceCommand cmdlet)
{
IEnumerable<string> computerNames = ConstValue.GetComputerNames(
GetComputerName(cmdlet));
List<CimSessionProxy> proxys = new();
switch (cmdlet.ParameterSetName)
{
case CimBaseCommand.CimInstanceComputerSet:
foreach (string computerName in computerNames)
{
// create CimSessionProxySetCimInstance object internally
proxys.Add(CreateSessionProxy(computerName, cmdlet.CimInstance, cmdlet, cmdlet.PassThru));
}
break;
case CimBaseCommand.CimInstanceSessionSet:
foreach (CimSession session in GetCimSession(cmdlet))
{
// create CimSessionProxySetCimInstance object internally
proxys.Add(CreateSessionProxy(session, cmdlet, cmdlet.PassThru));
}
break;
default:
break;
}
switch (cmdlet.ParameterSetName)
{
case CimBaseCommand.CimInstanceComputerSet:
case CimBaseCommand.CimInstanceSessionSet:
string nameSpace = ConstValue.GetNamespace(GetCimInstanceParameter(cmdlet).CimSystemProperties.Namespace);
string target = cmdlet.CimInstance.ToString();
foreach (CimSessionProxy proxy in proxys)
{
if (!cmdlet.ShouldProcess(target, action))
{
return;
}
Exception exception = null;
CimInstance instance = cmdlet.CimInstance;
// For CimInstance parameter sets, Property is an optional parameter
if (cmdlet.Property != null)
{
if (!SetProperty(cmdlet.Property, ref instance, ref exception))
{
cmdlet.ThrowTerminatingError(exception, action);
return;
}
}
proxy.ModifyInstanceAsync(nameSpace, instance);
}
break;
case CimBaseCommand.QueryComputerSet:
case CimBaseCommand.QuerySessionSet:
GetCimInstanceInternal(cmdlet);
break;
default:
break;
}
}
/// <summary>
/// <para>
/// Set <see cref="CimInstance"/> with properties specified in cmdlet
/// </para>
/// </summary>
/// <param name="cimInstance"></param>
public void SetCimInstance(CimInstance cimInstance, CimSetCimInstanceContext context, CmdletOperationBase cmdlet)
{
DebugHelper.WriteLog("CimSetCimInstance::SetCimInstance", 4);
if (!cmdlet.ShouldProcess(cimInstance.ToString(), action))
{
return;
}
Exception exception = null;
if (!SetProperty(context.Property, ref cimInstance, ref exception))
{
cmdlet.ThrowTerminatingError(exception, action);
return;
}
CimSessionProxy proxy = CreateCimSessionProxy(context.Proxy, context.PassThru);
proxy.ModifyInstanceAsync(cimInstance.CimSystemProperties.Namespace, cimInstance);
}
#region private members
/// <summary>
/// <para>
/// Set the properties value to be modified to the given
/// <see cref="CimInstance"/>
/// </para>
/// </summary>
/// <param name="properties"></param>
/// <param name="cimInstance"></param>
/// <param name="terminationMessage"></param>
/// <returns></returns>
private bool SetProperty(IDictionary properties, ref CimInstance cimInstance, ref Exception exception)
{
DebugHelper.WriteLogEx();
if (properties.Count == 0)
{
// simply ignore if empty properties was provided
return true;
}
IDictionaryEnumerator enumerator = properties.GetEnumerator();
while (enumerator.MoveNext())
{
object value = GetBaseObject(enumerator.Value);
string key = enumerator.Key.ToString();
DebugHelper.WriteLog("Input property name '{0}' with value '{1}'", 1, key, value);
try
{
CimProperty property = cimInstance.CimInstanceProperties[key];
// modify existing property value if found
if (property != null)
{
if ((property.Flags & CimFlags.ReadOnly) == CimFlags.ReadOnly)
{
// can not modify ReadOnly property
exception = new CimException(string.Format(CultureInfo.CurrentUICulture,
CimCmdletStrings.CouldNotModifyReadonlyProperty, key, cimInstance));
return false;
}
// allow modify the key property value as long as it is not readonly,
// then the modified ciminstance is stand for a different CimInstance
DebugHelper.WriteLog("Set property name '{0}' has old value '{1}'", 4, key, property.Value);
property.Value = value;
}
else // For dynamic instance, it is valid to add a new property
{
CimProperty newProperty;
if (value == null)
{
newProperty = CimProperty.Create(
key,
value,
CimType.String,
CimFlags.Property);
}
else
{
CimType referenceType = CimType.Unknown;
object referenceObject = GetReferenceOrReferenceArrayObject(value, ref referenceType);
if (referenceObject != null)
{
newProperty = CimProperty.Create(
key,
referenceObject,
referenceType,
CimFlags.Property);
}
else
{
newProperty = CimProperty.Create(
key,
value,
CimFlags.Property);
}
}
try
{
cimInstance.CimInstanceProperties.Add(newProperty);
}
catch (CimException e)
{
if (e.NativeErrorCode == NativeErrorCode.Failed)
{
string errorMessage = string.Format(CultureInfo.CurrentUICulture,
CimCmdletStrings.UnableToAddPropertyToInstance,
newProperty.Name,
cimInstance);
exception = new CimException(errorMessage, e);
}
else
{
exception = e;
}
return false;
}
DebugHelper.WriteLog("Add non-key property name '{0}' with value '{1}'.", 3, key, value);
}
}
catch (Exception e)
{
DebugHelper.WriteLog("Exception {0}", 4, e);
exception = e;
return false;
}
}
return true;
}
#endregion
#region const strings
/// <summary>
/// Action.
/// </summary>
private const string action = @"Set-CimInstance";
#endregion
}
}
|