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;
|
2019-04-30 07:25:11 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2016-07-14 04:27:37 +02:00
|
|
|
using System.ComponentModel;
|
2019-04-30 07:25:11 +02:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
using System.IO;
|
2016-07-14 04:27:37 +02:00
|
|
|
using System.Management.Automation;
|
|
|
|
using System.Management.Automation.Provider;
|
2019-04-30 07:25:11 +02:00
|
|
|
using System.Reflection;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Runtime.InteropServices;
|
2016-07-14 04:27:37 +02:00
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
namespace Microsoft.WSMan.Management
|
|
|
|
{
|
|
|
|
#region Test-WSMAN
|
|
|
|
|
|
|
|
/// <summary>
|
2017-01-16 22:31:14 +01:00
|
|
|
/// Issues an operation against the remote machine to ensure that the wsman
|
2019-01-06 00:40:24 +01:00
|
|
|
/// service is running.
|
2016-07-14 04:27:37 +02:00
|
|
|
/// </summary>
|
|
|
|
|
2019-12-02 19:25:56 +01:00
|
|
|
[Cmdlet(VerbsDiagnostic.Test, "WSMan", HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2097114")]
|
2016-07-14 04:27:37 +02:00
|
|
|
public class TestWSManCommand : AuthenticatingWSManCommand, IDisposable
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "ComputerName".
|
2017-01-16 22:31:14 +01:00
|
|
|
/// Executes the management operation on the specified computer. The default is
|
|
|
|
/// the local computer. Type the fully qualified domain name, NETBIOS name or IP
|
2019-01-06 00:40:24 +01:00
|
|
|
/// address to indicate the remote host.
|
2016-07-14 04:27:37 +02:00
|
|
|
/// </summary>
|
|
|
|
[Parameter(Position = 0, ValueFromPipeline = true)]
|
|
|
|
[Alias("cn")]
|
2019-01-07 19:46:00 +01:00
|
|
|
public string ComputerName
|
2016-07-14 04:27:37 +02:00
|
|
|
{
|
|
|
|
get { return computername; }
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set
|
|
|
|
{
|
|
|
|
computername = value;
|
2018-10-21 13:11:08 +02:00
|
|
|
if ((string.IsNullOrEmpty(computername)) || (computername.Equals(".", StringComparison.OrdinalIgnoreCase)))
|
2016-07-14 04:27:37 +02:00
|
|
|
{
|
|
|
|
computername = "localhost";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2019-01-07 19:46:00 +01:00
|
|
|
private string computername = null;
|
2016-07-14 04:27:37 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "Authentication".
|
2017-01-16 22:31:14 +01:00
|
|
|
/// This parameter takes a set of authentication methods the user can select
|
|
|
|
/// from. The available method are an enum called AuthenticationMechanism in the
|
2017-09-05 04:45:51 +02:00
|
|
|
/// System.Management.Automation.Runspaces namespace. The available options
|
2016-07-14 04:27:37 +02:00
|
|
|
/// should be as follows:
|
2017-01-16 22:31:14 +01:00
|
|
|
/// - Default : Use the default authentication (ad defined by the underlying
|
2016-07-14 04:27:37 +02:00
|
|
|
/// protocol) for establishing a remote connection.
|
|
|
|
/// - Negotiate
|
|
|
|
/// - Kerberos
|
|
|
|
/// - Basic: Use basic authentication for establishing a remote connection.
|
2017-01-16 22:31:14 +01:00
|
|
|
/// -CredSSP: Use CredSSP authentication for establishing a remote connection
|
|
|
|
/// which will enable the user to perform credential delegation. (i.e. second
|
2016-07-14 04:27:37 +02:00
|
|
|
/// hop)
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Overriding to use a different default than the one in AuthenticatingWSManCommand base class
|
|
|
|
/// </remarks>
|
|
|
|
[Parameter]
|
|
|
|
[ValidateNotNullOrEmpty]
|
|
|
|
[Alias("auth", "am")]
|
|
|
|
public override AuthenticationMechanism Authentication
|
|
|
|
{
|
|
|
|
get { return authentication; }
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2017-01-16 22:31:14 +01:00
|
|
|
set
|
|
|
|
{
|
2016-07-14 04:27:37 +02:00
|
|
|
authentication = value;
|
|
|
|
ValidateSpecifiedAuthentication();
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
private AuthenticationMechanism authentication = AuthenticationMechanism.None;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "Port".
|
2017-01-16 22:31:14 +01:00
|
|
|
/// Specifies the port to be used when connecting to the ws management service.
|
2016-07-14 04:27:37 +02:00
|
|
|
/// </summary>
|
|
|
|
[Parameter(ParameterSetName = "ComputerName")]
|
|
|
|
[ValidateNotNullOrEmpty]
|
|
|
|
[ValidateRange(1, Int32.MaxValue)]
|
|
|
|
public Int32 Port
|
|
|
|
{
|
|
|
|
get { return port; }
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set { port = value; }
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
private Int32 port = 0;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "UseSSL".
|
2017-01-16 22:31:14 +01:00
|
|
|
/// Uses the Secure Sockets Layer (SSL) protocol to establish a connection to
|
|
|
|
/// the remote computer. If SSL is not available on the port specified by the
|
2016-07-14 04:27:37 +02:00
|
|
|
/// Port parameter, the command fails.
|
|
|
|
/// </summary>
|
|
|
|
[Parameter(ParameterSetName = "ComputerName")]
|
|
|
|
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "SSL")]
|
|
|
|
public SwitchParameter UseSSL
|
|
|
|
{
|
|
|
|
get { return usessl; }
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set { usessl = value; }
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
private SwitchParameter usessl;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The following is the definition of the input parameter "ApplicationName".
|
|
|
|
/// ApplicationName identifies the remote endpoint.
|
|
|
|
/// </summary>
|
|
|
|
[Parameter(ParameterSetName = "ComputerName")]
|
|
|
|
[ValidateNotNullOrEmpty]
|
2019-01-07 19:46:00 +01:00
|
|
|
public string ApplicationName
|
2016-07-14 04:27:37 +02:00
|
|
|
{
|
|
|
|
get { return applicationname; }
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-07-14 04:27:37 +02:00
|
|
|
set { applicationname = value; }
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2019-01-07 19:46:00 +01:00
|
|
|
private string applicationname = null;
|
2016-07-14 04:27:37 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// ProcessRecord method.
|
|
|
|
/// </summary>
|
|
|
|
protected override void ProcessRecord()
|
|
|
|
{
|
|
|
|
WSManHelper helper = new WSManHelper(this);
|
|
|
|
IWSManEx wsmanObject = (IWSManEx)new WSManClass();
|
2018-12-31 16:44:16 +01:00
|
|
|
string connectionStr = string.Empty;
|
2016-07-14 04:27:37 +02:00
|
|
|
connectionStr = helper.CreateConnectionString(null, port, computername, applicationname);
|
|
|
|
IWSManSession m_SessionObj = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_SessionObj = helper.CreateSessionObject(wsmanObject, Authentication, null, Credential, connectionStr, CertificateThumbprint, usessl.IsPresent);
|
2019-01-06 02:51:20 +01:00
|
|
|
m_SessionObj.Timeout = 1000; // 1 sec. we are putting this low so that Test-WSMan can return promptly if the server goes unresponsive.
|
2016-07-14 04:27:37 +02:00
|
|
|
XmlDocument xmldoc = new XmlDocument();
|
|
|
|
xmldoc.LoadXml(m_SessionObj.Identify(0));
|
|
|
|
WriteObject(xmldoc.DocumentElement);
|
|
|
|
}
|
2019-04-19 05:30:24 +02:00
|
|
|
catch (Exception)
|
2016-07-14 04:27:37 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-12-31 11:10:15 +01:00
|
|
|
if (!string.IsNullOrEmpty(m_SessionObj.Error))
|
2016-07-14 04:27:37 +02:00
|
|
|
{
|
|
|
|
XmlDocument ErrorDoc = new XmlDocument();
|
|
|
|
ErrorDoc.LoadXml(m_SessionObj.Error);
|
|
|
|
InvalidOperationException ex = new InvalidOperationException(ErrorDoc.OuterXml);
|
|
|
|
ErrorRecord er = new ErrorRecord(ex, "WsManError", ErrorCategory.InvalidOperation, computername);
|
|
|
|
this.WriteError(er);
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 05:30:24 +02:00
|
|
|
catch (Exception)
|
|
|
|
{ }
|
2016-07-14 04:27:37 +02:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (m_SessionObj != null)
|
|
|
|
Dispose(m_SessionObj);
|
|
|
|
}
|
2019-01-06 02:51:20 +01:00
|
|
|
}
|
2016-07-14 04:27:37 +02:00
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
|
|
|
|
/// <summary>
|
2019-01-09 22:07:41 +01:00
|
|
|
/// Public dispose method.
|
2016-07-14 04:27:37 +02:00
|
|
|
/// </summary>
|
|
|
|
public
|
|
|
|
void
|
|
|
|
Dispose()
|
|
|
|
{
|
2019-01-06 02:51:20 +01:00
|
|
|
// CleanUp();
|
2016-07-14 04:27:37 +02:00
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
/// <summary>
|
2019-01-09 22:07:41 +01:00
|
|
|
/// Public dispose method.
|
2016-07-14 04:27:37 +02:00
|
|
|
/// </summary>
|
|
|
|
public
|
|
|
|
void
|
|
|
|
Dispose(IWSManSession sessionObject)
|
|
|
|
{
|
|
|
|
sessionObject = null;
|
|
|
|
this.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion IDisposable Members
|
2019-01-06 02:51:20 +01:00
|
|
|
}
|
2016-07-14 04:27:37 +02:00
|
|
|
#endregion
|
|
|
|
}
|