public CimActivityImplementationContext(ActivityImplementationContext activityImplementationContext, string computerName, PSCredential credential, string certificateThumbprint, AuthenticationMechanism? authenticationMechanism, bool useSsl, uint port, PSSessionOption sessionOption, CimSession session, CimSessionOptions cimSessionOptions, string moduleDefinition, Uri resourceUri)
		{
			if (activityImplementationContext != null)
			{
				base.PowerShellInstance = activityImplementationContext.PowerShellInstance;
				this.ResourceUri = resourceUri;
				this.ComputerName = computerName;
				base.PSCredential = credential;
				base.PSCertificateThumbprint = certificateThumbprint;
				base.PSAuthentication = authenticationMechanism;
				base.PSUseSsl = new bool?(useSsl);
				base.PSPort = new uint?(port);
				base.PSSessionOption = sessionOption;
				this.Session = session;
				this.SessionOptions = cimSessionOptions;
				if (moduleDefinition != null)
				{
					CimActivityImplementationContext._moduleScriptBlock = ScriptBlock.Create(moduleDefinition);
					this._moduleDefinition = moduleDefinition;
				}
				return;
			}
			else
			{
				throw new ArgumentNullException("activityImplementationContext");
			}
		}
 public virtual void SetSessionOptions(PSSessionOption options)
 {
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     if (options.Culture != null)
     {
         this.Culture = options.Culture;
     }
     if (options.UICulture != null)
     {
         this.UICulture = options.UICulture;
     }
     this.openTimeout = this.TimeSpanToTimeOutMs(options.OpenTimeout);
     this.cancelTimeout = this.TimeSpanToTimeOutMs(options.CancelTimeout);
     this.operationTimeout = this.TimeSpanToTimeOutMs(options.OperationTimeout);
     this.idleTimeout = ((options.IdleTimeout.TotalMilliseconds >= -1.0) && (options.IdleTimeout.TotalMilliseconds < 2147483647.0)) ? ((int) options.IdleTimeout.TotalMilliseconds) : 0x7fffffff;
 }
 protected override void BeginProcessing()
 {
     PSSessionOption sendToPipeline = new PSSessionOption {
         ProxyAccessType = this.ProxyAccessType,
         ProxyAuthentication = this.ProxyAuthentication,
         ProxyCredential = this.ProxyCredential,
         SkipCACheck = (bool) this.SkipCACheck,
         SkipCNCheck = (bool) this.SkipCNCheck,
         SkipRevocationCheck = (bool) this.SkipRevocationCheck
     };
     if (this.operationtimeout.HasValue)
     {
         sendToPipeline.OperationTimeout = TimeSpan.FromMilliseconds((double) this.operationtimeout.Value);
     }
     sendToPipeline.NoEncryption = (bool) this.NoEncryption;
     sendToPipeline.UseUTF16 = (bool) this.UseUTF16;
     sendToPipeline.IncludePortInSPN = (bool) this.IncludePortInSPN;
     if (this.maximumRedirection.HasValue)
     {
         sendToPipeline.MaximumConnectionRedirectionCount = this.MaximumRedirection;
     }
     sendToPipeline.NoCompression = this.NoCompression.IsPresent;
     sendToPipeline.NoMachineProfile = this.NoMachineProfile.IsPresent;
     sendToPipeline.MaximumReceivedDataSizePerCommand = this.maxRecvdDataSizePerCommand;
     sendToPipeline.MaximumReceivedObjectSize = this.maxRecvdObjectSize;
     if (this.Culture != null)
     {
         sendToPipeline.Culture = this.Culture;
     }
     if (this.UICulture != null)
     {
         sendToPipeline.UICulture = this.UICulture;
     }
     if (this.openTimeout.HasValue)
     {
         sendToPipeline.OpenTimeout = TimeSpan.FromMilliseconds((double) this.openTimeout.Value);
     }
     if (this.cancelTimeout.HasValue)
     {
         sendToPipeline.CancelTimeout = TimeSpan.FromMilliseconds((double) this.cancelTimeout.Value);
     }
     if (this.idleTimeout.HasValue)
     {
         sendToPipeline.IdleTimeout = TimeSpan.FromMilliseconds((double) this.idleTimeout.Value);
     }
     sendToPipeline.OutputBufferingMode = this.outputBufferingMode;
     if (this.ApplicationArguments != null)
     {
         sendToPipeline.ApplicationArguments = this.ApplicationArguments;
     }
     base.WriteObject(sendToPipeline);
 }
        /// <summary>
        /// Performs initialization of cmdlet execution.
        /// </summary>
        protected override void BeginProcessing()
        {
            PSSessionOption result = new PSSessionOption();
            // Begin: WSMan specific options
            result.ProxyAccessType = this.ProxyAccessType;
            result.ProxyAuthentication = this.ProxyAuthentication;
            result.ProxyCredential = this.ProxyCredential;
            result.SkipCACheck = this.SkipCACheck;
            result.SkipCNCheck = this.SkipCNCheck;
            result.SkipRevocationCheck = this.SkipRevocationCheck;
            if (_operationtimeout.HasValue)
            {
                result.OperationTimeout = TimeSpan.FromMilliseconds(_operationtimeout.Value);
            }
            result.NoEncryption = this.NoEncryption;
            result.UseUTF16 = this.UseUTF16;
            result.IncludePortInSPN = this.IncludePortInSPN;
            // End: WSMan specific options
            if (_maximumRedirection.HasValue)
            {
                result.MaximumConnectionRedirectionCount = this.MaximumRedirection;
            }

            result.NoCompression = this.NoCompression.IsPresent;
            result.NoMachineProfile = this.NoMachineProfile.IsPresent;

            result.MaximumReceivedDataSizePerCommand = _maxRecvdDataSizePerCommand;
            result.MaximumReceivedObjectSize = _maxRecvdObjectSize;

            if (this.Culture != null)
            {
                result.Culture = this.Culture;
            }
            if (this.UICulture != null)
            {
                result.UICulture = this.UICulture;
            }

            if (_openTimeout.HasValue)
            {
                result.OpenTimeout = TimeSpan.FromMilliseconds(_openTimeout.Value);
            }
            if (_cancelTimeout.HasValue)
            {
                result.CancelTimeout = TimeSpan.FromMilliseconds(_cancelTimeout.Value);
            }
            if (_idleTimeout.HasValue)
            {
                result.IdleTimeout = TimeSpan.FromMilliseconds(_idleTimeout.Value);
            }

            result.OutputBufferingMode = OutputBufferingMode;

            result.MaxConnectionRetryCount = MaxConnectionRetryCount;

            if (this.ApplicationArguments != null)
            {
                result.ApplicationArguments = this.ApplicationArguments;
            }

            this.WriteObject(result);
        }
Exemple #5
0
        internal static List <WSManConnectionInfo> GetConnectionInfo(string[] PSComputerName, string[] PSConnectionUri,
                                                                     string PSCertificateThumbprint, string PSConfigurationName,
                                                                     bool?PSUseSsl, uint?PSPort, string PSApplicationName,
                                                                     PSCredential PSCredential, AuthenticationMechanism PSAuthentication,
                                                                     bool PSAllowRedirection, System.Management.Automation.Remoting.PSSessionOption options)
        {
            List <WSManConnectionInfo> connections = new List <WSManConnectionInfo>();

            string[] machineList           = null;
            bool     connectByComputerName = false;

            // Connect by computername
            if ((!PSComputerName.IsNullOrEmpty()) && (PSConnectionUri.IsNullOrEmpty()))
            {
                machineList           = PSComputerName;
                connectByComputerName = true;
            }
            else if ((PSComputerName.IsNullOrEmpty()) && (!PSConnectionUri.IsNullOrEmpty()))
            {
                machineList = PSConnectionUri;
            }
            else
            {
                throw new ArgumentException(Resources.CannotSupplyUriAndComputername);
            }

            // Go through each machine in the list an update its properties
            foreach (string machine in machineList)
            {
                if (!string.IsNullOrEmpty(machine))
                {
                    WSManConnectionInfo connectionInfo = new WSManConnectionInfo();

                    if (PSPort.HasValue)
                    {
                        connectionInfo.Port = (int)PSPort.Value;
                    }

                    if (PSUseSsl.HasValue && (PSUseSsl.Value))
                    {
                        connectionInfo.Scheme = WSManConnectionInfo.HttpsScheme;
                    }

                    if (!String.IsNullOrEmpty(PSConfigurationName))
                    {
                        connectionInfo.ShellUri = PSConfigurationName;
                    }

                    if (!String.IsNullOrEmpty(PSApplicationName))
                    {
                        connectionInfo.AppName = PSApplicationName;
                    }

                    if (connectByComputerName)
                    {
                        connectionInfo.ComputerName = machine;
                    }
                    else
                    {
                        connectionInfo.ConnectionUri = (Uri)LanguagePrimitives.ConvertTo(machine, typeof(Uri), System.Globalization.CultureInfo.InvariantCulture);
                    }

                    if (PSCredential != null)
                    {
                        connectionInfo.Credential = PSCredential;
                    }

                    if (!String.IsNullOrEmpty(PSCertificateThumbprint))
                    {
                        connectionInfo.CertificateThumbprint = PSCertificateThumbprint;
                    }

                    if (PSAuthentication != AuthenticationMechanism.Default)
                    {
                        connectionInfo.AuthenticationMechanism = PSAuthentication;
                    }

                    connectionInfo.MaximumConnectionRedirectionCount = PSAllowRedirection ? DefaultMaximumConnectionRedirectionCount : 0;

                    if (options != null)
                    {
                        connectionInfo.SetSessionOptions(options);
                    }

                    connections.Add(connectionInfo);
                }
                else
                {
                    // add a null connection to account for "" or $null in PSComputerName parameter
                    connections.Add(null);
                }
            }

            return(connections);
        }
        /// <summary>
        /// Get a CIM session for the target computer
        /// </summary>
        /// <param name="computerName"></param>
        /// <param name="credential"></param>
        /// <param name="certificateThumbprint"></param>
        /// <param name="authenticationMechanism"></param>
        /// <param name="sessionOptions"></param>
        /// <param name="useSsl"></param>
        /// <param name="port"></param>
        /// <param name="pssessionOption"></param>
        /// <returns></returns>
        internal CimSession GetSession(string computerName, PSCredential credential, string certificateThumbprint, AuthenticationMechanism authenticationMechanism, CimSessionOptions sessionOptions, bool useSsl, uint port, PSSessionOption pssessionOption)
        {
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(computerName), "ComputerName is null in GetSession. GetSession should not be called in this case.");
            lock (SyncRoot)
            {
                SessionEntry newSessionEntry;

                if (availableSessions.ContainsKey(computerName))
                {

                    List<SessionEntry> sel = availableSessions[computerName];
                    if (sel.Count > 0)
                    {
                        for (int i = 0; i < sel.Count; i++)
                        {
                            SessionEntry se = sel[i];

                            // No session options specified or the object matches exactly...
                            if ((se.SessionOptions == null && sessionOptions == null) || CompareSessionOptions(se, sessionOptions, credential, certificateThumbprint, authenticationMechanism, useSsl, port, pssessionOption))
                            {
                                // Up the number of references to this session object...
                                se.AddReference();
                                return se.Session;
                            }
                        }
                    }
                }

                
                // Allocate a new session entry for this computer

                newSessionEntry = new SessionEntry(computerName, credential, certificateThumbprint, authenticationMechanism, sessionOptions, useSsl, port, pssessionOption);
                newSessionEntry.IterationsRemaining = MaxIterations;
                newSessionEntry.AddReference();
                if (! availableSessions.ContainsKey(computerName))
                {
                    availableSessions.Add(computerName, new List<SessionEntry>());
                }

                availableSessions[computerName].Add(newSessionEntry);

                // Return the session object
                return newSessionEntry.Session;
            }
        }
        private static bool CompareSessionOptions(SessionEntry sessionEntry, CimSessionOptions options2, PSCredential credential2, string certificateThumbprint, AuthenticationMechanism authenticationMechanism, bool useSsl, uint port, PSSessionOption pssessionOption)
        {
            if (!sessionEntry.SessionOptions.Timeout.Equals(options2.Timeout))
                return false;

            if (!string.Equals(sessionEntry.SessionOptions.Culture.ToString(), options2.Culture.ToString(), StringComparison.OrdinalIgnoreCase))
                return false;

            if (!string.Equals(sessionEntry.SessionOptions.UICulture.ToString(), options2.UICulture.ToString(), StringComparison.OrdinalIgnoreCase))
                return false;

            if (!string.Equals(sessionEntry.CertificateThumbprint, certificateThumbprint, StringComparison.OrdinalIgnoreCase))
                return false;

            if (sessionEntry.AuthenticationMechanism != authenticationMechanism)
                return false;

            if (!Workflow.WorkflowUtils.CompareCredential(sessionEntry.Credential, credential2))
                return false;

            if (sessionEntry.UseSsl != useSsl)
                return false;

            if (sessionEntry.Port != port)
                return false;


            // check PSSessionOption if present
            if (pssessionOption == null ^ sessionEntry.PSSessionOption == null)
            {
                return false;
            }

            if (pssessionOption != null && sessionEntry.PSSessionOption != null)
            {
                if (sessionEntry.PSSessionOption.ProxyAccessType != pssessionOption.ProxyAccessType)
                    return false;

                if (sessionEntry.PSSessionOption.ProxyAuthentication != pssessionOption.ProxyAuthentication)
                    return false;

                if (!Workflow.WorkflowUtils.CompareCredential(sessionEntry.PSSessionOption.ProxyCredential, pssessionOption.ProxyCredential))
                    return false;

                if (sessionEntry.PSSessionOption.SkipCACheck != pssessionOption.SkipCACheck)
                    return false;

                if (sessionEntry.PSSessionOption.SkipCNCheck != pssessionOption.SkipCNCheck)
                    return false;

                if (sessionEntry.PSSessionOption.SkipRevocationCheck != pssessionOption.SkipRevocationCheck)
                    return false;

                if (sessionEntry.PSSessionOption.NoEncryption != pssessionOption.NoEncryption)
                    return false;

                if (sessionEntry.PSSessionOption.UseUTF16 != pssessionOption.UseUTF16)
                    return false;
            }

            return true;
        }
 public SessionEntry(string computerName, PSCredential credential, string certificateThumbprint, AuthenticationMechanism authenticationMechanism, CimSessionOptions sessionOptions, bool useSsl, uint port, PSSessionOption pssessionOption)
 {
     SessionOptions = sessionOptions;
     _credential = credential;
     _certificateThumbprint = certificateThumbprint;
     _authenticationMechanism = authenticationMechanism;
     _useSsl = useSsl;
     _port = port;
     _psSessionOption = pssessionOption;
     Session = CimSession.Create(computerName, sessionOptions);
 }
 /// <summary>
 /// Create an instance of the CIM activity implementation class
 /// </summary>
 /// <param name="activityImplementationContext"></param>
 /// <param name="computerName"></param>
 /// <param name="credential"></param>
 /// <param name="certificateThumbprint"></param>
 /// <param name="authenticationMechanism"></param>
 /// <param name="useSsl"></param>
 /// <param name="port"></param>
 /// <param name="sessionOption"></param>
 /// <param name="session"></param>
 /// <param name="cimSessionOptions"></param>
 /// <param name="moduleDefinition"></param>
 /// <param name="resourceUri"></param>
 public CimActivityImplementationContext(
     ActivityImplementationContext activityImplementationContext,
     string computerName, 
     PSCredential credential, 
     string certificateThumbprint, 
     AuthenticationMechanism? authenticationMechanism, 
     bool useSsl, 
     uint port, 
     PSSessionOption sessionOption, 
     CimSession session, 
     CimSessionOptions cimSessionOptions, 
     string moduleDefinition,
     Uri resourceUri)
 {
     if (activityImplementationContext == null)
     {
         throw new ArgumentNullException("activityImplementationContext");
     }
     this.PowerShellInstance = activityImplementationContext.PowerShellInstance;
     ResourceUri = resourceUri;
     ComputerName = computerName;
     PSCredential = credential;
     PSCertificateThumbprint = certificateThumbprint;
     PSAuthentication = authenticationMechanism;
     PSUseSsl = useSsl;
     PSPort = port;
     PSSessionOption = sessionOption;
     Session = session;
     SessionOptions = cimSessionOptions;
     if (moduleDefinition != null)
     {
         // Creating a script block forces the string into the compiled script cache so we
         // don't need to reparse it at execution time. Locking the static _moduleDefinition is not
         // required since the operation is idempotent.
         _moduleScriptBlock = ScriptBlock.Create(moduleDefinition);
         _moduleDefinition = moduleDefinition;
     }
 }
Exemple #10
0
		private static bool CompareSessionOptions(CimConnectionManager.SessionEntry sessionEntry, CimSessionOptions options2, PSCredential credential2, string certificateThumbprint, AuthenticationMechanism authenticationMechanism, bool useSsl, uint port, PSSessionOption pssessionOption)
		{
			TimeSpan timeout = sessionEntry.SessionOptions.Timeout;
			if (timeout.Equals(options2.Timeout))
			{
				if (string.Equals(sessionEntry.SessionOptions.Culture.ToString(), options2.Culture.ToString(), StringComparison.OrdinalIgnoreCase))
				{
					if (string.Equals(sessionEntry.SessionOptions.UICulture.ToString(), options2.UICulture.ToString(), StringComparison.OrdinalIgnoreCase))
					{
						if (string.Equals(sessionEntry.CertificateThumbprint, certificateThumbprint, StringComparison.OrdinalIgnoreCase))
						{
							if (sessionEntry.AuthenticationMechanism == authenticationMechanism)
							{
								if (WorkflowUtils.CompareCredential(sessionEntry.Credential, credential2))
								{
									if (sessionEntry.UseSsl == useSsl)
									{
										if (sessionEntry.Port == port)
										{
											if (!(pssessionOption == null ^ sessionEntry.PSSessionOption == null))
											{
												if (pssessionOption != null && sessionEntry.PSSessionOption != null)
												{
													if (sessionEntry.PSSessionOption.ProxyAccessType == pssessionOption.ProxyAccessType)
													{
														if (sessionEntry.PSSessionOption.ProxyAuthentication == pssessionOption.ProxyAuthentication)
														{
															if (WorkflowUtils.CompareCredential(sessionEntry.PSSessionOption.ProxyCredential, pssessionOption.ProxyCredential))
															{
																if (sessionEntry.PSSessionOption.SkipCACheck == pssessionOption.SkipCACheck)
																{
																	if (sessionEntry.PSSessionOption.SkipCNCheck == pssessionOption.SkipCNCheck)
																	{
																		if (sessionEntry.PSSessionOption.SkipRevocationCheck == pssessionOption.SkipRevocationCheck)
																		{
																			if (sessionEntry.PSSessionOption.NoEncryption == pssessionOption.NoEncryption)
																			{
																				if (sessionEntry.PSSessionOption.UseUTF16 != pssessionOption.UseUTF16)
																				{
																					return false;
																				}
																			}
																			else
																			{
																				return false;
																			}
																		}
																		else
																		{
																			return false;
																		}
																	}
																	else
																	{
																		return false;
																	}
																}
																else
																{
																	return false;
																}
															}
															else
															{
																return false;
															}
														}
														else
														{
															return false;
														}
													}
													else
													{
														return false;
													}
												}
												return true;
											}
											else
											{
												return false;
											}
										}
										else
										{
											return false;
										}
									}
									else
									{
										return false;
									}
								}
								else
								{
									return false;
								}
							}
							else
							{
								return false;
							}
						}
						else
						{
							return false;
						}
					}
					else
					{
						return false;
					}
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
Exemple #11
0
		internal CimSession GetSession(string computerName, PSCredential credential, string certificateThumbprint, AuthenticationMechanism authenticationMechanism, CimSessionOptions sessionOptions, bool useSsl, uint port, PSSessionOption pssessionOption)
		{
			CimSession session;
			lock (this.SyncRoot)
			{
				if (this.availableSessions.ContainsKey(computerName))
				{
					List<CimConnectionManager.SessionEntry> item = this.availableSessions[computerName];
					if (item.Count > 0)
					{
						int num = 0;
						while (num < item.Count)
						{
							CimConnectionManager.SessionEntry sessionEntry = item[num];
							if ((sessionEntry.SessionOptions != null || sessionOptions != null) && !CimConnectionManager.CompareSessionOptions(sessionEntry, sessionOptions, credential, certificateThumbprint, authenticationMechanism, useSsl, port, pssessionOption))
							{
								num++;
							}
							else
							{
								sessionEntry.AddReference();
								session = sessionEntry.Session;
								return session;
							}
						}
					}
				}
				CimConnectionManager.SessionEntry sessionEntry1 = new CimConnectionManager.SessionEntry(computerName, credential, certificateThumbprint, authenticationMechanism, sessionOptions, useSsl, port, pssessionOption);
				sessionEntry1.IterationsRemaining = 6;
				sessionEntry1.AddReference();
				if (!this.availableSessions.ContainsKey(computerName))
				{
					this.availableSessions.Add(computerName, new List<CimConnectionManager.SessionEntry>());
				}
				this.availableSessions[computerName].Add(sessionEntry1);
				session = sessionEntry1.Session;
			}
			return session;
		}
Exemple #12
0
		internal static List<WSManConnectionInfo> GetConnectionInfo(string[] PSComputerName, string[] PSConnectionUri, string PSCertificateThumbprint, string PSConfigurationName, bool? PSUseSsl, int? PSPort, string PSApplicationName, PSCredential PSCredential, AuthenticationMechanism PSAuthentication, bool PSAllowRedirection, PSSessionOption options)
		{
			int defaultMaximumConnectionRedirectionCount;
			List<WSManConnectionInfo> wSManConnectionInfos = new List<WSManConnectionInfo>();
			string[] pSConnectionUri = null;
			bool flag = false;
			if (PSComputerName.IsNullOrEmpty() || !PSConnectionUri.IsNullOrEmpty())
			{
				if (!PSComputerName.IsNullOrEmpty() || PSConnectionUri.IsNullOrEmpty())
				{
					throw new ArgumentException(Resources.CannotSupplyUriAndComputername);
				}
				else
				{
					pSConnectionUri = PSConnectionUri;
				}
			}
			else
			{
				pSConnectionUri = PSComputerName;
				flag = true;
			}
			string[] strArrays = pSConnectionUri;
			for (int i = 0; i < (int)strArrays.Length; i++)
			{
				string str = strArrays[i];
				if (string.IsNullOrEmpty(str))
				{
					wSManConnectionInfos.Add(null);
				}
				else
				{
					WSManConnectionInfo wSManConnectionInfo = new WSManConnectionInfo();
					if (PSPort.HasValue)
					{
						wSManConnectionInfo.Port = PSPort.Value;
					}
					if (PSUseSsl.HasValue && PSUseSsl.Value)
					{
						wSManConnectionInfo.Scheme = "https";
					}
					if (!string.IsNullOrEmpty(PSConfigurationName))
					{
						wSManConnectionInfo.ShellUri = PSConfigurationName;
					}
					if (!string.IsNullOrEmpty(PSApplicationName))
					{
						wSManConnectionInfo.AppName = PSApplicationName;
					}
					if (!flag)
					{
						wSManConnectionInfo.ConnectionUri = (Uri)LanguagePrimitives.ConvertTo(str, typeof(Uri), CultureInfo.InvariantCulture);
					}
					else
					{
						wSManConnectionInfo.ComputerName = str;
					}
					if (PSCredential != null)
					{
						wSManConnectionInfo.Credential = PSCredential;
					}
					if (!string.IsNullOrEmpty(PSCertificateThumbprint))
					{
						wSManConnectionInfo.CertificateThumbprint = PSCertificateThumbprint;
					}
					if (PSAuthentication != AuthenticationMechanism.Default)
					{
						wSManConnectionInfo.AuthenticationMechanism = PSAuthentication;
					}
					WSManConnectionInfo wSManConnectionInfo1 = wSManConnectionInfo;
					if (PSAllowRedirection)
					{
						defaultMaximumConnectionRedirectionCount = ActivityUtils.DefaultMaximumConnectionRedirectionCount;
					}
					else
					{
						defaultMaximumConnectionRedirectionCount = 0;
					}
					wSManConnectionInfo1.MaximumConnectionRedirectionCount = defaultMaximumConnectionRedirectionCount;
					if (options != null)
					{
						wSManConnectionInfo.SetSessionOptions(options);
					}
					wSManConnectionInfos.Add(wSManConnectionInfo);
				}
			}
			return wSManConnectionInfos;
		}