Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
NewWSManSession.cs310 linesDownload Raw Back to Microsoft.WSMan.Management
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Collections;6using System.Collections.Generic;7using System.ComponentModel;8using System.Diagnostics.CodeAnalysis;9using System.IO;10using System.Management.Automation;11using System.Management.Automation.Provider;12using System.Net;13using System.Reflection;14using System.Runtime.CompilerServices;15using System.Runtime.InteropServices;16using System.Xml;17 18namespace Microsoft.WSMan.Management19{20    /// <summary>21    /// Creates a WSMan Session option hashtable which can be passed into WSMan22    /// cmdlets:23    /// Get-WSManInstance24    /// Set-WSManInstance25    /// Invoke-WSManAction26    /// Connect-WSMan.27    /// </summary>28    [Cmdlet(VerbsCommon.New, "WSManSessionOption", HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2096845")]29    [OutputType(typeof(SessionOption))]30    public class NewWSManSessionOptionCommand : PSCmdlet31    {32        /// <summary>33        /// </summary>34        [Parameter]35        [ValidateNotNullOrEmpty]36        public ProxyAccessType ProxyAccessType37        {38            get39            {40                return _proxyaccesstype;41            }42 43            set44            {45                _proxyaccesstype = value;46            }47        }48 49        private ProxyAccessType _proxyaccesstype;50 51        /// <summary>52        /// The following is the definition of the input parameter "ProxyAuthentication".53        /// This parameter takes a set of authentication methods the user can select54        /// from.  The available options should be as follows:55        /// - Negotiate: Use the default authentication (ad defined by the underlying56        /// protocol) for establishing a remote connection.57        /// - Basic:  Use basic authentication for establishing a remote connection58        /// - Digest: Use Digest authentication for establishing a remote connection.59        /// </summary>60        [Parameter]61        [ValidateNotNullOrEmpty]62        public ProxyAuthentication ProxyAuthentication63        {64            get65            {66                return proxyauthentication;67            }68 69            set70            {71                proxyauthentication = value;72            }73        }74 75        private ProxyAuthentication proxyauthentication;76 77        /// <summary>78        /// The following is the definition of the input parameter "ProxyCredential".79        /// </summary>80        [Parameter]81        [ValidateNotNullOrEmpty]82        [Credential]83        public PSCredential ProxyCredential84        {85            get86            {87                return _proxycredential;88            }89 90            set91            {92                _proxycredential = value;93            }94        }95 96        private PSCredential _proxycredential;97 98        /// <summary>99        /// The following is the definition of the input parameter "SkipCACheck".100        /// When connecting over HTTPS, the client does not validate that the server101        /// certificate is signed by a trusted certificate authority (CA). Use only when102        /// the remote computer is trusted by other means, for example, if the remote103        /// computer is part of a network that is physically secure and isolated or the104        /// remote computer is listed as a trusted host in WinRM configuration.105        /// </summary>106        [Parameter]107        public SwitchParameter SkipCACheck108        {109            get110            {111                return skipcacheck;112            }113 114            set115            {116                skipcacheck = value;117            }118        }119 120        private bool skipcacheck;121 122        /// <summary>123        /// The following is the definition of the input parameter "SkipCNCheck".124        /// Indicates that certificate common name (CN) of the server need not match the125        /// hostname of the server. Used only in remote operations using https. This126        /// option should only be used for trusted machines.127        /// </summary>128        [Parameter]129        public SwitchParameter SkipCNCheck130        {131            get132            {133                return skipcncheck;134            }135 136            set137            {138                skipcncheck = value;139            }140        }141 142        private bool skipcncheck;143 144        /// <summary>145        /// The following is the definition of the input parameter "SkipRevocation".146        /// Indicates that certificate common name (CN) of the server need not match the147        /// hostname of the server. Used only in remote operations using https. This148        /// option should only be used for trusted machines.149        /// </summary>150        [Parameter]151        public SwitchParameter SkipRevocationCheck152        {153            get154            {155                return skiprevocationcheck;156            }157 158            set159            {160                skiprevocationcheck = value;161            }162        }163 164        private bool skiprevocationcheck;165 166        /// <summary>167        /// The following is the definition of the input parameter "SPNPort".168        /// Appends port number to the connection Service Principal Name SPN of the169        /// remote server.170        /// SPN is used when authentication mechanism is Kerberos or Negotiate.171        /// </summary>172        [Parameter]173        [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "SPN")]174        [ValidateRange(0, int.MaxValue)]175        public int SPNPort176        {177            get178            {179                return spnport;180            }181 182            set183            {184                spnport = value;185            }186        }187 188        private int spnport;189 190        /// <summary>191        /// The following is the definition of the input parameter "Timeout".192        /// Defines the timeout in ms for the wsman operation.193        /// </summary>194        [Parameter]195        [Alias("OperationTimeoutMSec")]196        [ValidateRange(0, int.MaxValue)]197        public int OperationTimeout198        {199            get200            {201                return operationtimeout;202            }203 204            set205            {206                operationtimeout = value;207            }208        }209 210        private int operationtimeout;211 212        /// <summary>213        /// The following is the definition of the input parameter "UnEncrypted".214        /// Specifies that no encryption will be used when doing remote operations over215        /// http. Unencrypted traffic is not allowed by default and must be enabled in216        /// the local configuration.217        /// </summary>218        [Parameter]219        public SwitchParameter NoEncryption220        {221            get222            {223                return noencryption;224            }225 226            set227            {228                noencryption = value;229            }230        }231 232        private bool noencryption;233 234        /// <summary>235        /// The following is the definition of the input parameter "UTF16".236        /// Indicates the request is encoded in UTF16 format rather than UTF8 format;237        /// UTF8 is the default.238        /// </summary>239        [Parameter]240        [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "UTF")]241        public SwitchParameter UseUTF16242        {243            get244            {245                return useutf16;246            }247 248            set249            {250                useutf16 = value;251            }252        }253 254        private bool useutf16;255 256        /// <summary>257        /// BeginProcessing method.258        /// </summary>259        protected override void BeginProcessing()260        {261            WSManHelper helper = new WSManHelper(this);262 263            if (proxyauthentication.Equals(ProxyAuthentication.Basic) || proxyauthentication.Equals(ProxyAuthentication.Digest))264            {265                if (_proxycredential == null)266                {267                    InvalidOperationException ex = new InvalidOperationException(helper.GetResourceMsgFromResourcetext("NewWSManSessionOptionCred"));268                    ErrorRecord er = new ErrorRecord(ex, "InvalidOperationException", ErrorCategory.InvalidOperation, null);269                    WriteError(er);270                    return;271                }272            }273 274            if ((_proxycredential != null) && (proxyauthentication == 0))275            {276                InvalidOperationException ex = new InvalidOperationException(helper.GetResourceMsgFromResourcetext("NewWSManSessionOptionAuth"));277                ErrorRecord er = new ErrorRecord(ex, "InvalidOperationException", ErrorCategory.InvalidOperation, null);278                WriteError(er);279                return;280            }281 282            // Creating the Session Object283            SessionOption objSessionOption = new SessionOption();284 285            objSessionOption.SPNPort = spnport;286            objSessionOption.UseUtf16 = useutf16;287            objSessionOption.SkipCNCheck = skipcncheck;288            objSessionOption.SkipCACheck = skipcacheck;289            objSessionOption.OperationTimeout = operationtimeout;290            objSessionOption.SkipRevocationCheck = skiprevocationcheck;291            // Proxy Settings292            objSessionOption.ProxyAccessType = _proxyaccesstype;293            objSessionOption.ProxyAuthentication = proxyauthentication;294 295            if (noencryption)296            {297                objSessionOption.UseEncryption = false;298            }299 300            if (_proxycredential != null)301            {302                NetworkCredential nwCredentials = _proxycredential.GetNetworkCredential();303                objSessionOption.ProxyCredential = nwCredentials;304            }305 306            WriteObject(objSessionOption);307        }308    }309}310