Exemplo n.º 1
0
        /// <summary>
        /// BeginProcessing method.
        /// </summary>
        protected override void BeginProcessing()
        {
            WSManHelper helper = new WSManHelper(this);

            if (proxyauthentication.Equals(ProxyAuthentication.Basic) || proxyauthentication.Equals(ProxyAuthentication.Digest))
            {
                if (_proxycredential == null)
                {
                    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;
            }



            //Creating the Session Object
            SessionOption objSessionOption = new SessionOption();

            objSessionOption.SPNPort             = spnport;
            objSessionOption.UseUtf16            = useutf16;
            objSessionOption.SkipCNCheck         = skipcncheck;
            objSessionOption.SkipCACheck         = skipcacheck;
            objSessionOption.OperationTimeout    = operationtimeout;
            objSessionOption.SkipRevocationCheck = skiprevocationcheck;
            //Proxy Settings
            objSessionOption.ProxyAccessType     = _proxyaccesstype;
            objSessionOption.ProxyAuthentication = proxyauthentication;

            if (noencryption)
            {
                objSessionOption.UseEncryption = false;
            }
            if (_proxycredential != null)
            {
                NetworkCredential nwCredentials = _proxycredential.GetNetworkCredential();
                objSessionOption.ProxyCredential = nwCredentials;
            }
            WriteObject(objSessionOption);
        } //End BeginProcessing()
Exemplo n.º 2
0
        protected override void BeginProcessing()
        {
            WSManHelper wSManHelper = new WSManHelper(this);

            if ((this.proxyauthentication.Equals(ProxyAuthentication.Basic) || this.proxyauthentication.Equals(ProxyAuthentication.Digest)) && this._proxycredential == null)
            {
                InvalidOperationException invalidOperationException = new InvalidOperationException(wSManHelper.GetResourceMsgFromResourcetext("NewWSManSessionOptionCred"));
                ErrorRecord errorRecord = new ErrorRecord(invalidOperationException, "InvalidOperationException", ErrorCategory.InvalidOperation, null);
                base.WriteError(errorRecord);
                return;
            }
            else
            {
                if (this._proxycredential == null || this.proxyauthentication != 0)
                {
                    SessionOption sessionOption = new SessionOption();
                    sessionOption.SPNPort             = this.spnport;
                    sessionOption.UseUtf16            = this.useutf16;
                    sessionOption.SkipCNCheck         = this.skipcncheck;
                    sessionOption.SkipCACheck         = this.skipcacheck;
                    sessionOption.OperationTimeout    = this.operationtimeout;
                    sessionOption.SkipRevocationCheck = this.skiprevocationcheck;
                    sessionOption.ProxyAccessType     = this._proxyacesstype;
                    sessionOption.ProxyAuthentication = this.proxyauthentication;
                    if (this.noencryption)
                    {
                        sessionOption.UseEncryption = false;
                    }
                    if (this._proxycredential != null)
                    {
                        NetworkCredential networkCredential = this._proxycredential.GetNetworkCredential();
                        sessionOption.ProxyCredential = networkCredential;
                    }
                    base.WriteObject(sessionOption);
                    return;
                }
                else
                {
                    InvalidOperationException invalidOperationException1 = new InvalidOperationException(wSManHelper.GetResourceMsgFromResourcetext("NewWSManSessionOptionAuth"));
                    ErrorRecord errorRecord1 = new ErrorRecord(invalidOperationException1, "InvalidOperationException", ErrorCategory.InvalidOperation, null);
                    base.WriteError(errorRecord1);
                    return;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// This method is used by Connect-WsMan Cmdlet and New-Item of WsMan Provider to create connection to WsMan
        /// </summary>
        /// <param name="ParameterSetName"></param>
        /// <param name="connectionuri"></param>
        /// <param name="port"></param>
        /// <param name="computername"></param>
        /// <param name="applicationname"></param>
        /// <param name="usessl"></param>
        /// <param name="authentication"></param>
        /// <param name="sessionoption"></param>
        /// <param name="credential"></param>
        /// <param name="certificateThumbprint"></param>
        internal void CreateWsManConnection(string ParameterSetName, Uri connectionuri, int port, string computername, string applicationname, bool usessl, AuthenticationMechanism authentication, SessionOption sessionoption, PSCredential credential, string certificateThumbprint)
        {
            IWSManEx m_wsmanObject = (IWSManEx) new WSManClass();

            try
            {
                string connectionStr = CreateConnectionString(connectionuri, port, computername, applicationname);
                if (connectionuri != null)
                {
                    //in the format http(s)://server[:port/applicationname]
                    string[] constrsplit  = connectionStr.Split(new string[] { ":" + port + "/" + applicationname }, StringSplitOptions.None);
                    string[] constrsplit1 = constrsplit[0].Split(new string[] { "//" }, StringSplitOptions.None);
                    computername = constrsplit1[1].Trim();
                }
                IWSManSession m_session = CreateSessionObject(m_wsmanObject, authentication, sessionoption, credential, connectionStr, certificateThumbprint, usessl);
                m_session.Identify(0);
                string key = computername;
                if (key == null)
                {
                    key = "localhost";
                }
                AddtoDictionary(key, m_session);
            }
            catch (IndexOutOfRangeException)
            {
                AssertError(_resourceMgr.GetString("NotProperURI"), false, connectionuri);
            }
            catch (Exception ex)
            {
                AssertError(ex.Message, false, computername);
            }
            finally
            {
                if (!String.IsNullOrEmpty(m_wsmanObject.Error))
                {
                    AssertError(m_wsmanObject.Error, true, computername);
                }
            }
        }
Exemplo n.º 4
0
        internal IWSManSession CreateSessionObject(IWSManEx wsmanObject, AuthenticationMechanism authentication, SessionOption sessionoption, PSCredential credential, string connectionString, string certificateThumbprint, bool usessl)
        {
            ValidateSpecifiedAuthentication(authentication, credential, certificateThumbprint);

            ////if authentication is given
            int sessionFlags = 0;

            if (authentication.ToString() != null)
            {
                if (authentication.Equals(AuthenticationMechanism.None))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseNoAuthentication;
                }
                if (authentication.Equals(AuthenticationMechanism.Basic))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseBasic | (int)WSManSessionFlags.WSManFlagCredUserNamePassword;
                }
                if (authentication.Equals(AuthenticationMechanism.Negotiate))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseNegotiate;
                }
                if (authentication.Equals(AuthenticationMechanism.Kerberos))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseKerberos;
                }
                if (authentication.Equals(AuthenticationMechanism.Digest))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseDigest | (int)WSManSessionFlags.WSManFlagCredUserNamePassword;
                }
                if (authentication.Equals(AuthenticationMechanism.Credssp))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseCredSsp | (int)WSManSessionFlags.WSManFlagCredUserNamePassword;
                }
                if (authentication.Equals(AuthenticationMechanism.ClientCertificate))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseClientCertificate;
                }
            }

            IWSManConnectionOptionsEx2 connObject = (IWSManConnectionOptionsEx2)wsmanObject.CreateConnectionOptions();

            if (credential != null)
            {
                //connObject = (IWSManConnectionOptionsEx2)wsmanObject.CreateConnectionOptions();
                System.Net.NetworkCredential nwCredential = new System.Net.NetworkCredential();
                if (credential.UserName != null)
                {
                    nwCredential = credential.GetNetworkCredential();
                    if (String.IsNullOrEmpty(nwCredential.Domain))
                    {
                        if (authentication.Equals(AuthenticationMechanism.Digest) || authentication.Equals(AuthenticationMechanism.Basic))
                        {
                            connObject.UserName = nwCredential.UserName;
                        }
                        else
                        {
                            // just wanted to not use null domain, empty is actually fine
                            connObject.UserName = "******" + nwCredential.UserName;
                        }
                    }
                    else
                    {
                        connObject.UserName = nwCredential.Domain + "\\" + nwCredential.UserName;
                    }
                    connObject.Password = nwCredential.Password;
                    if (!authentication.Equals(AuthenticationMechanism.Credssp) || !authentication.Equals(AuthenticationMechanism.Digest) || authentication.Equals(AuthenticationMechanism.Basic))
                    {
                        sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagCredUserNamePassword;
                    }
                }
            }

            if (certificateThumbprint != null)
            {
                connObject.CertificateThumbprint = certificateThumbprint;
                sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseClientCertificate;
            }

            if (sessionoption != null)
            {
                if (sessionoption.ProxyAuthentication != 0)
                {
                    int ProxyAccessflags         = 0;
                    int ProxyAuthenticationFlags = 0;
                    if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyIEConfig))
                    {
                        ProxyAccessflags = connObject.ProxyIEConfig();
                    }
                    else if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyAutoDetect))
                    {
                        ProxyAccessflags = connObject.ProxyAutoDetect();
                    }
                    else if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyNoProxyServer))
                    {
                        ProxyAccessflags = connObject.ProxyNoProxyServer();
                    }
                    else if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyWinHttpConfig))
                    {
                        ProxyAccessflags = connObject.ProxyWinHttpConfig();
                    }

                    if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Basic))
                    {
                        ProxyAuthenticationFlags = connObject.ProxyAuthenticationUseBasic();
                    }
                    else if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Negotiate))
                    {
                        ProxyAuthenticationFlags = connObject.ProxyAuthenticationUseNegotiate();
                    }
                    else if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Digest))
                    {
                        ProxyAuthenticationFlags = connObject.ProxyAuthenticationUseDigest();
                    }
                    if (sessionoption.ProxyCredential != null)
                    {
                        try
                        {
                            connObject.SetProxy(ProxyAccessflags, ProxyAuthenticationFlags, sessionoption.ProxyCredential.UserName, sessionoption.ProxyCredential.Password);
                        }
                        catch (Exception ex)
                        {
                            AssertError(ex.Message, false, null);
                        }
                    }
                    else
                    {
                        connObject.SetProxy((int)sessionoption.ProxyAccessType, (int)sessionoption.ProxyAuthentication, null, null);
                    }
                }
                if (sessionoption.SkipCACheck)
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagSkipCACheck;
                }
                if (sessionoption.SkipCNCheck)
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagSkipCNCheck;
                }
                if (sessionoption.SPNPort > 0)
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagEnableSpnServerPort;
                }
                if (sessionoption.UseUtf16)
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUtf16;
                }
                else
                {
                    //If UseUtf16 is false, then default Encoding is Utf8
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUtf8;
                }
                if (!sessionoption.UseEncryption)
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagNoEncryption;
                }
                if (sessionoption.SkipRevocationCheck)
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagSkipRevocationCheck;
                }
            }
            else
            {
                //If SessionOption is null then, default Encoding is Utf8
                sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUtf8;
            }

            if (usessl)
            {
                sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseSsl;
            }

            IWSManSession m_SessionObj = null;

            try
            {
                m_SessionObj = (IWSManSession)wsmanObject.CreateSession(connectionString, sessionFlags, connObject);
                if (sessionoption != null)
                {
                    if (sessionoption.OperationTimeout > 0)
                    {
                        m_SessionObj.Timeout = sessionoption.OperationTimeout;
                    }
                }
            }
            catch (COMException ex)
            {
                AssertError(ex.Message, false, null);
            }
            return(m_SessionObj);
        }
Exemplo n.º 5
0
        /// <summary>
        /// BeginProcessing method.
        /// </summary>
        protected override void BeginProcessing()
        {
            WSManHelper helper = new WSManHelper(this);

            if (proxyauthentication.Equals(ProxyAuthentication.Basic) || proxyauthentication.Equals(ProxyAuthentication.Digest))
            {
                if (_proxycredential == null)
                {
                    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;
            }



            //Creating the Session Object
            SessionOption objSessionOption = new SessionOption();

            objSessionOption.SPNPort = spnport;
            objSessionOption.UseUtf16 = useutf16;
            objSessionOption.SkipCNCheck = skipcncheck;
            objSessionOption.SkipCACheck = skipcacheck;
            objSessionOption.OperationTimeout = operationtimeout;
            objSessionOption.SkipRevocationCheck = skiprevocationcheck;
            //Proxy Settings
            objSessionOption.ProxyAccessType = _proxyacesstype;
            objSessionOption.ProxyAuthentication = proxyauthentication;

            if (noencryption)
            {
                objSessionOption.UseEncryption = false;
            }
            if (_proxycredential != null)
            {
                NetworkCredential nwCredentials = _proxycredential.GetNetworkCredential();
                objSessionOption.ProxyCredential = nwCredentials;
            }
            WriteObject(objSessionOption);

        }//End BeginProcessing()
Exemplo n.º 6
0
		internal void CreateWsManConnection(string ParameterSetName, Uri connectionuri, int port, string computername, string applicationname, bool usessl, AuthenticationMechanism authentication, SessionOption sessionoption, PSCredential credential, string certificateThumbprint)
		{
			IWSManEx wSManClass = (IWSManEx)(new WSManClass());
			try
			{
				try
				{
					string str = this.CreateConnectionString(connectionuri, port, computername, applicationname);
					if (connectionuri != null)
					{
						string[] strArrays = new string[1];
						object[] objArray = new object[4];
						objArray[0] = ":";
						objArray[1] = port;
						objArray[2] = "/";
						objArray[3] = applicationname;
						strArrays[0] = string.Concat(objArray);
						string[] strArrays1 = str.Split(strArrays, StringSplitOptions.None);
						string[] strArrays2 = new string[1];
						strArrays2[0] = "//";
						string[] strArrays3 = strArrays1[0].Split(strArrays2, StringSplitOptions.None);
						computername = strArrays3[1].Trim();
					}
					IWSManSession wSManSession = this.CreateSessionObject(wSManClass, authentication, sessionoption, credential, str, certificateThumbprint, usessl);
					wSManSession.Identify(0);
					string str1 = computername;
					if (str1 == null)
					{
						str1 = "localhost";
					}
					this.AddtoDictionary(str1, wSManSession);
				}
				catch (IndexOutOfRangeException indexOutOfRangeException)
				{
					this.AssertError(this._resourceMgr.GetString("NotProperURI"), false, connectionuri);
				}
				catch (Exception exception1)
				{
					Exception exception = exception1;
					this.AssertError(exception.Message, false, computername);
				}
			}
			finally
			{
				if (!string.IsNullOrEmpty(wSManClass.Error))
				{
					this.AssertError(wSManClass.Error, true, computername);
				}
			}
		}
Exemplo n.º 7
0
		internal IWSManSession CreateSessionObject(IWSManEx wsmanObject, AuthenticationMechanism authentication, SessionOption sessionoption, PSCredential credential, string connectionString, string certificateThumbprint, bool usessl)
		{
			WSManHelper.ValidateSpecifiedAuthentication(authentication, credential, certificateThumbprint);
			int num = 0;
			if (authentication.ToString() != null)
			{
				if (authentication.Equals(AuthenticationMechanism.None))
				{
					num = num | 0x8000;
				}
				if (authentication.Equals(AuthenticationMechanism.Basic))
				{
					num = num | 0x40000 | 0x1000;
				}
				if (authentication.Equals(AuthenticationMechanism.Negotiate))
				{
					num = num | 0x20000;
				}
				if (authentication.Equals(AuthenticationMechanism.Kerberos))
				{
					num = num | 0x80000;
				}
				if (authentication.Equals(AuthenticationMechanism.Digest))
				{
					num = num | 0x10000 | 0x1000;
				}
				if (authentication.Equals(AuthenticationMechanism.Credssp))
				{
					num = num | 0x1000000 | 0x1000;
				}
				if (authentication.Equals(AuthenticationMechanism.ClientCertificate))
				{
					num = num | 0x200000;
				}
			}
			IWSManConnectionOptionsEx2 userName = (IWSManConnectionOptionsEx2)wsmanObject.CreateConnectionOptions();
			if (credential != null)
			{
				if (credential.UserName != null)
				{
					NetworkCredential networkCredential = credential.GetNetworkCredential();
					if (!string.IsNullOrEmpty(networkCredential.Domain))
					{
						userName.UserName = string.Concat(networkCredential.Domain, "\\", networkCredential.UserName);
					}
					else
					{
						if (authentication.Equals(AuthenticationMechanism.Digest) || authentication.Equals(AuthenticationMechanism.Basic))
						{
							userName.UserName = networkCredential.UserName;
						}
						else
						{
							userName.UserName = string.Concat("\\", networkCredential.UserName);
						}
					}
					userName.Password = networkCredential.Password;
					if (!authentication.Equals(AuthenticationMechanism.Credssp) || !authentication.Equals(AuthenticationMechanism.Digest) || authentication.Equals(AuthenticationMechanism.Basic))
					{
						num = num | 0x1000;
					}
				}
			}
			if (certificateThumbprint != null)
			{
				userName.CertificateThumbprint = certificateThumbprint;
				num = num | 0x200000;
			}
			if (sessionoption == null)
			{
				num = num | 1;
			}
			else
			{
				if (sessionoption.ProxyAuthentication != 0)
				{
					int num1 = 0;
					int num2 = 0;
					if (!sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyIEConfig))
					{
						if (!sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyAutoDetect))
						{
							if (!sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyNoProxyServer))
							{
								if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyWinHttpConfig))
								{
									num1 = userName.ProxyWinHttpConfig();
								}
							}
							else
							{
								num1 = userName.ProxyNoProxyServer();
							}
						}
						else
						{
							num1 = userName.ProxyAutoDetect();
						}
					}
					else
					{
						num1 = userName.ProxyIEConfig();
					}
					if (!sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Basic))
					{
						if (!sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Negotiate))
						{
							if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Digest))
							{
								num2 = userName.ProxyAuthenticationUseDigest();
							}
						}
						else
						{
							num2 = userName.ProxyAuthenticationUseNegotiate();
						}
					}
					else
					{
						num2 = userName.ProxyAuthenticationUseBasic();
					}
					if (sessionoption.ProxyCredential == null)
					{
                        userName.SetProxy((int)sessionoption.ProxyAccessType, (int)sessionoption.ProxyAuthentication, null, null);
					}
					else
					{
						try
						{
							userName.SetProxy(num1, num2, sessionoption.ProxyCredential.UserName, sessionoption.ProxyCredential.Password);
						}
						catch (Exception exception1)
						{
							Exception exception = exception1;
							this.AssertError(exception.Message, false, null);
						}
					}
				}
				if (sessionoption.SkipCACheck)
				{
					num = num | 0x2000;
				}
				if (sessionoption.SkipCNCheck)
				{
					num = num | 0x4000;
				}
				if (sessionoption.SPNPort > 0)
				{
					num = num | 0x400000;
				}
				if (!sessionoption.UseUtf16)
				{
					num = num | 1;
				}
				else
				{
					num = num | 0x800000;
				}
				if (!sessionoption.UseEncryption)
				{
					num = num | 0x100000;
				}
				if (sessionoption.SkipRevocationCheck)
				{
					num = num | 0x2000000;
				}
			}
			if (usessl)
			{
				num = num | 0x8000000;
			}
			IWSManSession operationTimeout = null;
			try
			{
				operationTimeout = (IWSManSession)wsmanObject.CreateSession(connectionString, num, userName);
				if (sessionoption != null && sessionoption.OperationTimeout > 0)
				{
					operationTimeout.Timeout = sessionoption.OperationTimeout;
				}
			}
			catch (COMException cOMException1)
			{
				COMException cOMException = cOMException1;
				this.AssertError(cOMException.Message, false, null);
			}
			return operationTimeout;
		}
Exemplo n.º 8
0
        /// <summary>
        /// This method is used by Connect-WsMan Cmdlet and New-Item of WsMan Provider to create connection to WsMan
        /// </summary>
        /// <param name="ParameterSetName"></param>
        /// <param name="connectionuri"></param>
        /// <param name="port"></param>
        /// <param name="computername"></param>
        /// <param name="applicationname"></param>
        /// <param name="usessl"></param>
        /// <param name="authentication"></param>
        /// <param name="sessionoption"></param>
        /// <param name="credential"></param>
        /// <param name="certificateThumbprint"></param>
        internal void CreateWsManConnection(string ParameterSetName, Uri connectionuri, int port, string computername, string applicationname, bool usessl, AuthenticationMechanism authentication, SessionOption sessionoption, PSCredential credential, string certificateThumbprint)
        {
            IWSManEx m_wsmanObject = (IWSManEx)new WSManClass();
            try
            {
                string connectionStr = CreateConnectionString(connectionuri, port, computername, applicationname);
                if (connectionuri != null)
                {
                    //in the format http(s)://server[:port/applicationname]
                    string[] constrsplit = connectionStr.Split(new string[] { ":" + port + "/" + applicationname }, StringSplitOptions.None);
                    string[] constrsplit1 = constrsplit[0].Split(new string[] { "//" }, StringSplitOptions.None);
                    computername = constrsplit1[1].Trim();
                }
                IWSManSession m_session = CreateSessionObject(m_wsmanObject, authentication, sessionoption, credential, connectionStr, certificateThumbprint, usessl);
                m_session.Identify(0);
                string key = computername;
                if (key == null)
                {
                    key = "localhost";
                }
                AddtoDictionary(key, m_session);
            }
            catch (IndexOutOfRangeException)
            {
                AssertError(_resourceMgr.GetString("NotProperURI"), false, connectionuri);
            }
            catch (Exception ex)
            {
                AssertError(ex.Message, false, computername);
            }
            finally
            {
                if (!String.IsNullOrEmpty(m_wsmanObject.Error))
                {
                    AssertError(m_wsmanObject.Error, true, computername);
                }

            }
        }
Exemplo n.º 9
0
        internal IWSManSession CreateSessionObject(IWSManEx wsmanObject, AuthenticationMechanism authentication, SessionOption sessionoption, PSCredential credential, string connectionString, string certificateThumbprint, bool usessl)
        {
            ValidateSpecifiedAuthentication(authentication, credential, certificateThumbprint);

            ////if authentication is given
            int sessionFlags = 0;

            if (authentication.ToString() != null)
            {
                if (authentication.Equals(AuthenticationMechanism.None))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseNoAuthentication;
                }
                if (authentication.Equals(AuthenticationMechanism.Basic))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseBasic | (int)WSManSessionFlags.WSManFlagCredUserNamePassword;
                }
                if (authentication.Equals(AuthenticationMechanism.Negotiate))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseNegotiate;
                }
                if (authentication.Equals(AuthenticationMechanism.Kerberos))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseKerberos;
                }
                if (authentication.Equals(AuthenticationMechanism.Digest))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseDigest | (int)WSManSessionFlags.WSManFlagCredUserNamePassword;
                }
                if (authentication.Equals(AuthenticationMechanism.Credssp))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseCredSsp | (int)WSManSessionFlags.WSManFlagCredUserNamePassword;
                }
                if (authentication.Equals(AuthenticationMechanism.ClientCertificate))
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseClientCertificate;
                }

            }

            IWSManConnectionOptionsEx2 connObject = (IWSManConnectionOptionsEx2)wsmanObject.CreateConnectionOptions();
            if (credential != null)
            {
                //connObject = (IWSManConnectionOptionsEx2)wsmanObject.CreateConnectionOptions();
                System.Net.NetworkCredential nwCredential = new System.Net.NetworkCredential();
                if (credential.UserName != null)
                {
                    nwCredential = credential.GetNetworkCredential();
                    if (String.IsNullOrEmpty(nwCredential.Domain))
                    {
                        if ( authentication.Equals(AuthenticationMechanism.Digest) || authentication.Equals(AuthenticationMechanism.Basic) )
                        {
                            connObject.UserName = nwCredential.UserName;
                        }
                        else
                        {
                            // just wanted to not use null domain, empty is actually fine
                            connObject.UserName = "******" + nwCredential.UserName;
                        }
                    }
                    else
                    {
                        connObject.UserName = nwCredential.Domain + "\\" + nwCredential.UserName;
                    }
                    connObject.Password = nwCredential.Password;
                    if (!authentication.Equals(AuthenticationMechanism.Credssp) || !authentication.Equals(AuthenticationMechanism.Digest) || authentication.Equals(AuthenticationMechanism.Basic))
                    {
                        sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagCredUserNamePassword;
                    }
                }
            }

            if (certificateThumbprint != null)
            {
                connObject.CertificateThumbprint = certificateThumbprint;
                sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseClientCertificate;
            }

            if (sessionoption != null)
            {

                if (sessionoption.ProxyAuthentication != 0)
                {
                    int ProxyAccessflags = 0;
                    int ProxyAuthenticationFlags = 0;
                    if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyIEConfig))
                    {
                        ProxyAccessflags = connObject.ProxyIEConfig();
                    }
                    else if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyAutoDetect))
                    {
                        ProxyAccessflags = connObject.ProxyAutoDetect();
                    }
                    else if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyNoProxyServer))
                    {
                        ProxyAccessflags = connObject.ProxyNoProxyServer();
                    }
                    else if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyWinHttpConfig))
                    {
                        ProxyAccessflags = connObject.ProxyWinHttpConfig();
                    }

                    if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Basic))
                    {
                        ProxyAuthenticationFlags = connObject.ProxyAuthenticationUseBasic();
                    }
                    else if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Negotiate))
                    {
                        ProxyAuthenticationFlags = connObject.ProxyAuthenticationUseNegotiate();
                    }
                    else if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Digest))
                    {
                        ProxyAuthenticationFlags = connObject.ProxyAuthenticationUseDigest();
                    }
                    if (sessionoption.ProxyCredential != null)
                    {
                        try
                        {
                            connObject.SetProxy(ProxyAccessflags, ProxyAuthenticationFlags, sessionoption.ProxyCredential.UserName, sessionoption.ProxyCredential.Password);
                        }
                        catch (Exception ex)
                        {
                            AssertError(ex.Message, false, null);
                        }
                    }
                    else
                    {
                        connObject.SetProxy((int)sessionoption.ProxyAccessType, (int)sessionoption.ProxyAuthentication, null, null);
                    }


                }
                if (sessionoption.SkipCACheck)
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagSkipCACheck;
                }
                if (sessionoption.SkipCNCheck)
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagSkipCNCheck;
                }
                if (sessionoption.SPNPort > 0)
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagEnableSpnServerPort;
                }
                if (sessionoption.UseUtf16)
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUtf16;
                }
                else
                {
                    //If UseUtf16 is false, then default Encoding is Utf8
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUtf8;
                }
                if (!sessionoption.UseEncryption)
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagNoEncryption;
                }
                if (sessionoption.SkipRevocationCheck)
                {
                    sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagSkipRevocationCheck;
                }
            }
            else
            {
                //If SessionOption is null then, default Encoding is Utf8
                sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUtf8;
            }

            if (usessl)
            {
                sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseSsl;
            }

            IWSManSession m_SessionObj = null;
            try
            {
                m_SessionObj = (IWSManSession)wsmanObject.CreateSession(connectionString, sessionFlags, connObject);
                if (sessionoption != null)
                {
                    if (sessionoption.OperationTimeout > 0)
                    {
                        m_SessionObj.Timeout = sessionoption.OperationTimeout;
                    }
                }
            }
            catch (COMException ex)
            {
                AssertError(ex.Message, false, null);
            }
            return m_SessionObj;
        }
Exemplo n.º 10
0
		protected override void BeginProcessing()
		{
			WSManHelper wSManHelper = new WSManHelper(this);
			if ((this.proxyauthentication.Equals(ProxyAuthentication.Basic) || this.proxyauthentication.Equals(ProxyAuthentication.Digest)) && this._proxycredential == null)
			{
				InvalidOperationException invalidOperationException = new InvalidOperationException(wSManHelper.GetResourceMsgFromResourcetext("NewWSManSessionOptionCred"));
				ErrorRecord errorRecord = new ErrorRecord(invalidOperationException, "InvalidOperationException", ErrorCategory.InvalidOperation, null);
				base.WriteError(errorRecord);
				return;
			}
			else
			{
				if (this._proxycredential == null || this.proxyauthentication != 0)
				{
					SessionOption sessionOption = new SessionOption();
					sessionOption.SPNPort = this.spnport;
					sessionOption.UseUtf16 = this.useutf16;
					sessionOption.SkipCNCheck = this.skipcncheck;
					sessionOption.SkipCACheck = this.skipcacheck;
					sessionOption.OperationTimeout = this.operationtimeout;
					sessionOption.SkipRevocationCheck = this.skiprevocationcheck;
					sessionOption.ProxyAccessType = this._proxyacesstype;
					sessionOption.ProxyAuthentication = this.proxyauthentication;
					if (this.noencryption)
					{
						sessionOption.UseEncryption = false;
					}
					if (this._proxycredential != null)
					{
						NetworkCredential networkCredential = this._proxycredential.GetNetworkCredential();
						sessionOption.ProxyCredential = networkCredential;
					}
					base.WriteObject(sessionOption);
					return;
				}
				else
				{
					InvalidOperationException invalidOperationException1 = new InvalidOperationException(wSManHelper.GetResourceMsgFromResourcetext("NewWSManSessionOptionAuth"));
					ErrorRecord errorRecord1 = new ErrorRecord(invalidOperationException1, "InvalidOperationException", ErrorCategory.InvalidOperation, null);
					base.WriteError(errorRecord1);
					return;
				}
			}
		}