示例#1
0
        internal static TransportErrorOccuredEventArgs ConstructTransportErrorEventArgs(IntPtr wsmanAPIHandle, WSManClientSessionTransportManager wsmanSessionTM, WSManNativeApi.WSManError errorStruct, TransportMethodEnum transportMethodReportingError, string resourceString, params object[] resourceArgs)
        {
            PSRemotingTransportException exception;

            if ((errorStruct.errorCode == -2144108135) && (wsmanSessionTM != null))
            {
                string redirectLocation = WSManNativeApi.WSManGetSessionOptionAsString(wsmanSessionTM.SessionHandle, WSManNativeApi.WSManSessionOption.WSMAN_OPTION_REDIRECT_LOCATION);
                string str2             = ParseEscapeWSManErrorMessage(WSManNativeApi.WSManGetErrorMessage(wsmanAPIHandle, errorStruct.errorCode)).Trim();
                exception = new PSRemotingTransportRedirectException(redirectLocation, PSRemotingErrorId.URIEndPointNotResolved, RemotingErrorIdStrings.URIEndPointNotResolved, new object[] { str2, redirectLocation });
            }
            else if ((errorStruct.errorCode == -2144108485) && (wsmanSessionTM != null))
            {
                string str3 = wsmanSessionTM.ConnectionInfo.ShellUri.Replace("http://schemas.microsoft.com/powershell/", string.Empty);
                string str4 = PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.InvalidConfigurationName, new object[] { str3, wsmanSessionTM.ConnectionInfo.ComputerName });
                exception = new PSRemotingTransportException(PSRemotingErrorId.InvalidConfigurationName, RemotingErrorIdStrings.ConnectExCallBackError, new object[] { wsmanSessionTM.ConnectionInfo.ComputerName, str4 })
                {
                    TransportMessage = ParseEscapeWSManErrorMessage(WSManNativeApi.WSManGetErrorMessage(wsmanAPIHandle, errorStruct.errorCode))
                };
            }
            else
            {
                string str5 = PSRemotingErrorInvariants.FormatResourceString(resourceString, resourceArgs);
                exception = new PSRemotingTransportException(PSRemotingErrorId.TroubleShootingHelpTopic, RemotingErrorIdStrings.TroubleShootingHelpTopic, new object[] { str5 })
                {
                    TransportMessage = ParseEscapeWSManErrorMessage(WSManNativeApi.WSManGetErrorMessage(wsmanAPIHandle, errorStruct.errorCode))
                };
            }
            exception.ErrorCode = errorStruct.errorCode;
            return(new TransportErrorOccuredEventArgs(exception, transportMethodReportingError));
        }
示例#2
0
        internal static string WSManGetErrorMessage(IntPtr wsManAPIHandle, int errorCode)
        {
            string name = Thread.CurrentThread.CurrentUICulture.Name;
            string str  = "";
            int    messageLengthUsed = 0;

            if (122 != WSManNativeApi.WSManGetErrorMessage(wsManAPIHandle, 0, name, errorCode, 0, (byte[])null, out messageLengthUsed))
            {
                return(str);
            }
            byte[] numArray = new byte[messageLengthUsed * 2];
            if (WSManNativeApi.WSManGetErrorMessage(wsManAPIHandle, 0, name, errorCode, messageLengthUsed * 2, numArray, out int _) != 0)
            {
                return(str);
            }
            try
            {
                str = Encoding.Unicode.GetString(numArray);
            }
            catch (ArgumentNullException ex)
            {
            }
            catch (DecoderFallbackException ex)
            {
            }
            return(str);
        }
        internal void SetWSManSessionOption(WSManNativeApi.WSManSessionOption option, int dwordData)
        {
            int errorCode = WSManNativeApi.WSManSetSessionOption(this.wsManSessionHandle, option, new WSManNativeApi.WSManDataDWord(dwordData));

            if (errorCode != 0)
            {
                throw new PSInvalidOperationException(WSManNativeApi.WSManGetErrorMessage(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, errorCode));
            }
        }
 internal void SetWSManSessionOption(WSManNativeApi.WSManSessionOption option, string stringData)
 {
     using (WSManNativeApi.WSManData wsManData = new WSManNativeApi.WSManData(stringData))
     {
         int errorCode = WSManNativeApi.WSManSetSessionOption(this.wsManSessionHandle, option, (IntPtr)wsManData);
         if (errorCode != 0)
         {
             throw new PSInvalidOperationException(WSManNativeApi.WSManGetErrorMessage(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, errorCode));
         }
     }
 }
 internal void SetSignalTimeOut(int milliseconds)
 {
     using (BaseClientTransportManager.tracer.TraceMethod("Setting SignalShell timeout: {0} milliseconds", (object)milliseconds))
     {
         int errorCode = WSManNativeApi.WSManSetSessionOption(this.wsManSessionHandle, WSManNativeApi.WSManSessionOption.WSMAN_OPTION_TIMEOUTMS_SIGNAL_SHELL, new WSManNativeApi.WSManDataDWord(milliseconds));
         if (errorCode != 0)
         {
             throw new PSInvalidOperationException(WSManNativeApi.WSManGetErrorMessage(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, errorCode));
         }
     }
 }
        private void Initialize(Uri connectionUri, WSManConnectionInfo connectionInfo)
        {
            bool   isSSLSpecified = false;
            string str            = connectionUri.OriginalString;

            if (connectionUri == connectionInfo.ConnectionUri && connectionInfo.UseDefaultWSManPort)
            {
                str = WSManConnectionInfo.GetConnectionString(connectionInfo.ConnectionUri, out isSSLSpecified);
            }
            string connection;

            if (string.IsNullOrEmpty(connectionUri.Query))
            {
                connection = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0}?PSVersion={1}", (object)str.TrimEnd('/'), (object)PSVersionInfo.PSVersion);
            }
            else
            {
                connection = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0};PSVersion={1}", (object)str, (object)PSVersionInfo.PSVersion);
            }
            WSManNativeApi.BaseWSManAuthenticationCredentials authenticationCredentials;
            if (connectionInfo.CertificateThumbprint != null)
            {
                authenticationCredentials = (WSManNativeApi.BaseWSManAuthenticationCredentials) new WSManNativeApi.WSManCertificateThumbprintCredentials(connectionInfo.CertificateThumbprint);
            }
            else
            {
                string       name = (string)null;
                SecureString pwd  = (SecureString)null;
                if (connectionInfo.Credential != null && !string.IsNullOrEmpty(connectionInfo.Credential.UserName))
                {
                    name = connectionInfo.Credential.UserName;
                    pwd  = connectionInfo.Credential.Password;
                }
                authenticationCredentials = (WSManNativeApi.BaseWSManAuthenticationCredentials) new WSManNativeApi.WSManUserNameAuthenticationCredentials(name, pwd, connectionInfo.WSManAuthenticationMechanism);
            }
            WSManNativeApi.WSManUserNameAuthenticationCredentials authCredentials = (WSManNativeApi.WSManUserNameAuthenticationCredentials)null;
            if (connectionInfo.ProxyCredential != null)
            {
                WSManNativeApi.WSManAuthenticationMechanism authMechanism = WSManNativeApi.WSManAuthenticationMechanism.WSMAN_FLAG_AUTH_NEGOTIATE;
                string       name = (string)null;
                SecureString pwd  = (SecureString)null;
                switch (connectionInfo.ProxyAuthentication)
                {
                case AuthenticationMechanism.Basic:
                    authMechanism = WSManNativeApi.WSManAuthenticationMechanism.WSMAN_FLAG_AUTH_BASIC;
                    break;

                case AuthenticationMechanism.Negotiate:
                    authMechanism = WSManNativeApi.WSManAuthenticationMechanism.WSMAN_FLAG_AUTH_NEGOTIATE;
                    break;

                case AuthenticationMechanism.Digest:
                    authMechanism = WSManNativeApi.WSManAuthenticationMechanism.WSMAN_FLAG_AUTH_DIGEST;
                    break;
                }
                if (!string.IsNullOrEmpty(connectionInfo.ProxyCredential.UserName))
                {
                    name = connectionInfo.ProxyCredential.UserName;
                    pwd  = connectionInfo.ProxyCredential.Password;
                }
                authCredentials = new WSManNativeApi.WSManUserNameAuthenticationCredentials(name, pwd, authMechanism);
            }
            WSManNativeApi.WSManProxyInfo wsManProxyInfo = connectionInfo.ProxyAccessType == ProxyAccessType.None ? (WSManNativeApi.WSManProxyInfo)null : new WSManNativeApi.WSManProxyInfo(connectionInfo.ProxyAccessType, authCredentials);
            int errorCode = 0;

            try
            {
                errorCode = WSManNativeApi.WSManCreateSession(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, connection, 0, (IntPtr)authenticationCredentials.GetMarshalledObject(), wsManProxyInfo == null ? IntPtr.Zero : (IntPtr)wsManProxyInfo, ref this.wsManSessionHandle);
            }
            finally
            {
                authCredentials?.Dispose();
                wsManProxyInfo?.Dispose();
                authenticationCredentials?.Dispose();
            }
            if (errorCode != 0)
            {
                throw new PSInvalidOperationException(WSManNativeApi.WSManGetErrorMessage(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, errorCode));
            }
            int num;

            WSManNativeApi.WSManGetSessionOptionAsDword(this.wsManSessionHandle, WSManNativeApi.WSManSessionOption.WSMAN_OPTION_SHELL_MAX_DATA_SIZE_PER_MESSAGE_KB, out num);
            this.Fragmentor.FragmentSize = num << 10;
            this.dataToBeSent.Fragmentor = this.Fragmentor;
            this.noCompression           = !connectionInfo.UseCompression;
            this.noMachineProfile        = connectionInfo.NoMachineProfile;
            this.idleTimeout             = (long)connectionInfo.IdleTimeout;
            if (isSSLSpecified)
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_USE_SSL, 1);
            }
            if (connectionInfo.NoEncryption)
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_UNENCRYPTED_MESSAGES, 1);
            }
            if (connectionInfo.AllowImplicitCredentialForNegotiate)
            {
                WSManNativeApi.WSManSetSessionOption(this.wsManSessionHandle, WSManNativeApi.WSManSessionOption.WSMAN_OPTION_ALLOW_NEGOTIATE_IMPLICIT_CREDENTIALS, new WSManNativeApi.WSManDataDWord(1));
            }
            if (connectionInfo.UseUTF16)
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_UTF16, 1);
            }
            if (connectionInfo.SkipCACheck)
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_SKIP_CA_CHECK, 1);
            }
            if (connectionInfo.SkipCNCheck)
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_SKIP_CN_CHECK, 1);
            }
            if (connectionInfo.SkipRevocationCheck)
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_SKIP_REVOCATION_CHECK, 1);
            }
            string name1 = connectionInfo.UICulture.Name;

            if (!string.IsNullOrEmpty(name1))
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_UI_LANGUAGE, name1);
            }
            string name2 = connectionInfo.Culture.Name;

            if (!string.IsNullOrEmpty(name2))
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_LOCALE, name2);
            }
            this.SetDefaultTimeOut(connectionInfo.OperationTimeout);
            this.SetConnectTimeOut(connectionInfo.OpenTimeout);
            this.SetCloseTimeOut(connectionInfo.CancelTimeout);
            this.SetSignalTimeOut(connectionInfo.CancelTimeout);
        }