File size: 18,991 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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#region Using directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Management.Automation;
#endregion
namespace Microsoft.Management.Infrastructure.CimCmdlets
{
/// <summary>
/// <para>
/// This Cmdlet creates an instance of a CIM class based on the class
/// definition, which is an instance factory
/// </para>
/// <para>
/// If -ClientOnly is not specified, New-CimInstance will create a new instance
/// on the server, otherwise just create client in-memory instance
/// </para>
/// </summary>
[Alias("ncim")]
[Cmdlet(VerbsCommon.New, "CimInstance", DefaultParameterSetName = CimBaseCommand.ClassNameComputerSet, SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=227963")]
[OutputType(typeof(CimInstance))]
public class NewCimInstanceCommand : CimBaseCommand
{
#region constructor
/// <summary>
/// Initializes a new instance of the <see cref="NewCimInstanceCommand"/> class.
/// </summary>
public NewCimInstanceCommand()
: base(parameters, parameterSets)
{
DebugHelper.WriteLogEx();
}
#endregion
#region parameters
/// <summary>
/// The following is the definition of the input parameter "ClassName".
/// Name of the Class to use to create Instance.
/// </summary>
[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ClassNameSessionSet)]
[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ClassNameComputerSet)]
public string ClassName
{
get
{
return className;
}
set
{
className = value;
base.SetParameter(value, nameClassName);
}
}
private string className;
/// <summary>
/// <para>
/// The following is the definition of the input parameter "ResourceUri".
/// Define the Resource Uri for which the instances are retrieved.
/// </para>
/// </summary>
[Parameter(Mandatory = true,
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ResourceUriSessionSet)]
[Parameter(Mandatory = true,
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ResourceUriComputerSet)]
public Uri ResourceUri
{
get
{
return resourceUri;
}
set
{
this.resourceUri = value;
base.SetParameter(value, nameResourceUri);
}
}
private Uri resourceUri;
/// <summary>
/// <para>
/// The following is the definition of the input parameter "Key".
/// Enables the user to specify list of key property name.
/// </para>
/// <para>
/// Example: -Key {"K1", "K2"}
/// </para>
/// </summary>
[Parameter(
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ClassNameSessionSet)]
[Parameter(
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ClassNameComputerSet)]
[Parameter(
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ResourceUriSessionSet)]
[Parameter(
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ResourceUriComputerSet)]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public string[] Key
{
get
{
return key;
}
set
{
key = value;
base.SetParameter(value, nameKey);
}
}
private string[] key;
/// <summary>
/// The following is the definition of the input parameter "CimClass".
/// The CimClass is used to create Instance.
/// </summary>
[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipeline = true,
ParameterSetName = CimClassSessionSet)]
[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipeline = true,
ParameterSetName = CimClassComputerSet)]
public CimClass CimClass
{
get
{
return cimClass;
}
set
{
cimClass = value;
base.SetParameter(value, nameCimClass);
}
}
private CimClass cimClass;
/// <summary>
/// <para>
/// The following is the definition of the input parameter "Property".
/// Enables the user to specify instances with specific property values.
/// </para>
/// <para>
/// Example: -Property @{P1="Value1";P2="Value2"}
/// </para>
/// </summary>
[Parameter(
Position = 1,
ValueFromPipelineByPropertyName = true)]
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
[Alias("Arguments")]
public IDictionary Property { get; set; }
/// <summary>
/// The following is the definition of the input parameter "Namespace".
/// Namespace used to look for the classes under to store the instances.
/// Default namespace is 'root\cimv2'
/// </summary>
[Parameter(
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ClassNameSessionSet)]
[Parameter(
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ClassNameComputerSet)]
[Parameter(
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ResourceUriSessionSet)]
[Parameter(
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ResourceUriComputerSet)]
public string Namespace
{
get
{
return nameSpace;
}
set
{
nameSpace = value;
base.SetParameter(value, nameNamespace);
}
}
private string nameSpace;
/// <summary>
/// The following is the definition of the input parameter "OperationTimeoutSec".
/// Operation Timeout of the cmdlet in seconds. Overrides the value in the Cim
/// Session.
/// </summary>
[Alias(AliasOT)]
[Parameter]
public uint OperationTimeoutSec { get; set; }
/// <summary>
/// <para>
/// The following is the definition of the input parameter "CimSession".
/// Identifies the CimSession which is to be used to create the instances.
/// </para>
/// </summary>
[Parameter(
Mandatory = true,
ValueFromPipeline = true,
ParameterSetName = CimBaseCommand.ClassNameSessionSet)]
[Parameter(
Mandatory = true,
ValueFromPipeline = true,
ParameterSetName = CimBaseCommand.ResourceUriSessionSet)]
[Parameter(
Mandatory = true,
ValueFromPipeline = true,
ParameterSetName = CimClassSessionSet)]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public CimSession[] CimSession
{
get
{
return cimSession;
}
set
{
cimSession = value;
base.SetParameter(value, nameCimSession);
}
}
private CimSession[] cimSession;
/// <summary>
/// <para>The following is the definition of the input parameter "ComputerName".
/// Provides the name of the computer from which to create the instances.
/// </para>
/// <para>
/// If no ComputerName is specified the default value is "localhost"
/// </para>
/// </summary>
[Alias(AliasCN, AliasServerName)]
[Parameter(
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ClassNameComputerSet)]
[Parameter(
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimBaseCommand.ResourceUriComputerSet)]
[Parameter(
ValueFromPipelineByPropertyName = true,
ParameterSetName = CimClassComputerSet)]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public string[] ComputerName
{
get
{
return computerName;
}
set
{
computerName = value;
base.SetParameter(value, nameComputerName);
}
}
private string[] computerName;
/// <summary>
/// <para>
/// The following is the definition of the input parameter "ClientOnly".
/// Indicates to create a client only ciminstance object, NOT on the server.
/// </para>
/// </summary>
[Alias("Local")]
[Parameter(
ParameterSetName = CimBaseCommand.ClassNameSessionSet)]
[Parameter(
ParameterSetName = CimBaseCommand.ClassNameComputerSet)]
[Parameter(
ParameterSetName = CimBaseCommand.CimClassComputerSet)]
[Parameter(
ParameterSetName = CimBaseCommand.CimClassSessionSet)]
public SwitchParameter ClientOnly
{
get
{
return clientOnly;
}
set
{
clientOnly = value;
base.SetParameter(value, nameClientOnly);
}
}
private SwitchParameter clientOnly;
#endregion
#region cmdlet methods
/// <summary>
/// BeginProcessing method.
/// </summary>
protected override void BeginProcessing()
{
this.CmdletOperation = new CmdletOperationBase(this);
this.AtBeginProcess = false;
}
/// <summary>
/// ProcessRecord method.
/// </summary>
protected override void ProcessRecord()
{
base.CheckParameterSet();
this.CheckArgument();
if (this.ClientOnly)
{
string conflictParameterName = null;
if (this.ComputerName != null)
{
conflictParameterName = @"ComputerName";
}
else if (this.CimSession != null)
{
conflictParameterName = @"CimSession";
}
if (conflictParameterName != null)
{
ThrowConflictParameterWasSet(@"New-CimInstance", conflictParameterName, @"ClientOnly");
return;
}
}
CimNewCimInstance cimNewCimInstance = this.GetOperationAgent() ?? CreateOperationAgent();
cimNewCimInstance.NewCimInstance(this);
cimNewCimInstance.ProcessActions(this.CmdletOperation);
}
/// <summary>
/// EndProcessing method.
/// </summary>
protected override void EndProcessing()
{
CimNewCimInstance cimNewCimInstance = this.GetOperationAgent();
cimNewCimInstance?.ProcessRemainActions(this.CmdletOperation);
}
#endregion
#region helper methods
/// <summary>
/// <para>
/// Get <see cref="CimNewCimInstance"/> object, which is
/// used to delegate all New-CimInstance operations.
/// </para>
/// </summary>
private CimNewCimInstance GetOperationAgent()
{
return this.AsyncOperation as CimNewCimInstance;
}
/// <summary>
/// <para>
/// Create <see cref="CimNewCimInstance"/> object, which is
/// used to delegate all New-CimInstance operations.
/// </para>
/// </summary>
/// <returns></returns>
private CimNewCimInstance CreateOperationAgent()
{
CimNewCimInstance cimNewCimInstance = new();
this.AsyncOperation = cimNewCimInstance;
return cimNewCimInstance;
}
/// <summary>
/// Check argument value.
/// </summary>
private void CheckArgument()
{
switch (this.ParameterSetName)
{
case CimBaseCommand.ClassNameComputerSet:
case CimBaseCommand.ClassNameSessionSet:
// validate the classname
this.className = ValidationHelper.ValidateArgumentIsValidName(nameClassName, this.className);
break;
default:
break;
}
}
#endregion
#region private members
#region const string of parameter names
internal const string nameClassName = "ClassName";
internal const string nameResourceUri = "ResourceUri";
internal const string nameKey = "Key";
internal const string nameCimClass = "CimClass";
internal const string nameProperty = "Property";
internal const string nameNamespace = "Namespace";
internal const string nameCimSession = "CimSession";
internal const string nameComputerName = "ComputerName";
internal const string nameClientOnly = "ClientOnly";
#endregion
/// <summary>
/// Static parameter definition entries.
/// </summary>
private static readonly Dictionary<string, HashSet<ParameterDefinitionEntry>> parameters = new()
{
{
nameClassName, new HashSet<ParameterDefinitionEntry> {
new ParameterDefinitionEntry(CimBaseCommand.ClassNameSessionSet, true),
new ParameterDefinitionEntry(CimBaseCommand.ClassNameComputerSet, true),
}
},
{
nameResourceUri, new HashSet<ParameterDefinitionEntry> {
new ParameterDefinitionEntry(CimBaseCommand.ResourceUriSessionSet, true),
new ParameterDefinitionEntry(CimBaseCommand.ResourceUriComputerSet, true),
}
},
{
nameKey, new HashSet<ParameterDefinitionEntry> {
new ParameterDefinitionEntry(CimBaseCommand.ClassNameSessionSet, false),
new ParameterDefinitionEntry(CimBaseCommand.ClassNameComputerSet, false),
new ParameterDefinitionEntry(CimBaseCommand.ResourceUriComputerSet, false),
new ParameterDefinitionEntry(CimBaseCommand.ResourceUriSessionSet, false),
}
},
{
nameCimClass, new HashSet<ParameterDefinitionEntry> {
new ParameterDefinitionEntry(CimBaseCommand.CimClassSessionSet, true),
new ParameterDefinitionEntry(CimBaseCommand.CimClassComputerSet, true),
}
},
{
nameNamespace, new HashSet<ParameterDefinitionEntry> {
new ParameterDefinitionEntry(CimBaseCommand.ClassNameSessionSet, false),
new ParameterDefinitionEntry(CimBaseCommand.ClassNameComputerSet, false),
new ParameterDefinitionEntry(CimBaseCommand.ResourceUriComputerSet, false),
new ParameterDefinitionEntry(CimBaseCommand.ResourceUriSessionSet, false),
}
},
{
nameCimSession, new HashSet<ParameterDefinitionEntry> {
new ParameterDefinitionEntry(CimBaseCommand.ClassNameSessionSet, true),
new ParameterDefinitionEntry(CimBaseCommand.ResourceUriSessionSet, true),
new ParameterDefinitionEntry(CimBaseCommand.CimClassSessionSet, true),
}
},
{
nameComputerName, new HashSet<ParameterDefinitionEntry> {
new ParameterDefinitionEntry(CimBaseCommand.ClassNameComputerSet, false),
new ParameterDefinitionEntry(CimBaseCommand.ResourceUriComputerSet, false),
new ParameterDefinitionEntry(CimBaseCommand.CimClassComputerSet, false),
}
},
{
nameClientOnly, new HashSet<ParameterDefinitionEntry> {
new ParameterDefinitionEntry(CimBaseCommand.ClassNameSessionSet, true),
new ParameterDefinitionEntry(CimBaseCommand.ClassNameComputerSet, true),
new ParameterDefinitionEntry(CimBaseCommand.CimClassSessionSet, true),
new ParameterDefinitionEntry(CimBaseCommand.CimClassComputerSet, true),
}
},
};
/// <summary>
/// Static parameter set entries.
/// </summary>
private static readonly Dictionary<string, ParameterSetEntry> parameterSets = new()
{
{ CimBaseCommand.ClassNameSessionSet, new ParameterSetEntry(2) },
{ CimBaseCommand.ClassNameComputerSet, new ParameterSetEntry(1, true) },
{ CimBaseCommand.CimClassSessionSet, new ParameterSetEntry(2) },
{ CimBaseCommand.CimClassComputerSet, new ParameterSetEntry(1) },
{ CimBaseCommand.ResourceUriSessionSet, new ParameterSetEntry(2) },
{ CimBaseCommand.ResourceUriComputerSet, new ParameterSetEntry(1) },
};
#endregion
}
}
|