MegaBites-AI/Windows-powershell
0372
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.Globalization;9using System.Management.Automation;10using Microsoft.Management.Infrastructure.Options;11#endregion12 13namespace Microsoft.Management.Infrastructure.CimCmdlets14{15 /// <summary>16 /// Define Protocol type.17 /// </summary>18 public enum ProtocolType19 {20 Default,21 22 [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")]23 Dcom,24 25 [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")]26 Wsman27 }28 29 /// <summary>30 /// The Cmdlet allows the IT Pro to create a CimSessionOptions object that she/he31 /// can subsequently use to create one or more CimSession connections. The32 /// options object holds the CIM Session information that is less commonly set33 /// and used by the IT Pro, and most commonly defaulted.34 ///35 /// The Cmdlet has two parameter sets, one for WMMan options and one for DCOM36 /// options. Depending on the arguments the Cmdlet will return an instance of37 /// DComSessionOptions or WSManSessionOptions, which derive from38 /// CimSessionOptions.39 /// </summary>40 [Alias("ncso")]41 [Cmdlet(VerbsCommon.New, "CimSessionOption", DefaultParameterSetName = ProtocolNameParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=227969")]42 [OutputType(typeof(CimSessionOptions))]43 public sealed class NewCimSessionOptionCommand : CimBaseCommand44 {45 #region constructor46 47 /// <summary>48 /// Initializes a new instance of the <see cref="NewCimSessionOptionCommand"/> class.49 /// </summary>50 public NewCimSessionOptionCommand()51 : base(parameters, parameterSets)52 {53 DebugHelper.WriteLogEx();54 }55 56 #endregion57 58 #region cmdlet parameters59 60 /// <summary>61 /// The following is the definition of the input parameter "NoEncryption".62 /// Switch indicating if WSMan can use no encryption in the given CimSession (there are also global client and server WSMan settings - AllowUnencrypted).63 /// </summary>64 [Parameter(ParameterSetName = WSManParameterSet)]65 public SwitchParameter NoEncryption66 {67 get68 {69 return noEncryption;70 }71 72 set73 {74 noEncryption = value;75 noEncryptionSet = true;76 base.SetParameter(value, nameNoEncryption);77 }78 }79 80 private SwitchParameter noEncryption;81 private bool noEncryptionSet = false;82 83 /// <summary>84 /// The following is the definition of the input parameter "CertificateCACheck".85 /// Switch indicating if Certificate Authority should be validated.86 /// </summary>87 [Parameter(ValueFromPipelineByPropertyName = true,88 ParameterSetName = WSManParameterSet)]89 public SwitchParameter SkipCACheck90 {91 get92 {93 return skipCACheck;94 }95 96 set97 {98 skipCACheck = value;99 skipCACheckSet = true;100 base.SetParameter(value, nameSkipCACheck);101 }102 }103 104 private SwitchParameter skipCACheck;105 private bool skipCACheckSet = false;106 107 /// <summary>108 /// The following is the definition of the input parameter "CertificateCNCheck".109 /// Switch indicating if Certificate Name should be validated.110 /// </summary>111 [Parameter(ValueFromPipelineByPropertyName = true,112 ParameterSetName = WSManParameterSet)]113 public SwitchParameter SkipCNCheck114 {115 get116 {117 return skipCNCheck;118 }119 120 set121 {122 skipCNCheck = value;123 skipCNCheckSet = true;124 base.SetParameter(value, nameSkipCNCheck);125 }126 }127 128 private SwitchParameter skipCNCheck;129 private bool skipCNCheckSet = false;130 131 /// <summary>132 /// The following is the definition of the input parameter "CertRevocationCheck".133 /// Switch indicating if certificate should be revoked.134 /// </summary>135 [Parameter(ValueFromPipelineByPropertyName = true,136 ParameterSetName = WSManParameterSet)]137 public SwitchParameter SkipRevocationCheck138 {139 get140 {141 return skipRevocationCheck;142 }143 144 set145 {146 skipRevocationCheck = value;147 skipRevocationCheckSet = true;148 base.SetParameter(value, nameSkipRevocationCheck);149 }150 }151 152 private SwitchParameter skipRevocationCheck;153 private bool skipRevocationCheckSet = false;154 155 /// <summary>156 /// The following is the definition of the input parameter "EncodePortInServicePrincipalName".157 /// Switch indicating if to encode Port In Service Principal Name.158 /// </summary>159 [Parameter(ValueFromPipelineByPropertyName = true,160 ParameterSetName = WSManParameterSet)]161 public SwitchParameter EncodePortInServicePrincipalName162 {163 get164 {165 return encodeportinserviceprincipalname;166 }167 168 set169 {170 encodeportinserviceprincipalname = value;171 encodeportinserviceprincipalnameSet = true;172 base.SetParameter(value, nameEncodePortInServicePrincipalName);173 }174 }175 176 private SwitchParameter encodeportinserviceprincipalname;177 private bool encodeportinserviceprincipalnameSet = false;178 179 /// <summary>180 /// The following is the definition of the input parameter "Encoding".181 /// Defined the message encoding.182 /// The allowed encodings are { Default | Utf8 | Utf16 }. The default value183 /// should be Utf8.184 /// </summary>185 [Parameter(ValueFromPipelineByPropertyName = true,186 ParameterSetName = WSManParameterSet)]187 public PacketEncoding Encoding188 {189 get190 {191 return encoding;192 }193 194 set195 {196 encoding = value;197 encodingSet = true;198 base.SetParameter(value, nameEncoding);199 }200 }201 202 private PacketEncoding encoding;203 private bool encodingSet = false;204 205 /// <summary>206 /// The following is the definition of the input parameter "HttpPrefix".207 /// This is the HTTP URL on the server on which the WSMan service is listening.208 /// In most cases it is /wsman, which is the default.209 /// </summary>210 [Parameter(ValueFromPipelineByPropertyName = true,211 ParameterSetName = WSManParameterSet)]212 public Uri HttpPrefix213 {214 get215 {216 return httpprefix;217 }218 219 set220 {221 httpprefix = value;222 base.SetParameter(value, nameHttpPrefix);223 }224 }225 226 private Uri httpprefix;227 228 /// <summary>229 /// The following is the definition of the input parameter "MaxEnvelopeSizeKB".230 /// Sets the limit to the maximum size of the WSMan message envelope.231 /// </summary>232 [Parameter(ValueFromPipelineByPropertyName = true,233 ParameterSetName = WSManParameterSet)]234 public uint MaxEnvelopeSizeKB235 {236 get237 {238 return maxenvelopesizekb;239 }240 241 set242 {243 maxenvelopesizekb = value;244 maxenvelopesizekbSet = true;245 base.SetParameter(value, nameMaxEnvelopeSizeKB);246 }247 }248 249 private uint maxenvelopesizekb;250 private bool maxenvelopesizekbSet = false;251 252 /// <summary>253 /// The following is the definition of the input parameter "ProxyAuthentication".254 /// Which proxy authentication types to use: Allowed set is:255 /// </summary>256 [Parameter(ValueFromPipelineByPropertyName = true,257 ParameterSetName = WSManParameterSet)]258 public PasswordAuthenticationMechanism ProxyAuthentication259 {260 get261 {262 return proxyAuthentication;263 }264 265 set266 {267 proxyAuthentication = value;268 proxyauthenticationSet = true;269 base.SetParameter(value, nameProxyAuthentication);270 }271 }272 273 private PasswordAuthenticationMechanism proxyAuthentication;274 private bool proxyauthenticationSet = false;275 276 /// <summary>277 /// The following is the definition of the input parameter "ProxyCertificateThumbprint".278 /// </summary>279 [Parameter(ValueFromPipelineByPropertyName = true,280 ParameterSetName = WSManParameterSet)]281 public string ProxyCertificateThumbprint282 {283 get284 {285 return proxycertificatethumbprint;286 }287 288 set289 {290 proxycertificatethumbprint = value;291 base.SetParameter(value, nameProxyCertificateThumbprint);292 }293 }294 295 private string proxycertificatethumbprint;296 297 /// <summary>298 /// The following is the definition of the input parameter "ProxyCredential".299 /// Ps Credential used by the proxy server when required by the server.300 /// </summary>301 [Parameter(ParameterSetName = WSManParameterSet)]302 [Credential]303 public PSCredential ProxyCredential304 {305 get306 {307 return proxycredential;308 }309 310 set311 {312 proxycredential = value;313 base.SetParameter(value, nameProxyCredential);314 }315 }316 317 private PSCredential proxycredential;318 319 /// <summary>320 /// The following is the definition of the input parameter "ProxyType".321 /// Which proxy type to use: Valid set is:322 /// { InternetExplorer | WinHttp | Auto | None }323 /// </summary>324 [Parameter(ValueFromPipelineByPropertyName = true,325 ParameterSetName = WSManParameterSet)]326 public ProxyType ProxyType327 {328 get329 {330 return proxytype;331 }332 333 set334 {335 proxytype = value;336 proxytypeSet = true;337 base.SetParameter(value, nameProxyType);338 }339 }340 341 private ProxyType proxytype;342 private bool proxytypeSet = false;343 344 /// <summary>345 /// The following is the definition of the input parameter "UseSSL".346 /// Switch indicating if Secure Sockets Layer connection should be used.347 /// </summary>348 [Parameter(ValueFromPipelineByPropertyName = true,349 ParameterSetName = WSManParameterSet)]350 public SwitchParameter UseSsl351 {352 get353 {354 return usessl;355 }356 357 set358 {359 usessl = value;360 usesslSet = true;361 base.SetParameter(value, nameUseSsl);362 }363 }364 365 private SwitchParameter usessl;366 private bool usesslSet = false;367 368 /// <summary>369 /// The following is the definition of the input parameter "Impersonation".370 /// Used to select if, and if so what kind of, impersonation should be used.371 /// Applies only to the DCOM channel.372 /// </summary>373 [Parameter(ParameterSetName = DcomParameterSet)]374 public ImpersonationType Impersonation375 {376 get377 {378 return impersonation;379 }380 381 set382 {383 impersonation = value;384 impersonationSet = true;385 base.SetParameter(value, nameImpersonation);386 }387 }388 389 private ImpersonationType impersonation;390 private bool impersonationSet = false;391 392 /// <summary>393 /// The following is the definition of the input parameter "PacketIntegrity".394 /// Switch indicating if the package integrity in DCOM connections should be395 /// checked/enforced.396 /// </summary>397 [Parameter(ParameterSetName = DcomParameterSet)]398 public SwitchParameter PacketIntegrity399 {400 get401 {402 return packetintegrity;403 }404 405 set406 {407 packetintegrity = value;408 packetintegritySet = true;409 base.SetParameter(value, namePacketIntegrity);410 }411 }412 413 private SwitchParameter packetintegrity;414 private bool packetintegritySet = false;415 416 /// <summary>417 /// The following is the definition of the input parameter "PacketPrivacy".418 /// Switch indicating if packet privacy of the packets in DCOM communications419 /// should be checked/enforced.420 /// </summary>421 [Parameter(ParameterSetName = DcomParameterSet)]422 public SwitchParameter PacketPrivacy423 {424 get425 {426 return packetprivacy;427 }428 429 set430 {431 packetprivacy = value;432 packetprivacySet = true;433 base.SetParameter(value, namePacketPrivacy);434 }435 }436 437 private SwitchParameter packetprivacy;438 private bool packetprivacySet = false;439 440 /// <summary>441 /// The following is the definition of the input parameter "Protocol".442 /// Switch indicating if to encode Port In Service Principal Name.443 /// </summary>444 [Parameter(445 Mandatory = true,446 Position = 0,447 ValueFromPipelineByPropertyName = true,448 ParameterSetName = ProtocolNameParameterSet)]449 public ProtocolType Protocol450 {451 get452 {453 return protocol;454 }455 456 set457 {458 protocol = value;459 base.SetParameter(value, nameProtocol);460 }461 }462 463 private ProtocolType protocol;464 465 /// <summary>466 /// The following is the definition of the input parameter "UICulture".467 /// Specifies the UI Culture to use. i.e. en-us, ar-sa.468 /// </summary>469 [Parameter(ValueFromPipelineByPropertyName = true)]470 public CultureInfo UICulture { get; set; }471 472 /// <summary>473 /// The following is the definition of the input parameter "Culture".474 /// Specifies the culture to use. i.e. en-us, ar-sa.475 /// </summary>476 [Parameter(ValueFromPipelineByPropertyName = true)]477 public CultureInfo Culture { get; set; }478 479 #endregion480 481 #region cmdlet processing methods482 483 /// <summary>484 /// BeginProcessing method.485 /// </summary>486 protected override void BeginProcessing()487 {488 this.CmdletOperation = new CmdletOperationBase(this);489 this.AtBeginProcess = false;490 }491 492 /// <summary>493 /// ProcessRecord method.494 /// </summary>495 protected override void ProcessRecord()496 {497 base.CheckParameterSet();498 CimSessionOptions options;499 switch (this.ParameterSetName)500 {501 case WSManParameterSet:502 {503 options = CreateWSMANSessionOptions();504 }505 506 break;507 case DcomParameterSet:508 {509 options = CreateDComSessionOptions();510 }511 512 break;513 case ProtocolNameParameterSet:514 switch (Protocol)515 {516 case ProtocolType.Dcom:517 options = CreateDComSessionOptions();518 break;519 case ProtocolType.Wsman:520 default:521 options = CreateWSMANSessionOptions();522 break;523 }524 525 break;526 default:527 return;528 }529 530 if (options != null)531 {532 if (this.Culture != null)533 {534 options.Culture = this.Culture;535 }536 537 if (this.UICulture != null)538 {539 options.UICulture = this.UICulture;540 }541 542 this.WriteObject(options);543 }544 }545 546 /// <summary>547 /// EndProcessing method.548 /// </summary>549 protected override void EndProcessing()550 {551 }552 553 #endregion554 555 #region helper functions556 /// <summary>557 /// Create DComSessionOptions.558 /// </summary>559 /// <returns></returns>560 internal DComSessionOptions CreateDComSessionOptions()561 {562 DComSessionOptions dcomoptions = new();563 if (this.impersonationSet)564 {565 dcomoptions.Impersonation = this.Impersonation;566 this.impersonationSet = false;567 }568 else569 {570 dcomoptions.Impersonation = ImpersonationType.Impersonate;571 }572 573 if (this.packetintegritySet)574 {575 dcomoptions.PacketIntegrity = this.packetintegrity;576 this.packetintegritySet = false;577 }578 else579 {580 dcomoptions.PacketIntegrity = true;581 }582 583 if (this.packetprivacySet)584 {585 dcomoptions.PacketPrivacy = this.PacketPrivacy;586 this.packetprivacySet = false;587 }588 else589 {590 dcomoptions.PacketPrivacy = true;591 }592 593 return dcomoptions;594 }595 596 /// <summary>597 /// Create WSMANSessionOptions.598 /// </summary>599 /// <returns></returns>600 internal WSManSessionOptions CreateWSMANSessionOptions()601 {602 WSManSessionOptions wsmanoptions = new();603 if (this.noEncryptionSet)604 {605 wsmanoptions.NoEncryption = true;606 this.noEncryptionSet = false;607 }608 else609 {610 wsmanoptions.NoEncryption = false;611 }612 613 if (this.skipCACheckSet)614 {615 wsmanoptions.CertCACheck = false;616 this.skipCACheckSet = false;617 }618 else619 {620 wsmanoptions.CertCACheck = true;621 }622 623 if (this.skipCNCheckSet)624 {625 wsmanoptions.CertCNCheck = false;626 this.skipCNCheckSet = false;627 }628 else629 {630 wsmanoptions.CertCNCheck = true;631 }632 633 if (this.skipRevocationCheckSet)634 {635 wsmanoptions.CertRevocationCheck = false;636 this.skipRevocationCheckSet = false;637 }638 else639 {640 wsmanoptions.CertRevocationCheck = true;641 }642 643 if (this.encodeportinserviceprincipalnameSet)644 {645 wsmanoptions.EncodePortInServicePrincipalName = this.EncodePortInServicePrincipalName;646 this.encodeportinserviceprincipalnameSet = false;647 }648 else649 {650 wsmanoptions.EncodePortInServicePrincipalName = false;651 }652 653 if (this.encodingSet)654 {655 wsmanoptions.PacketEncoding = this.Encoding;656 }657 else658 {659 wsmanoptions.PacketEncoding = PacketEncoding.Utf8;660 }661 662 if (this.HttpPrefix != null)663 {664 wsmanoptions.HttpUrlPrefix = this.HttpPrefix;665 }666 667 if (this.maxenvelopesizekbSet)668 {669 wsmanoptions.MaxEnvelopeSize = this.MaxEnvelopeSizeKB;670 }671 else672 {673 wsmanoptions.MaxEnvelopeSize = 0;674 }675 676 if (!string.IsNullOrWhiteSpace(this.ProxyCertificateThumbprint))677 {678 CimCredential credentials = new(CertificateAuthenticationMechanism.Default, this.ProxyCertificateThumbprint);679 wsmanoptions.AddProxyCredentials(credentials);680 }681 682 if (this.proxyauthenticationSet)683 {684 this.proxyauthenticationSet = false;685 DebugHelper.WriteLogEx("create credential", 1);686 CimCredential credentials = CreateCimCredentials(this.ProxyCredential, this.ProxyAuthentication, @"New-CimSessionOption", @"ProxyAuthentication");687 if (credentials != null)688 {689 try690 {691 DebugHelper.WriteLogEx("Add proxy credential", 1);692 wsmanoptions.AddProxyCredentials(credentials);693 }694 catch (Exception ex)695 {696 DebugHelper.WriteLogEx(ex.ToString(), 1);697 throw;698 }699 }700 }701 702 if (this.proxytypeSet)703 {704 wsmanoptions.ProxyType = this.ProxyType;705 this.proxytypeSet = false;706 }707 else708 {709 wsmanoptions.ProxyType = Options.ProxyType.WinHttp;710 }711 712 if (this.usesslSet)713 {714 wsmanoptions.UseSsl = this.UseSsl;715 this.usesslSet = false;716 }717 else718 {719 wsmanoptions.UseSsl = false;720 }721 722 wsmanoptions.DestinationPort = 0;723 return wsmanoptions;724 }725 #endregion726 727 #region private members728 729 #region const string of parameter names730 internal const string nameNoEncryption = "NoEncryption";731 internal const string nameSkipCACheck = "SkipCACheck";732 internal const string nameSkipCNCheck = "SkipCNCheck";733 internal const string nameSkipRevocationCheck = "SkipRevocationCheck";734 internal const string nameEncodePortInServicePrincipalName = "EncodePortInServicePrincipalName";735 internal const string nameEncoding = "Encoding";736 internal const string nameHttpPrefix = "HttpPrefix";737 internal const string nameMaxEnvelopeSizeKB = "MaxEnvelopeSizeKB";738 internal const string nameProxyAuthentication = "ProxyAuthentication";739 internal const string nameProxyCertificateThumbprint = "ProxyCertificateThumbprint";740 internal const string nameProxyCredential = "ProxyCredential";741 internal const string nameProxyType = "ProxyType";742 internal const string nameUseSsl = "UseSsl";743 internal const string nameImpersonation = "Impersonation";744 internal const string namePacketIntegrity = "PacketIntegrity";745 internal const string namePacketPrivacy = "PacketPrivacy";746 internal const string nameProtocol = "Protocol";747 #endregion748 749 /// <summary>750 /// Static parameter definition entries.751 /// </summary>752 private static readonly Dictionary<string, HashSet<ParameterDefinitionEntry>> parameters = new()753 {754 {755 nameNoEncryption, new HashSet<ParameterDefinitionEntry> {756 new ParameterDefinitionEntry(CimBaseCommand.WSManParameterSet, false),757 }758 },759 760 {761 nameSkipCACheck, new HashSet<ParameterDefinitionEntry> {762 new ParameterDefinitionEntry(CimBaseCommand.WSManParameterSet, false),763 }764 },765 766 {767 nameSkipCNCheck, new HashSet<ParameterDefinitionEntry> {768 new ParameterDefinitionEntry(CimBaseCommand.WSManParameterSet, false),769 }770 },771 {772 nameSkipRevocationCheck, new HashSet<ParameterDefinitionEntry> {773 new ParameterDefinitionEntry(CimBaseCommand.WSManParameterSet, false),774 }775 },776 777 {778 nameEncodePortInServicePrincipalName, new HashSet<ParameterDefinitionEntry> {779 new ParameterDefinitionEntry(CimBaseCommand.WSManParameterSet, false),780 }781 },782 {783 nameEncoding, new HashSet<ParameterDefinitionEntry> {784 new ParameterDefinitionEntry(CimBaseCommand.WSManParameterSet, false),785 }786 },787 788 {789 nameHttpPrefix, new HashSet<ParameterDefinitionEntry> {790 new ParameterDefinitionEntry(CimBaseCommand.WSManParameterSet, false),791 }792 },793 {794 nameMaxEnvelopeSizeKB, new HashSet<ParameterDefinitionEntry> {795 new ParameterDefinitionEntry(CimBaseCommand.WSManParameterSet, false),796 }797 },798 799 {800 nameProxyAuthentication, new HashSet<ParameterDefinitionEntry> {801 new ParameterDefinitionEntry(CimBaseCommand.WSManParameterSet, false),802 }803 },804 {805 nameProxyCertificateThumbprint, new HashSet<ParameterDefinitionEntry> {806 new ParameterDefinitionEntry(CimBaseCommand.WSManParameterSet, false),807 }808 },809 810 {811 nameProxyCredential, new HashSet<ParameterDefinitionEntry> {812 new ParameterDefinitionEntry(CimBaseCommand.WSManParameterSet, false),813 }814 },815 {816 nameProxyType, new HashSet<ParameterDefinitionEntry> {817 new ParameterDefinitionEntry(CimBaseCommand.WSManParameterSet, false),818 }819 },820 821 {822 nameUseSsl, new HashSet<ParameterDefinitionEntry> {823 new ParameterDefinitionEntry(CimBaseCommand.WSManParameterSet, false),824 }825 },826 {827 nameImpersonation, new HashSet<ParameterDefinitionEntry> {828 new ParameterDefinitionEntry(CimBaseCommand.DcomParameterSet, false),829 }830 },831 832 {833 namePacketIntegrity, new HashSet<ParameterDefinitionEntry> {834 new ParameterDefinitionEntry(CimBaseCommand.DcomParameterSet, false),835 }836 },837 {838 namePacketPrivacy, new HashSet<ParameterDefinitionEntry> {839 new ParameterDefinitionEntry(CimBaseCommand.DcomParameterSet, false),840 }841 },842 843 {844 nameProtocol, new HashSet<ParameterDefinitionEntry> {845 new ParameterDefinitionEntry(CimBaseCommand.ProtocolNameParameterSet, true),846 }847 },848 };849 850 /// <summary>851 /// Static parameter set entries.852 /// </summary>853 private static readonly Dictionary<string, ParameterSetEntry> parameterSets = new()854 {855 { CimBaseCommand.ProtocolNameParameterSet, new ParameterSetEntry(1, true) },856 { CimBaseCommand.DcomParameterSet, new ParameterSetEntry(0) },857 { CimBaseCommand.WSManParameterSet, new ParameterSetEntry(0) },858 };859 #endregion860 }861}862 