MegaBites-AI/Windows-powershell
0372
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.Reflection;13using System.Runtime.CompilerServices;14using System.Runtime.InteropServices;15using System.Xml;16 17namespace Microsoft.WSMan.Management18{19 #region Test-WSMAN20 21 /// <summary>22 /// Issues an operation against the remote machine to ensure that the wsman23 /// service is running.24 /// </summary>25 [Cmdlet(VerbsDiagnostic.Test, "WSMan", HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2097114")]26 [OutputType(typeof(XmlElement))]27 public class TestWSManCommand : AuthenticatingWSManCommand, IDisposable28 {29 /// <summary>30 /// The following is the definition of the input parameter "ComputerName".31 /// Executes the management operation on the specified computer. The default is32 /// the local computer. Type the fully qualified domain name, NETBIOS name or IP33 /// address to indicate the remote host.34 /// </summary>35 [Parameter(Position = 0, ValueFromPipeline = true)]36 [Alias("cn")]37 public string ComputerName38 {39 get40 {41 return computername;42 }43 44 set45 {46 computername = value;47 if ((string.IsNullOrEmpty(computername)) || (computername.Equals(".", StringComparison.OrdinalIgnoreCase)))48 {49 computername = "localhost";50 }51 }52 }53 54 private string computername = null;55 56 /// <summary>57 /// The following is the definition of the input parameter "Authentication".58 /// This parameter takes a set of authentication methods the user can select59 /// from. The available method are an enum called AuthenticationMechanism in the60 /// System.Management.Automation.Runspaces namespace. The available options61 /// should be as follows:62 /// - Default : Use the default authentication (ad defined by the underlying63 /// protocol) for establishing a remote connection.64 /// - Negotiate65 /// - Kerberos66 /// - Basic: Use basic authentication for establishing a remote connection.67 /// -CredSSP: Use CredSSP authentication for establishing a remote connection68 /// which will enable the user to perform credential delegation. (i.e. second69 /// hop)70 /// </summary>71 /// <remarks>72 /// Overriding to use a different default than the one in AuthenticatingWSManCommand base class73 /// </remarks>74 [Parameter]75 [ValidateNotNullOrEmpty]76 [Alias("auth", "am")]77 public override AuthenticationMechanism Authentication78 {79 get80 {81 return authentication;82 }83 84 set85 {86 authentication = value;87 ValidateSpecifiedAuthentication();88 }89 }90 91 private AuthenticationMechanism authentication = AuthenticationMechanism.None;92 93 /// <summary>94 /// The following is the definition of the input parameter "Port".95 /// Specifies the port to be used when connecting to the ws management service.96 /// </summary>97 [Parameter(ParameterSetName = "ComputerName")]98 [ValidateNotNullOrEmpty]99 [ValidateRange(1, int.MaxValue)]100 public int Port101 {102 get { return port; }103 104 set { port = value; }105 }106 107 private int port = 0;108 109 /// <summary>110 /// The following is the definition of the input parameter "UseSSL".111 /// Uses the Secure Sockets Layer (SSL) protocol to establish a connection to112 /// the remote computer. If SSL is not available on the port specified by the113 /// Port parameter, the command fails.114 /// </summary>115 [Parameter(ParameterSetName = "ComputerName")]116 [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "SSL")]117 public SwitchParameter UseSSL118 {119 get { return usessl; }120 121 set { usessl = value; }122 }123 124 private SwitchParameter usessl;125 126 /// <summary>127 /// The following is the definition of the input parameter "ApplicationName".128 /// ApplicationName identifies the remote endpoint.129 /// </summary>130 [Parameter(ParameterSetName = "ComputerName")]131 [ValidateNotNullOrEmpty]132 public string ApplicationName133 {134 get { return applicationname; }135 136 set { applicationname = value; }137 }138 139 private string applicationname = null;140 141 /// <summary>142 /// ProcessRecord method.143 /// </summary>144 protected override void ProcessRecord()145 {146 WSManHelper helper = new WSManHelper(this);147 IWSManEx wsmanObject = (IWSManEx)new WSManClass();148 string connectionStr = string.Empty;149 connectionStr = helper.CreateConnectionString(null, port, computername, applicationname);150 IWSManSession m_SessionObj = null;151 try152 {153 m_SessionObj = helper.CreateSessionObject(wsmanObject, Authentication, null, Credential, connectionStr, CertificateThumbprint, usessl.IsPresent);154 m_SessionObj.Timeout = 1000; // 1 sec. we are putting this low so that Test-WSMan can return promptly if the server goes unresponsive.155 XmlDocument xmldoc = new XmlDocument();156 xmldoc.LoadXml(m_SessionObj.Identify(0));157 WriteObject(xmldoc.DocumentElement);158 }159 catch (Exception)160 {161 try162 {163 if (!string.IsNullOrEmpty(m_SessionObj.Error))164 {165 XmlDocument ErrorDoc = new XmlDocument();166 ErrorDoc.LoadXml(m_SessionObj.Error);167 InvalidOperationException ex = new InvalidOperationException(ErrorDoc.OuterXml);168 ErrorRecord er = new ErrorRecord(ex, "WsManError", ErrorCategory.InvalidOperation, computername);169 this.WriteError(er);170 }171 }172 catch (Exception)173 { }174 }175 finally176 {177 if (m_SessionObj != null)178 Dispose(m_SessionObj);179 }180 }181 182 #region IDisposable Members183 184 /// <summary>185 /// Public dispose method.186 /// </summary>187 public188 void189 Dispose()190 {191 // CleanUp();192 GC.SuppressFinalize(this);193 }194 /// <summary>195 /// Public dispose method.196 /// </summary>197 public198 void199 Dispose(IWSManSession sessionObject)200 {201 sessionObject = null;202 this.Dispose();203 }204 205 #endregion IDisposable Members206 }207 #endregion208}209 