MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#region Using directives5 6using System;7using System.Collections.Generic;8using System.Diagnostics.CodeAnalysis;9using System.Management.Automation;10 11#endregion12 13/******************************************************************************14 * warning 28750: Banned usage of lstrlen and its variants: lstrlenW is a15 * banned API for improved error handling purposes.16 *****************************************************************************/17namespace Microsoft.Management.Infrastructure.CimCmdlets18{19 /// <summary>20 /// This Cmdlet allows the to remove, or terminate, one or more CimSession(s).21 /// </summary>22 [Alias("rcms")]23 [Cmdlet(VerbsCommon.Remove, "CimSession",24 SupportsShouldProcess = true,25 DefaultParameterSetName = CimSessionSet,26 HelpUri = "https://go.microsoft.com/fwlink/?LinkId=227968")]27 public sealed class RemoveCimSessionCommand : CimBaseCommand28 {29 #region constructor30 31 /// <summary>32 /// Initializes a new instance of the <see cref="RemoveCimSessionCommand"/> class.33 /// </summary>34 public RemoveCimSessionCommand()35 : base(parameters, parameterSets)36 {37 }38 39 #endregion40 41 #region parameters42 43 /// <summary>44 /// The following is the definition of the input parameter "CimSession".45 /// Specifies one or more CimSession object to be removed from the local PS46 /// session/runspace.47 /// </summary>48 [Parameter(49 Mandatory = true,50 Position = 0,51 ValueFromPipeline = true,52 ValueFromPipelineByPropertyName = true,53 ParameterSetName = CimSessionSet)]54 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]55 public CimSession[] CimSession56 {57 get58 {59 return cimsession;60 }61 62 set63 {64 cimsession = value;65 base.SetParameter(value, nameCimSession);66 }67 }68 69 private CimSession[] cimsession;70 71 /// <summary>72 /// <para>The following is the definition of the input parameter "ComputerName".73 /// Specified one or more computer names for which all CimSession(s)74 /// (connections) should be removed (terminated).</para>75 /// <para>This is the only optional parameter. If no value for this parameter is76 /// provided, all CimSession(s) are terminated.</para>77 /// </summary>78 [Alias(AliasCN, AliasServerName)]79 [Parameter(80 Mandatory = true,81 Position = 0,82 ValueFromPipelineByPropertyName = true,83 ParameterSetName = ComputerNameSet)]84 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]85 public string[] ComputerName86 {87 get88 {89 return computername;90 }91 92 set93 {94 computername = value;95 base.SetParameter(value, nameComputerName);96 }97 }98 99 private string[] computername;100 101 /// <summary>102 /// The following is the definition of the input parameter "Id".103 /// Specifies the friendly Id(s) of the CimSession(s) that should be removed104 /// (terminated).105 /// </summary>106 [Parameter(Mandatory = true,107 Position = 0,108 ValueFromPipelineByPropertyName = true,109 ParameterSetName = SessionIdSet)]110 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]111 public uint[] Id112 {113 get114 {115 return id;116 }117 118 set119 {120 id = value;121 base.SetParameter(value, nameId);122 }123 }124 125 private uint[] id;126 127 /// <summary>128 /// The following is the definition of the input parameter "InstanceId".129 /// Specifies one or more automatically generated InstanceId(s) (GUIDs) of the130 /// CimSession(s) that should be removed (terminated).131 /// </summary>132 [Parameter(133 Mandatory = true,134 ValueFromPipelineByPropertyName = true,135 ParameterSetName = InstanceIdSet)]136 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]137 public Guid[] InstanceId138 {139 get140 {141 return instanceid;142 }143 144 set145 {146 instanceid = value;147 base.SetParameter(value, nameInstanceId);148 }149 }150 151 private Guid[] instanceid;152 153 /// <summary>154 /// The following is the definition of the input parameter "Name".155 /// Specifies one or more of friendly Names of the CimSession(s) that should be156 /// removed (terminated).157 /// </summary>158 [Parameter(159 Mandatory = true,160 ValueFromPipelineByPropertyName = true,161 ParameterSetName = NameSet)]162 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]163 public string[] Name164 {165 get166 {167 return name;168 }169 170 set171 {172 name = value;173 base.SetParameter(value, nameName);174 }175 }176 177 private string[] name;178 179 #endregion180 181 /// <summary>182 /// BeginProcessing method.183 /// </summary>184 protected override void BeginProcessing()185 {186 this.cimRemoveSession = new CimRemoveSession();187 this.AtBeginProcess = false;188 }189 190 /// <summary>191 /// ProcessRecord method.192 /// </summary>193 protected override void ProcessRecord()194 {195 base.CheckParameterSet();196 this.cimRemoveSession.RemoveCimSession(this);197 }198 199 #region private members200 /// <summary>201 /// <see cref="CimRemoveSession"/> object used to remove the session from202 /// session cache.203 /// </summary>204 private CimRemoveSession cimRemoveSession;205 206 #region const string of parameter names207 internal const string nameCimSession = "CimSession";208 internal const string nameComputerName = "ComputerName";209 internal const string nameId = "Id";210 internal const string nameInstanceId = "InstanceId";211 internal const string nameName = "Name";212 #endregion213 214 /// <summary>215 /// Static parameter definition entries.216 /// </summary>217 private static readonly Dictionary<string, HashSet<ParameterDefinitionEntry>> parameters = new()218 {219 {220 nameCimSession, new HashSet<ParameterDefinitionEntry> {221 new ParameterDefinitionEntry(CimBaseCommand.CimSessionSet, true),222 }223 },224 {225 nameComputerName, new HashSet<ParameterDefinitionEntry> {226 new ParameterDefinitionEntry(CimBaseCommand.ComputerNameSet, true),227 }228 },229 {230 nameId, new HashSet<ParameterDefinitionEntry> {231 new ParameterDefinitionEntry(CimBaseCommand.SessionIdSet, true),232 }233 },234 {235 nameInstanceId, new HashSet<ParameterDefinitionEntry> {236 new ParameterDefinitionEntry(CimBaseCommand.InstanceIdSet, true),237 }238 },239 {240 nameName, new HashSet<ParameterDefinitionEntry> {241 new ParameterDefinitionEntry(CimBaseCommand.NameSet, true),242 }243 },244 };245 246 /// <summary>247 /// Static parameter set entries.248 /// </summary>249 private static readonly Dictionary<string, ParameterSetEntry> parameterSets = new()250 {251 { CimBaseCommand.CimSessionSet, new ParameterSetEntry(1, true) },252 { CimBaseCommand.ComputerNameSet, new ParameterSetEntry(1) },253 { CimBaseCommand.SessionIdSet, new ParameterSetEntry(1) },254 { CimBaseCommand.InstanceIdSet, new ParameterSetEntry(1) },255 { CimBaseCommand.NameSet, new ParameterSetEntry(1) },256 };257 #endregion258 }259}260 