Пример #1
0
        public WSManSessionOptions(WSManSessionOptions optionsToClone) : base(optionsToClone)
        {
            DestinationOptionsHandle handle;

            DestinationOptionsMethods.Clone(base.DestinationOptionsHandle, out handle);
            DestinationOptionsHandleOnDemand = handle;
        }
Пример #2
0
		protected CimSessionProxy CreateSessionProxy (string computerName, CimBaseCommand cmdlet)
		{
			CimSessionProxy cimSessionProxy = null;
			if (!string.IsNullOrEmpty (computerName) && !computerName.Equals ("localhost", StringComparison.OrdinalIgnoreCase)) {
				/* Set on the fly credentials */
				System.Management.Automation.PSCredential credential = GetOnTheFlyCredentials(cmdlet);
				if (credential == null) 
					cimSessionProxy = base.CreateCimSessionProxy (computerName);
				else {
					CimSessionOptions options = new WSManSessionOptions ();
					options.AddDestinationCredentials (cmdlet.CreateCimCredentials (credential, PasswordAuthenticationMechanism.Default, "Get-CimIstance", "Authentication"));
					cimSessionProxy = base.CreateCimSessionProxy (computerName, options);
				}
			} 
			else {
				cimSessionProxy = base.CreateCimSessionProxy (computerName);
			}
			this.SetSessionProxyProperties(ref cimSessionProxy, cmdlet);
			return cimSessionProxy;
		}
Пример #3
0
		private CimSessionProxy CreateSessionProxy(string computerName, GetCimClassCommand cmdlet)
		{
			CimSessionProxy cimSessionProxyGetCimClass = null;
			if (!string.IsNullOrEmpty (computerName) && !computerName.Equals ("localhost", StringComparison.OrdinalIgnoreCase)) {
				/* Set on the fly credentials */
				System.Management.Automation.PSCredential credential = GetOnTheFlyCredentials(cmdlet);
				if (credential == null) 
					cimSessionProxyGetCimClass = new CimSessionProxyGetCimClass(computerName);
				else {
					CimSessionOptions options = new WSManSessionOptions ();
					options.AddDestinationCredentials (cmdlet.CreateCimCredentials (credential, PasswordAuthenticationMechanism.Default, "Get-CimClass", "Authentication"));
					cimSessionProxyGetCimClass = new CimSessionProxyGetCimClass (computerName, options);
				}
			}
			else {
				cimSessionProxyGetCimClass = new CimSessionProxyGetCimClass(computerName);
			}
			base.SubscribeEventAndAddProxytoCache(cimSessionProxyGetCimClass);
			this.SetSessionProxyProperties(ref cimSessionProxyGetCimClass, cmdlet);
			return cimSessionProxyGetCimClass;
		}
Пример #4
0
        }//End EndProcessing()
        #endregion

        #region helper methods

        /// <summary>
        /// Build a CimSessionOptions, used to create CimSession
        /// </summary>
        /// <returns>Null means no prefer CimSessionOptions</returns>
        internal void BuildSessionOptions(out CimSessionOptions outputOptions, out CimCredential outputCredential)
        {
            DebugHelper.WriteLogEx();

            CimSessionOptions options = null;
            if (this.SessionOption != null)
            {
                // clone the sessionOption object
                if (this.SessionOption is WSManSessionOptions)
                {
                    options = new WSManSessionOptions(this.sessionOption as WSManSessionOptions);
                }
                else
                {
                    options = new DComSessionOptions(this.sessionOption as DComSessionOptions);
                }
            }
            outputOptions = null;
            outputCredential = null;
            if (options != null)
            {
                DComSessionOptions dcomOptions = (options as DComSessionOptions);
                if (dcomOptions != null)
                {
                    bool conflict = false;
                    string parameterName = string.Empty;
                    if (this.CertificateThumbprint != null)
                    {
                        conflict = true;
                        parameterName = @"CertificateThumbprint";
                    }
                    if (portSet)
                    {
                        conflict = true;
                        parameterName = @"Port";
                    }
                    if (conflict)
                    {
                        ThrowConflictParameterWasSet(@"New-CimSession", parameterName, @"DComSessionOptions");
                        return;
                    }
                }
            }
            if (portSet || (this.CertificateThumbprint != null))
            {
                WSManSessionOptions wsmanOptions = (options == null) ? new WSManSessionOptions() : options as WSManSessionOptions;
                if (portSet)
                {
                    wsmanOptions.DestinationPort = this.Port;
                    portSet = false;
                }
                if (this.CertificateThumbprint != null)
                {
                    CimCredential credentials = new CimCredential(CertificateAuthenticationMechanism.Default, this.CertificateThumbprint);
                    wsmanOptions.AddDestinationCredentials(credentials);
                }
                options = wsmanOptions;
            }
            if (this.operationTimeoutSet)
            {
                if (options != null)
                {
                    options.Timeout = TimeSpan.FromSeconds((double)this.OperationTimeoutSec);
                }
            }
            if (this.authenticationSet || (this.credential != null))
            {
                PasswordAuthenticationMechanism authentication = this.authenticationSet ? this.Authentication : PasswordAuthenticationMechanism.Default;
                if (this.authenticationSet)
                {
                    this.authenticationSet = false;
                }
                CimCredential credentials = CreateCimCredentials(this.Credential, authentication, @"New-CimSession", @"Authentication");
                if (credentials == null)
                {
                    return;
                }

                DebugHelper.WriteLog("Credentials: {0}", 1, credentials);
                outputCredential = credentials;
                if (options != null)
                {
                    DebugHelper.WriteLog("Add credentials to option: {0}", 1, options);
                    options.AddDestinationCredentials(credentials);
                }
            }
            DebugHelper.WriteLogEx("Set outputOptions: {0}", 1, outputOptions);
            outputOptions = options;
        }
Пример #5
0
 /// <summary>
 /// Instantiates a deep copy of <paramref name="optionsToClone"/>
 /// </summary>
 /// <param name="optionsToClone">options to clone</param>
 /// <exception cref="ArgumentNullException">Thrown when <paramref name="optionsToClone"/> is <c>null</c></exception>
 public WSManSessionOptions(WSManSessionOptions optionsToClone)
     : base(optionsToClone)
 {
 }
Пример #6
0
 /// <summary>
 /// Create WSMANSessionOptions
 /// </summary>
 /// <returns></returns>
 internal WSManSessionOptions CreateWSMANSessionOptions()
 {
     WSManSessionOptions wsmanoptions = new WSManSessionOptions();
     if (this.noEncryptionSet)
     {
         wsmanoptions.NoEncryption = true;
         this.noEncryptionSet = false;
     }
     else
     {
         wsmanoptions.NoEncryption = false;
     }
     if (this.skipCACheckSet)
     {
         wsmanoptions.CertCACheck = false;
         this.skipCACheckSet = false;
     }
     else
     {
         wsmanoptions.CertCACheck = true;
     }
     if (this.skipCNCheckSet)
     {
         wsmanoptions.CertCNCheck = false;
         this.skipCNCheckSet = false;
     }
     else
     {
         wsmanoptions.CertCNCheck = true;
     }
     if (this.skipRevocationCheckSet)
     {
         wsmanoptions.CertRevocationCheck = false;
         this.skipRevocationCheckSet = false;
     }
     else
     {
         wsmanoptions.CertRevocationCheck = true;
     }
     if (this.encodeportinserviceprincipalnameSet)
     {
         wsmanoptions.EncodePortInServicePrincipalName = this.EncodePortInServicePrincipalName;
         this.encodeportinserviceprincipalnameSet = false;
     }
     else
     {
         wsmanoptions.EncodePortInServicePrincipalName = false;
     }
     if (this.encodingSet)
     {
         wsmanoptions.PacketEncoding = this.Encoding;
     }
     else
     {
         wsmanoptions.PacketEncoding = PacketEncoding.Utf8;
     }
     if (this.HttpPrefix != null)
     {
         wsmanoptions.HttpUrlPrefix = this.HttpPrefix;
     }
     if (this.maxenvelopesizekbSet)
     {
         wsmanoptions.MaxEnvelopeSize = this.MaxEnvelopeSizeKB;
     }
     else
     {
         wsmanoptions.MaxEnvelopeSize = 0;
     }
     if (!String.IsNullOrWhiteSpace(this.ProxyCertificateThumbprint))
     {
         CimCredential credentials = new CimCredential(CertificateAuthenticationMechanism.Default, this.ProxyCertificateThumbprint);
         wsmanoptions.AddProxyCredentials(credentials);
     }
     if (this.proxyauthenticationSet)
     {
         this.proxyauthenticationSet = false;
         DebugHelper.WriteLogEx("create credential", 1);
         CimCredential credentials = CreateCimCredentials(this.ProxyCredential, this.ProxyAuthentication, @"New-CimSessionOption", @"ProxyAuthentication");
         if (credentials != null)
         {
             try
             {
                 DebugHelper.WriteLogEx("Add proxy credential", 1);
                 wsmanoptions.AddProxyCredentials(credentials);
             }
             catch (Exception ex)
             {
                 DebugHelper.WriteLogEx(ex.ToString(), 1);
                 throw ex;
             }
         }
     }
     if (this.proxytypeSet)
     {
         wsmanoptions.ProxyType = this.ProxyType;
         this.proxytypeSet = false;
     }
     else
     {
         wsmanoptions.ProxyType = Options.ProxyType.WinHttp;
     }
     if (this.usesslSet)
     {
         wsmanoptions.UseSsl = this.UseSsl;
         this.usesslSet = false;
     }
     else
     {
         wsmanoptions.UseSsl = false;
     }
     wsmanoptions.DestinationPort = 0;
     return wsmanoptions;
 }
Пример #7
0
        /// <summary>
        /// <para>
        /// Create <see cref="CimSessionOptions"/> based on the given computerName,
        /// timeout and credential
        /// </para>
        /// </summary>
        /// <param name="computerName"></param>
        /// <param name="timeout"></param>
        /// <param name="credential"></param>
        /// <returns></returns>
        internal static CimSessionOptions CreateCimSessionOption(string computerName,
            UInt32 timeout, CimCredential credential)
        {
            DebugHelper.WriteLogEx();

            CimSessionOptions option;
            if (ConstValue.IsDefaultComputerName(computerName))
            {
                DebugHelper.WriteLog("<<<<<<<<<< Use protocol DCOM  {0}", 1, computerName);
                option = new DComSessionOptions();
            }
            else
            {
                DebugHelper.WriteLog("<<<<<<<<<< Use protocol WSMAN {0}", 1, computerName);
                option = new WSManSessionOptions();
            }
            if (timeout != 0)
            {
                option.Timeout = TimeSpan.FromSeconds((double)timeout);
            }
            if (credential != null)
            {
                option.AddDestinationCredentials(credential);
            }
            DebugHelper.WriteLogEx("returned option :{0}.", 1, option);
            return option;
        }
Пример #8
0
		internal void BuildSessionOptions (out CimSessionOptions outputOptions, out CimCredential outputCredential)
		{
			WSManSessionOptions wSManSessionOption;
			PasswordAuthenticationMechanism authentication;
			DebugHelper.WriteLogEx ();
			CimSessionOptions dComSessionOption = null;

			/* Requires Authentication for Remote Host */
			if (this.credential == null && ComputerName != null) {
				bool requiredAuth = false;
				foreach(var c in ComputerName)
				{
					if (c != null && !c.Equals("localhost", StringComparison.OrdinalIgnoreCase))
					{
						requiredAuth = true;
						break;
					}
				}
				if (requiredAuth)
				{
					TrySetCredentials();
				}
			}

			if (this.SessionOption != null) {
				if (this.SessionOption as WSManSessionOptions == null) {
					dComSessionOption = new DComSessionOptions (this.sessionOption as DComSessionOptions);
				} else {
					dComSessionOption = new WSManSessionOptions (this.sessionOption as WSManSessionOptions);
				}
			}
			outputOptions = null;
			outputCredential = null;
			if (dComSessionOption != null) {
				DComSessionOptions dComSessionOption1 = dComSessionOption as DComSessionOptions;
				if (dComSessionOption1 != null) {
					bool flag = false;
					string empty = string.Empty;
					if (this.CertificateThumbprint != null) {
						flag = true;
						empty = "CertificateThumbprint";
					}
					if (this.portSet) {
						flag = true;
						empty = "Port";
					}
					if (flag) {
						base.ThrowConflictParameterWasSet ("New-CimSession", empty, "DComSessionOptions");
						return;
					}
				}
			}

			if (this.portSet || this.CertificateThumbprint != null) {
				if (dComSessionOption == null) {
					wSManSessionOption = new WSManSessionOptions ();
				} else {
					wSManSessionOption = dComSessionOption as WSManSessionOptions;
				}
				WSManSessionOptions port = wSManSessionOption;
				if (this.portSet) {
					port.DestinationPort = this.Port;
					this.portSet = false;
				}
				if (this.CertificateThumbprint != null) {
					CimCredential cimCredential = new CimCredential (CertificateAuthenticationMechanism.Default, this.CertificateThumbprint);
					port.AddDestinationCredentials (cimCredential);
				}
				dComSessionOption = port;
			}
			if (this.operationTimeoutSet && dComSessionOption != null) {
				dComSessionOption.Timeout = TimeSpan.FromSeconds ((double)((float)this.OperationTimeoutSec));
			}

			if (this.authenticationSet || this.credential != null) {
				if (this.authenticationSet) {
					authentication = this.Authentication;
				} else {
					authentication = PasswordAuthenticationMechanism.Default;
				}
				PasswordAuthenticationMechanism passwordAuthenticationMechanism = authentication;
				if (this.authenticationSet) {
					this.authenticationSet = false;
				}
				CimCredential cimCredential1 = base.CreateCimCredentials (this.Credential, passwordAuthenticationMechanism, "New-CimSession", "Authentication");
				if (cimCredential1 != null) {
					object[] objArray = new object[1];
					objArray [0] = cimCredential1;
					DebugHelper.WriteLog ("Credentials: {0}", 1, objArray);
					outputCredential = cimCredential1;
					if (dComSessionOption != null) {
						object[] objArray1 = new object[1];
						objArray1 [0] = dComSessionOption;
						DebugHelper.WriteLog ("Add credentials to option: {0}", 1, objArray1);
						dComSessionOption.AddDestinationCredentials (cimCredential1);
					}
				} else {
					return;
				}
			}
		
			object[] objArray2 = new object[1];
			objArray2[0] = outputOptions;
			DebugHelper.WriteLogEx("Set outputOptions: {0}", 1, objArray2);
			outputOptions = dComSessionOption;
		}
Пример #9
0
		public WSManSessionOptions(WSManSessionOptions optionsToClone) : base(optionsToClone)
		{
			DestinationOptionsHandle handle;
			DestinationOptionsMethods.Clone (base.DestinationOptionsHandle, out handle);
			DestinationOptionsHandleOnDemand = handle;
		}
Пример #10
0
		protected override List<ActivityImplementationContext> GetImplementation(NativeActivityContext context)
		{
			string empty;
			string str;
			string empty1;
			string str1;
			typeof(GenericCimCmdletActivity).IsAssignableFrom(base.GetType());
			string[] strArrays = this.PSComputerName.Get(context);
			CimSession[] cimSessionArray = this.CimSession.Get(context);
			Uri uri = null;
			if (this.ResourceUri != null)
			{
				uri = this.ResourceUri.Get(context);
			}
			List<ActivityImplementationContext> activityImplementationContexts = new List<ActivityImplementationContext>();
			if (strArrays == null || (int)strArrays.Length <= 0)
			{
				ActivityImplementationContext powerShell = this.GetPowerShell(context);
				CimActivityImplementationContext cimActivityImplementationContext = new CimActivityImplementationContext(powerShell, null, null, null, new AuthenticationMechanism?(AuthenticationMechanism.Default), false, 0, null, null, null, this.ModuleDefinition, uri);
				activityImplementationContexts.Add(cimActivityImplementationContext);
			}
			else
			{
				WSManSessionOptions wSManSessionOption = new WSManSessionOptions();
				uint? nullable = base.PSActionRunningTimeoutSec.Get(context);
				if (nullable.HasValue)
				{
					wSManSessionOption.Timeout = TimeSpan.FromSeconds((double)((float)nullable.Value));
				}
				bool? nullable1 = this.PSUseSsl.Get(context);
				bool value = false;
				if (nullable1.HasValue)
				{
					wSManSessionOption.UseSsl = nullable1.Value;
					value = nullable1.Value;
				}
				uint? nullable2 = this.PSPort.Get(context);
				uint num = 0;
				if (nullable2.HasValue)
				{
					wSManSessionOption.DestinationPort = nullable2.Value;
					num = nullable2.Value;
				}
				PSSessionOption pSSessionOption = this.PSSessionOption.Get(context);
				if (pSSessionOption != null)
				{
					wSManSessionOption.NoEncryption = pSSessionOption.NoEncryption;
					wSManSessionOption.CertCACheck = pSSessionOption.SkipCACheck;
					wSManSessionOption.CertCNCheck = pSSessionOption.SkipCNCheck;
					wSManSessionOption.CertRevocationCheck = pSSessionOption.SkipRevocationCheck;
					if (pSSessionOption.UseUTF16)
					{
						wSManSessionOption.PacketEncoding = PacketEncoding.Utf16;
					}
					if (pSSessionOption.Culture != null)
					{
						wSManSessionOption.Culture = pSSessionOption.Culture;
					}
					if (pSSessionOption.UICulture != null)
					{
						wSManSessionOption.UICulture = pSSessionOption.UICulture;
					}
					if (pSSessionOption.ProxyCredential != null)
					{
						char[] chrArray = new char[1];
						chrArray[0] = '\\';
						string[] strArrays1 = pSSessionOption.ProxyCredential.UserName.Split(chrArray);
						if ((int)strArrays1.Length >= 2)
						{
							empty = strArrays1[0];
							str = strArrays1[1];
						}
						else
						{
							empty = string.Empty;
							str = strArrays1[0];
						}
						wSManSessionOption.AddProxyCredentials(new CimCredential(PSGeneratedCIMActivity.ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(pSSessionOption.ProxyAuthentication), empty, str, pSSessionOption.ProxyCredential.Password));
					}
					ProxyAccessType proxyAccessType = pSSessionOption.ProxyAccessType;
					if (proxyAccessType == ProxyAccessType.IEConfig)
					{
						wSManSessionOption.ProxyType = ProxyType.InternetExplorer;
						goto Label0;
					}
					else if (proxyAccessType == ProxyAccessType.WinHttpConfig)
					{
						wSManSessionOption.ProxyType = ProxyType.WinHttp;
						goto Label0;
					}
					else if (proxyAccessType == (ProxyAccessType.IEConfig | ProxyAccessType.WinHttpConfig))
					{
						goto Label0;
					}
					else if (proxyAccessType == ProxyAccessType.AutoDetect)
					{
						wSManSessionOption.ProxyType = ProxyType.Auto;
						goto Label0;
					}
				}
			Label0:
				PSCredential pSCredential = this.PSCredential.Get(context);
				string str2 = this.PSCertificateThumbprint.Get(context);
				if (pSCredential == null || str2 == null)
				{
					PasswordAuthenticationMechanism cimPasswordAuthenticationMechanism = PasswordAuthenticationMechanism.Default;
					AuthenticationMechanism? nullable3 = this.PSAuthentication.Get(context);
					if (nullable3.HasValue)
					{
						cimPasswordAuthenticationMechanism = PSGeneratedCIMActivity.ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(nullable3.Value);
					}
					if (str2 != null)
					{
						wSManSessionOption.AddDestinationCredentials(new CimCredential(CertificateAuthenticationMechanism.Default, str2));
					}
					if (pSCredential != null)
					{
						char[] chrArray1 = new char[1];
						chrArray1[0] = '\\';
						string[] strArrays2 = pSCredential.UserName.Split(chrArray1);
						if ((int)strArrays2.Length >= 2)
						{
							empty1 = strArrays2[0];
							str1 = strArrays2[1];
						}
						else
						{
							empty1 = string.Empty;
							str1 = strArrays2[0];
						}
						wSManSessionOption.AddDestinationCredentials(new CimCredential(cimPasswordAuthenticationMechanism, empty1, str1, pSCredential.Password));
					}
					if (cimSessionArray == null || (int)cimSessionArray.Length <= 0)
					{
						string[] strArrays3 = strArrays;
						for (int i = 0; i < (int)strArrays3.Length; i++)
						{
							string str3 = strArrays3[i];
							ActivityImplementationContext activityImplementationContext = this.GetPowerShell(context);
							CimActivityImplementationContext cimActivityImplementationContext1 = new CimActivityImplementationContext(activityImplementationContext, str3, pSCredential, str2, nullable3, value, num, pSSessionOption, null, wSManSessionOption, this.ModuleDefinition, uri);
							activityImplementationContexts.Add(cimActivityImplementationContext1);
						}
					}
					else
					{
						CimSession[] cimSessionArray1 = cimSessionArray;
						for (int j = 0; j < (int)cimSessionArray1.Length; j++)
						{
							CimSession cimSession = cimSessionArray1[j];
							ActivityImplementationContext powerShell1 = this.GetPowerShell(context);
							CimActivityImplementationContext cimActivityImplementationContext2 = new CimActivityImplementationContext(powerShell1, cimSession.ComputerName, pSCredential, str2, nullable3, value, num, pSSessionOption, cimSession, wSManSessionOption, this.ModuleDefinition, uri);
							activityImplementationContexts.Add(cimActivityImplementationContext2);
						}
					}
				}
				else
				{
					throw new ArgumentException(Resources.CredentialParameterCannotBeSpecifiedWithPSCertificateThumbPrint);
				}
			}
			return activityImplementationContexts;
		}
        protected override List<ActivityImplementationContext> GetImplementation(NativeActivityContext context)
        {
            bool needRunspace = !typeof(GenericCimCmdletActivity).IsAssignableFrom(this.GetType());
            string[] computernames = PSComputerName.Get(context);
            CimSession[] sessions = this.CimSession.Get(context);
            Uri resourceUri = null;

            if (ResourceUri != null)
            {
                resourceUri = ResourceUri.Get(context);
            }

            List<ActivityImplementationContext> commands = new List<ActivityImplementationContext>();

            // Configure the remote connectivity options...
            if (computernames != null && computernames.Length > 0)
            {
                WSManSessionOptions sessionOptions = new WSManSessionOptions();

                // Set a timeout on the connection...
                uint? timeout = PSActionRunningTimeoutSec.Get(context);
                if (timeout.HasValue)
                {
                    sessionOptions.Timeout = TimeSpan.FromSeconds((double)(timeout.Value));
                }

                // See if we should use SSL or not...
                bool? useSsl = PSUseSsl.Get(context);
                bool sessionOptionUseSsl = false;

                if (useSsl.HasValue)
                {
                    sessionOptions.UseSsl = useSsl.Value;
                    sessionOptionUseSsl = useSsl.Value;
                }

                // Set the port to use
                uint? port = PSPort.Get(context);
                uint sessionOptionPort = 0;

                if (port.HasValue)
                {
                    sessionOptions.DestinationPort = port.Value;
                    sessionOptionPort = port.Value;
                }

                // Map over options from PSSessionConfig to WSManSessionOptions
                PSSessionOption pso = PSSessionOption.Get(context);
                if (pso != null)
                {
                    sessionOptions.NoEncryption = pso.NoEncryption;
                    sessionOptions.CertCACheck = pso.SkipCACheck;
                    sessionOptions.CertCNCheck = pso.SkipCNCheck;
                    sessionOptions.CertRevocationCheck = pso.SkipRevocationCheck;

                    if (pso.UseUTF16)
                        sessionOptions.PacketEncoding = PacketEncoding.Utf16;

                    if (pso.Culture != null)
                        sessionOptions.Culture = pso.Culture;

                    if (pso.UICulture != null)
                        sessionOptions.UICulture = pso.UICulture;

                    if (pso.ProxyCredential != null)
                    {   
                        string[] parts = pso.ProxyCredential.UserName.Split('\\');
                        string domain, userid;
                        if (parts.Length < 2)
                        {
                            domain = string.Empty;
                            userid = parts[0];
                        }
                        else
                        {
                            domain = parts[0];
                            userid = parts[1];
                        }

                        sessionOptions.AddProxyCredentials(
                            new CimCredential(ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(pso.ProxyAuthentication), 
                                domain, userid, pso.ProxyCredential.Password));
                    }

                    switch (pso.ProxyAccessType)
                    {
                        case ProxyAccessType.WinHttpConfig:
                            sessionOptions.ProxyType = ProxyType.WinHttp;
                            break;
                        case ProxyAccessType.AutoDetect:
                            sessionOptions.ProxyType = ProxyType.Auto;
                            break;

                        case ProxyAccessType.IEConfig:
                            sessionOptions.ProxyType = ProxyType.InternetExplorer;
                            break;
                    }
                }

                PSCredential pscreds = PSCredential.Get(context);
                string certificateThumbprint = PSCertificateThumbprint.Get(context);

                if (pscreds != null && certificateThumbprint != null)
                {
                    throw new ArgumentException(Resources.CredentialParameterCannotBeSpecifiedWithPSCertificateThumbPrint);
                }

                PasswordAuthenticationMechanism passwordAuthenticationMechanism = PasswordAuthenticationMechanism.Default;
                AuthenticationMechanism? authenticationMechanism = PSAuthentication.Get(context);
                
                if (authenticationMechanism.HasValue)
                    passwordAuthenticationMechanism = ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(authenticationMechanism.Value);

                
                if (certificateThumbprint != null)
                {
                    sessionOptions.AddDestinationCredentials(new CimCredential(CertificateAuthenticationMechanism.Default, certificateThumbprint));
                }

                if (pscreds != null)
                {
                    string[] parts = pscreds.UserName.Split('\\');
                    string domain, userid;
                    if (parts.Length < 2)
                    {
                        domain = string.Empty;
                        userid = parts[0];
                    }
                    else
                    {
                        domain = parts[0];
                        userid = parts[1];
                    }

                    sessionOptions.AddDestinationCredentials(new CimCredential(passwordAuthenticationMechanism, domain, userid, pscreds.Password));
                }

                // Create the PowerShell instance, and add the script to it.
                if (sessions != null && sessions.Length > 0)
                {
                    foreach (CimSession session in sessions)
                    {
                        ActivityImplementationContext configuredCommand = GetPowerShell(context);

                        CimActivityImplementationContext activityImplementationContext = 
                            new CimActivityImplementationContext(
                                configuredCommand, 
                                session.ComputerName, 
                                pscreds, 
                                certificateThumbprint, 
                                authenticationMechanism,
                                sessionOptionUseSsl, 
                                sessionOptionPort, 
                                pso, 
                                session, 
                                sessionOptions, 
                                ModuleDefinition, 
                                resourceUri);

                        commands.Add(activityImplementationContext);
                        //if (needRunspace)
                        //    GetRunspaceForCimCmdlet(context, activityImplementationContext);
                    }
                }
                else if (this.PSCommandName.Equals("CimCmdlets\\New-CimSession", StringComparison.OrdinalIgnoreCase))
                {
                    // NewCimSession activity is a special one as it creates the required sessions based on number of computers specified in one go.

                    ActivityImplementationContext baseContext = GetPowerShell(context);

                    CimActivityImplementationContext activityImplementationContext =
                        new CimActivityImplementationContext(baseContext,
                                                                null,  // ComputerName
                                                                pscreds,
                                                                certificateThumbprint,
                                                                authenticationMechanism,
                                                                sessionOptionUseSsl,
                                                                sessionOptionPort,
                                                                pso,
                                                                null, // session
                                                                sessionOptions,
                                                                ModuleDefinition,
                                                                resourceUri);

                    commands.Add(activityImplementationContext);
                }
                else
                {
                    foreach (string computer in computernames)
                    {
                        ActivityImplementationContext baseContext = GetPowerShell(context);

                        CimActivityImplementationContext activityImplementationContext =
                            new CimActivityImplementationContext(baseContext, 
                                                                 computer, 
                                                                 pscreds,
                                                                 certificateThumbprint,
                                                                 authenticationMechanism,
                                                                 sessionOptionUseSsl, 
                                                                 sessionOptionPort,
                                                                 pso, 
                                                                 null, // session
                                                                 sessionOptions, 
                                                                 ModuleDefinition, 
                                                                 resourceUri);

                        commands.Add(activityImplementationContext);
                    }
                }
            }
            // Configure the local invocation options
            else
            {
                // Create the PowerShell instance, and add the script to it.
                ActivityImplementationContext baseContext = GetPowerShell(context);
                CimActivityImplementationContext activityImplementationContext =
                    new CimActivityImplementationContext(baseContext, 
                                                         null,  // ComputerName
                                                         null,  // Credential
                                                         null,  // CertificateThumbprint
                                                         AuthenticationMechanism.Default,
                                                         false, // UseSsl
                                                         0,     // Port
                                                         null,  // PSSessionOption
                                                         null,  // Session
                                                         null,  // CimSessionOptions
                                                         ModuleDefinition,
                                                         resourceUri);

                commands.Add(activityImplementationContext);
            }

            return commands;
        }
Пример #12
0
		internal static CimSessionOptions CreateCimSessionOption(string computerName, uint timeout, CimCredential credential)
		{
			CimSessionOptions wSManSessionOption;
			DebugHelper.WriteLogEx();
			if (!ConstValue.IsDefaultComputerName(computerName))
			{
				object[] objArray = new object[1];
				objArray[0] = computerName;
				DebugHelper.WriteLog("<<<<<<<<<< Use protocol WSMAN {0}", 1, objArray);
				wSManSessionOption = new WSManSessionOptions();
			}
			else
			{
				object[] objArray1 = new object[1];
				objArray1[0] = computerName;
				DebugHelper.WriteLog("<<<<<<<<<< Use protocol DCOM  {0}", 1, objArray1);
				wSManSessionOption = new DComSessionOptions();
			}
			if (timeout != 0)
			{
				wSManSessionOption.Timeout = TimeSpan.FromSeconds((double)((float)timeout));
			}
			if (credential != null)
			{
				wSManSessionOption.AddDestinationCredentials(credential);
			}
			object[] objArray2 = new object[1];
			objArray2[0] = wSManSessionOption;
			DebugHelper.WriteLogEx("returned option :{0}.", 1, objArray2);
			return wSManSessionOption;
		}