MegaBites-AI/Windows-powershell
0308
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#region Using directives5using System;6using System.Collections.Generic;7using System.Diagnostics.CodeAnalysis;8using System.Management.Automation;9#endregion10 11namespace Microsoft.Management.Infrastructure.CimCmdlets12{13 /// <summary>14 /// The command returns zero, one or more CimSession objects that represent15 /// connections with remote computers established from the current PS Session.16 /// </summary>17 [Alias("gcms")]18 [Cmdlet(VerbsCommon.Get, "CimSession", DefaultParameterSetName = ComputerNameSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=227966")]19 [OutputType(typeof(CimSession))]20 public sealed class GetCimSessionCommand : CimBaseCommand21 {22 #region constructor23 24 /// <summary>25 /// Initializes a new instance of the <see cref="GetCimSessionCommand"/> class.26 /// </summary>27 public GetCimSessionCommand()28 : base(parameters, parameterSets)29 {30 DebugHelper.WriteLogEx();31 }32 33 #endregion34 35 #region parameters36 37 /// <summary>38 /// <para>39 /// The following is the definition of the input parameter "ComputerName".40 /// Specifies one or more connections by providing their ComputerName(s). The41 /// Cmdlet then gets CimSession(s) opened with those connections. This parameter42 /// is an alternative to using CimSession(s) that also identifies the remote43 /// computer(s).44 /// </para>45 /// <para>46 /// This is the only optional parameter of the Cmdlet. If not provided, the47 /// Cmdlet returns all CimSession(s) live/active in the runspace.48 /// </para>49 /// <para>50 /// If an instance of CimSession is pipelined to Get-CimSession, the51 /// ComputerName property of the instance is bound by name with this parameter.52 /// </para>53 /// </summary>54 [Alias(AliasCN, AliasServerName)]55 [Parameter(Position = 0,56 ValueFromPipelineByPropertyName = true,57 ParameterSetName = ComputerNameSet)]58 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]59 public string[] ComputerName60 {61 get62 {63 return computername;64 }65 66 set67 {68 computername = value;69 base.SetParameter(value, nameComputerName);70 }71 }72 73 private string[] computername;74 75 /// <summary>76 /// The following is the definition of the input parameter "Id".77 /// Specifies one or more numeric Id(s) for which to get CimSession(s).78 /// </summary>79 [Parameter(Mandatory = true,80 Position = 0,81 ValueFromPipelineByPropertyName = true,82 ParameterSetName = SessionIdSet)]83 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]84 public uint[] Id85 {86 get87 {88 return id;89 }90 91 set92 {93 id = value;94 base.SetParameter(value, nameId);95 }96 }97 98 private uint[] id;99 100 /// <summary>101 /// The following is the definition of the input parameter "InstanceID".102 /// Specifies one or Session Instance IDs.103 /// </summary>104 [Parameter(Mandatory = true,105 ValueFromPipelineByPropertyName = true,106 ParameterSetName = InstanceIdSet)]107 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]108 public Guid[] InstanceId109 {110 get111 {112 return instanceid;113 }114 115 set116 {117 instanceid = value;118 base.SetParameter(value, nameInstanceId);119 }120 }121 122 private Guid[] instanceid;123 124 /// <summary>125 /// The following is the definition of the input parameter "Name".126 /// Specifies one or more session Name(s) for which to get CimSession(s). The127 /// argument may contain wildcard characters.128 /// </summary>129 [Parameter(Mandatory = true,130 ValueFromPipelineByPropertyName = true,131 ParameterSetName = NameSet)]132 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]133 public string[] Name134 {135 get136 {137 return name;138 }139 140 set141 {142 name = value;143 base.SetParameter(value, nameName);144 }145 }146 147 private string[] name;148 149 #endregion150 151 #region cmdlet processing methods152 /// <summary>153 /// BeginProcessing method.154 /// </summary>155 protected override void BeginProcessing()156 {157 cimGetSession = new CimGetSession();158 this.AtBeginProcess = false;159 }160 161 /// <summary>162 /// ProcessRecord method.163 /// </summary>164 protected override void ProcessRecord()165 {166 base.CheckParameterSet();167 cimGetSession.GetCimSession(this);168 }169 170 #endregion171 172 #region private members173 /// <summary>174 /// <see cref="CimGetSession"/> object used to search CimSession from cache.175 /// </summary>176 private CimGetSession cimGetSession;177 178 #region const string of parameter names179 internal const string nameComputerName = "ComputerName";180 internal const string nameId = "Id";181 internal const string nameInstanceId = "InstanceId";182 internal const string nameName = "Name";183 #endregion184 185 /// <summary>186 /// Static parameter definition entries.187 /// </summary>188 private static readonly Dictionary<string, HashSet<ParameterDefinitionEntry>> parameters = new()189 {190 {191 nameComputerName, new HashSet<ParameterDefinitionEntry> {192 new ParameterDefinitionEntry(CimBaseCommand.ComputerNameSet, false),193 }194 },195 {196 nameId, new HashSet<ParameterDefinitionEntry> {197 new ParameterDefinitionEntry(CimBaseCommand.SessionIdSet, true),198 }199 },200 {201 nameInstanceId, new HashSet<ParameterDefinitionEntry> {202 new ParameterDefinitionEntry(CimBaseCommand.InstanceIdSet, true),203 }204 },205 {206 nameName, new HashSet<ParameterDefinitionEntry> {207 new ParameterDefinitionEntry(CimBaseCommand.NameSet, true),208 }209 },210 };211 212 /// <summary>213 /// Static parameter set entries.214 /// </summary>215 private static readonly Dictionary<string, ParameterSetEntry> parameterSets = new()216 {217 { CimBaseCommand.ComputerNameSet, new ParameterSetEntry(0, true) },218 { CimBaseCommand.SessionIdSet, new ParameterSetEntry(1) },219 { CimBaseCommand.InstanceIdSet, new ParameterSetEntry(1) },220 { CimBaseCommand.NameSet, new ParameterSetEntry(1) },221 };222 #endregion223 }224}225 