2020-03-24 19:08:37 +01:00
|
|
|
// Copyright (c) Microsoft Corporation.
|
2018-02-13 18:23:53 +01:00
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2019-04-30 07:25:11 +02:00
|
|
|
using System.ComponentModel;
|
2016-07-14 04:27:37 +02:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2019-04-30 07:25:11 +02:00
|
|
|
using System.IO;
|
|
|
|
using System.Management.Automation;
|
|
|
|
using System.Management.Automation.Provider;
|
2016-07-14 04:27:37 +02:00
|
|
|
using System.Net;
|
2019-04-30 07:25:11 +02:00
|
|
|
using System.Reflection;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Xml;
|
2016-07-14 04:27:37 +02:00
|
|
|
|
|
|
|
namespace Microsoft.WSMan.Management
|
|
|
|
{
|
|
|
|
/// <summary>
|
2017-01-16 22:31:14 +01:00
|
|
|
/// Creates a WSMan Session option hashtable which can be passed into WSMan
|
2016-07-14 04:27:37 +02:00
|
|
|
/// cmdlets:
|
|
|
|
/// Get-WSManInstance
|
|
|
|
/// Set-WSManInstance
|
|
|
|
/// Invoke-WSManAction
|
2019-01-06 00:40:24 +01:00
|
|
|
/// Connect-WSMan.
|
2016-07-14 04:27:37 +02:00
|
|
|
/// </summary>
|
2019-12-02 19:25:56 +01:00
|
|
|
[Cmdlet(VerbsCommon.New, "WSManSessionOption", HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2096845")]
|
2016-07-14 04:27:37 +02:00
|
|
|
public class NewWSManSessionOptionCommand : PSCmdlet
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
|
|
|
[ValidateNotNullOrEmpty]
|
|
|
|
public ProxyAccessType ProxyAccessType
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2016-08-30 05:53:12 +02:00
|
|
|
return _proxyaccesstype;
|
2016-07-14 04:27:37 +02:00
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set
|
|
|
|
{
|
2016-08-30 05:53:12 +02:00
|
|
|
_proxyaccesstype = value;
|
2016-07-14 04:27:37 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-08-30 05:53:12 +02:00
|
|
|
private ProxyAccessType _proxyaccesstype;
|
2016-07-14 04:27:37 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "ProxyAuthentication".
|
2017-01-16 22:31:14 +01:00
|
|
|
/// This parameter takes a set of authentication methods the user can select
|
2016-07-14 04:27:37 +02:00
|
|
|
/// from. The available options should be as follows:
|
2017-01-16 22:31:14 +01:00
|
|
|
/// - Negotiate: Use the default authentication (ad defined by the underlying
|
2016-07-14 04:27:37 +02:00
|
|
|
/// protocol) for establishing a remote connection.
|
|
|
|
/// - Basic: Use basic authentication for establishing a remote connection
|
2019-01-06 00:40:24 +01:00
|
|
|
/// - Digest: Use Digest authentication for establishing a remote connection.
|
2016-07-14 04:27:37 +02:00
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
|
|
|
[ValidateNotNullOrEmpty]
|
|
|
|
public ProxyAuthentication ProxyAuthentication
|
|
|
|
{
|
2021-01-09 08:44:38 +01:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return proxyauthentication;
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set
|
|
|
|
{
|
|
|
|
proxyauthentication = value;
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
private ProxyAuthentication proxyauthentication;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "ProxyCredential".
|
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
|
|
|
[ValidateNotNullOrEmpty]
|
|
|
|
[Credential]
|
|
|
|
public PSCredential ProxyCredential
|
|
|
|
{
|
2021-01-09 08:44:38 +01:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return _proxycredential;
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set
|
|
|
|
{
|
|
|
|
_proxycredential = value;
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
private PSCredential _proxycredential;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "SkipCACheck".
|
2017-01-16 22:31:14 +01:00
|
|
|
/// When connecting over HTTPS, the client does not validate that the server
|
|
|
|
/// certificate is signed by a trusted certificate authority (CA). Use only when
|
|
|
|
/// the remote computer is trusted by other means, for example, if the remote
|
|
|
|
/// computer is part of a network that is physically secure and isolated or the
|
2019-01-06 00:40:24 +01:00
|
|
|
/// remote computer is listed as a trusted host in WinRM configuration.
|
2016-07-14 04:27:37 +02:00
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
|
|
|
public SwitchParameter SkipCACheck
|
|
|
|
{
|
2021-01-09 08:44:38 +01:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return skipcacheck;
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set
|
|
|
|
{
|
|
|
|
skipcacheck = value;
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
private bool skipcacheck;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "SkipCNCheck".
|
2017-01-16 22:31:14 +01:00
|
|
|
/// Indicates that certificate common name (CN) of the server need not match the
|
|
|
|
/// hostname of the server. Used only in remote operations using https. This
|
2019-01-06 00:40:24 +01:00
|
|
|
/// option should only be used for trusted machines.
|
2016-07-14 04:27:37 +02:00
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
|
|
|
public SwitchParameter SkipCNCheck
|
|
|
|
{
|
2021-01-09 08:44:38 +01:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return skipcncheck;
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set
|
|
|
|
{
|
|
|
|
skipcncheck = value;
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
private bool skipcncheck;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "SkipRevocation".
|
2017-01-16 22:31:14 +01:00
|
|
|
/// Indicates that certificate common name (CN) of the server need not match the
|
|
|
|
/// hostname of the server. Used only in remote operations using https. This
|
2019-01-06 00:40:24 +01:00
|
|
|
/// option should only be used for trusted machines.
|
2016-07-14 04:27:37 +02:00
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
|
|
|
public SwitchParameter SkipRevocationCheck
|
|
|
|
{
|
2021-01-09 08:44:38 +01:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return skiprevocationcheck;
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set
|
|
|
|
{
|
|
|
|
skiprevocationcheck = value;
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
private bool skiprevocationcheck;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "SPNPort".
|
2017-01-16 22:31:14 +01:00
|
|
|
/// Appends port number to the connection Service Principal Name SPN of the
|
2016-07-14 04:27:37 +02:00
|
|
|
/// remote server.
|
2019-01-06 00:40:24 +01:00
|
|
|
/// SPN is used when authentication mechanism is Kerberos or Negotiate.
|
2016-07-14 04:27:37 +02:00
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
|
|
|
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "SPN")]
|
2021-07-28 21:39:14 +02:00
|
|
|
[ValidateRange(0, int.MaxValue)]
|
|
|
|
public int SPNPort
|
2016-07-14 04:27:37 +02:00
|
|
|
{
|
2021-01-09 08:44:38 +01:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return spnport;
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set
|
|
|
|
{
|
|
|
|
spnport = value;
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2021-07-28 21:39:14 +02:00
|
|
|
private int spnport;
|
2016-07-14 04:27:37 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "Timeout".
|
2019-01-06 00:40:24 +01:00
|
|
|
/// Defines the timeout in ms for the wsman operation.
|
2016-07-14 04:27:37 +02:00
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
|
|
|
[Alias("OperationTimeoutMSec")]
|
2021-07-28 21:39:14 +02:00
|
|
|
[ValidateRange(0, int.MaxValue)]
|
|
|
|
public int OperationTimeout
|
2016-07-14 04:27:37 +02:00
|
|
|
{
|
2021-01-09 08:44:38 +01:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return operationtimeout;
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set
|
|
|
|
{
|
|
|
|
operationtimeout = value;
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2021-07-28 21:39:14 +02:00
|
|
|
private int operationtimeout;
|
2016-07-14 04:27:37 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "UnEncrypted".
|
2017-01-16 22:31:14 +01:00
|
|
|
/// Specifies that no encryption will be used when doing remote operations over
|
|
|
|
/// http. Unencrypted traffic is not allowed by default and must be enabled in
|
2019-01-06 00:40:24 +01:00
|
|
|
/// the local configuration.
|
2016-07-14 04:27:37 +02:00
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
|
|
|
public SwitchParameter NoEncryption
|
|
|
|
{
|
2021-01-09 08:44:38 +01:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return noencryption;
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set
|
|
|
|
{
|
|
|
|
noencryption = value;
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
private bool noencryption;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "UTF16".
|
2017-01-16 22:31:14 +01:00
|
|
|
/// Indicates the request is encoded in UTF16 format rather than UTF8 format;
|
2016-07-14 04:27:37 +02:00
|
|
|
/// UTF8 is the default.
|
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
|
|
|
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "UTF")]
|
|
|
|
public SwitchParameter UseUTF16
|
|
|
|
{
|
2021-01-09 08:44:38 +01:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return useutf16;
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set
|
|
|
|
{
|
|
|
|
useutf16 = value;
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
private bool useutf16;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// BeginProcessing method.
|
|
|
|
/// </summary>
|
|
|
|
protected override void BeginProcessing()
|
|
|
|
{
|
|
|
|
WSManHelper helper = new WSManHelper(this);
|
|
|
|
|
|
|
|
if (proxyauthentication.Equals(ProxyAuthentication.Basic) || proxyauthentication.Equals(ProxyAuthentication.Digest))
|
|
|
|
{
|
2020-07-31 01:06:38 +02:00
|
|
|
if (_proxycredential == null)
|
2016-07-14 04:27:37 +02:00
|
|
|
{
|
|
|
|
InvalidOperationException ex = new InvalidOperationException(helper.GetResourceMsgFromResourcetext("NewWSManSessionOptionCred"));
|
|
|
|
ErrorRecord er = new ErrorRecord(ex, "InvalidOperationException", ErrorCategory.InvalidOperation, null);
|
|
|
|
WriteError(er);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((_proxycredential != null) && (proxyauthentication == 0))
|
|
|
|
{
|
|
|
|
InvalidOperationException ex = new InvalidOperationException(helper.GetResourceMsgFromResourcetext("NewWSManSessionOptionAuth"));
|
|
|
|
ErrorRecord er = new ErrorRecord(ex, "InvalidOperationException", ErrorCategory.InvalidOperation, null);
|
|
|
|
WriteError(er);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-06 02:51:20 +01:00
|
|
|
// Creating the Session Object
|
2016-07-14 04:27:37 +02:00
|
|
|
SessionOption objSessionOption = new SessionOption();
|
|
|
|
|
|
|
|
objSessionOption.SPNPort = spnport;
|
|
|
|
objSessionOption.UseUtf16 = useutf16;
|
|
|
|
objSessionOption.SkipCNCheck = skipcncheck;
|
|
|
|
objSessionOption.SkipCACheck = skipcacheck;
|
|
|
|
objSessionOption.OperationTimeout = operationtimeout;
|
|
|
|
objSessionOption.SkipRevocationCheck = skiprevocationcheck;
|
2019-01-06 02:51:20 +01:00
|
|
|
// Proxy Settings
|
2016-08-30 05:53:12 +02:00
|
|
|
objSessionOption.ProxyAccessType = _proxyaccesstype;
|
2016-07-14 04:27:37 +02:00
|
|
|
objSessionOption.ProxyAuthentication = proxyauthentication;
|
|
|
|
|
|
|
|
if (noencryption)
|
|
|
|
{
|
|
|
|
objSessionOption.UseEncryption = false;
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
if (_proxycredential != null)
|
|
|
|
{
|
|
|
|
NetworkCredential nwCredentials = _proxycredential.GetNetworkCredential();
|
|
|
|
objSessionOption.ProxyCredential = nwCredentials;
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
WriteObject(objSessionOption);
|
2019-01-06 02:51:20 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-14 04:27:37 +02:00
|
|
|
}
|