MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#region Using directives5 6using System.Collections.Generic;7using System.Management.Automation;8 9#endregion10 11namespace Microsoft.Management.Infrastructure.CimCmdlets12{13 /// <summary>14 /// Containing all information originated from15 /// the parameters of <see cref="GetCimClassCommand"/>16 /// </summary>17 internal class CimGetCimClassContext : XOperationContextBase18 {19 /// <summary>20 /// Initializes a new instance of the <see cref="CimGetCimClassContext"/> class.21 /// </summary>22 /// <param name="methodName"></param>23 /// <param name="propertyName"></param>24 /// <param name="qualifierName"></param>25 internal CimGetCimClassContext(26 string theClassName,27 string theMethodName,28 string thePropertyName,29 string theQualifierName)30 {31 this.ClassName = theClassName;32 this.MethodName = theMethodName;33 this.PropertyName = thePropertyName;34 this.QualifierName = theQualifierName;35 }36 37 /// <summary>38 /// <para>39 /// The following is the definition of the input parameter "ClassName".40 /// </para>41 /// <para>42 /// Wildcard expansion should be allowed.43 /// </para>44 /// </summary>45 public string ClassName { get; set; }46 47 /// <summary>48 /// <para>49 /// The following is the definition of the input parameter "MethodName",50 /// Which may contains wildchar.51 /// Then Filter the <see cref="CimClass"/> by given methodname52 /// </para>53 /// </summary>54 internal string MethodName { get; }55 56 /// <summary>57 /// <para>58 /// The following is the definition of the input parameter "PropertyName",59 /// Which may contains wildchar.60 /// Filter the <see cref="CimClass"/> by given property name.61 /// </para>62 /// </summary>63 internal string PropertyName { get; }64 65 /// <summary>66 /// <para>67 /// The following is the definition of the input parameter "QualifierName",68 /// Which may contains wildchar.69 /// Filter the <see cref="CimClass"/> by given methodname70 /// </para>71 /// </summary>72 internal string QualifierName { get; }73 }74 75 /// <summary>76 /// <para>77 /// Implements operations of get-cimclass cmdlet.78 /// </para>79 /// </summary>80 internal sealed class CimGetCimClass : CimAsyncOperation81 {82 /// <summary>83 /// Initializes a new instance of the <see cref="CimGetCimClass"/> class.84 /// </summary>85 public CimGetCimClass()86 : base()87 {88 }89 90 /// <summary>91 /// <para>92 /// Base on parametersetName to retrieve <see cref="CimClass"/>93 /// </para>94 /// </summary>95 /// <param name="cmdlet"><see cref="GetCimClassCommand"/> object.</param>96 public void GetCimClass(GetCimClassCommand cmdlet)97 {98 List<CimSessionProxy> proxys = new();99 string nameSpace = ConstValue.GetNamespace(cmdlet.Namespace);100 string className = cmdlet.ClassName ?? @"*";101 CimGetCimClassContext context = new(102 cmdlet.ClassName,103 cmdlet.MethodName,104 cmdlet.PropertyName,105 cmdlet.QualifierName);106 switch (cmdlet.ParameterSetName)107 {108 case CimBaseCommand.ComputerSetName:109 {110 IEnumerable<string> computerNames = ConstValue.GetComputerNames(111 cmdlet.ComputerName);112 foreach (string computerName in computerNames)113 {114 CimSessionProxy proxy = CreateSessionProxy(computerName, cmdlet);115 proxy.ContextObject = context;116 proxys.Add(proxy);117 }118 }119 120 break;121 case CimBaseCommand.SessionSetName:122 {123 foreach (CimSession session in cmdlet.CimSession)124 {125 CimSessionProxy proxy = CreateSessionProxy(session, cmdlet);126 proxy.ContextObject = context;127 proxys.Add(proxy);128 }129 }130 131 break;132 default:133 return;134 }135 136 if (WildcardPattern.ContainsWildcardCharacters(className))137 {138 // retrieve all classes and then filter based on139 // classname, propertyname, methodname, and qualifiername140 foreach (CimSessionProxy proxy in proxys)141 {142 proxy.EnumerateClassesAsync(nameSpace);143 }144 }145 else146 {147 foreach (CimSessionProxy proxy in proxys)148 {149 proxy.GetClassAsync(nameSpace, className);150 }151 }152 }153 154 #region private methods155 156 /// <summary>157 /// <para>158 /// Set <see cref="CimSessionProxy"/> properties159 /// </para>160 /// </summary>161 /// <param name="proxy"></param>162 /// <param name="cmdlet"></param>163 private static void SetSessionProxyProperties(164 ref CimSessionProxy proxy,165 GetCimClassCommand cmdlet)166 {167 proxy.OperationTimeout = cmdlet.OperationTimeoutSec;168 proxy.Amended = cmdlet.Amended;169 }170 171 /// <summary>172 /// <para>173 /// Create <see cref="CimSessionProxy"/> and set properties174 /// </para>175 /// </summary>176 /// <param name="computerName"></param>177 /// <param name="cmdlet"></param>178 /// <returns></returns>179 private CimSessionProxy CreateSessionProxy(180 string computerName,181 GetCimClassCommand cmdlet)182 {183 CimSessionProxy proxy = new CimSessionProxyGetCimClass(computerName);184 this.SubscribeEventAndAddProxytoCache(proxy);185 SetSessionProxyProperties(ref proxy, cmdlet);186 return proxy;187 }188 189 /// <summary>190 /// Create <see cref="CimSessionProxy"/> and set properties.191 /// </summary>192 /// <param name="session"></param>193 /// <param name="cmdlet"></param>194 /// <returns></returns>195 private CimSessionProxy CreateSessionProxy(196 CimSession session,197 GetCimClassCommand cmdlet)198 {199 CimSessionProxy proxy = new CimSessionProxyGetCimClass(session);200 this.SubscribeEventAndAddProxytoCache(proxy);201 SetSessionProxyProperties(ref proxy, cmdlet);202 return proxy;203 }204 205 #endregion206 207 }208}209 