Beispiel #1
0
 /// <summary>
 /// Loads and returns all workflow jobs based on provided filters.
 /// </summary>
 /// <param name="type">Represent which type of data needs to be used to apply filter.</param>
 /// <param name="filters">Filters represents the key value pair of conditions.</param>
 /// <returns>Returns the collection of workflow instances.</returns>
 internal IEnumerable<Job2> GetJobs(WorkflowFilterTypes type, Dictionary<string, object> filters)
 {
     Tracer.WriteMessage(Facility + "Getting workflow instances based on filters");
     return GetJobs(_wfJobTable.Values, type, filters);
 }
Beispiel #2
0
        private static bool SearchAllFilterTypes(PSWorkflowJob job, WorkflowFilterTypes type, string key, out object value)
        {
            PSWorkflowContext metadata = job.PSWorkflowInstance.PSWorkflowContext;
            value = null;
            if (metadata == null)
                return false;
            var searchTable = new Dictionary<WorkflowFilterTypes, Dictionary<string, object>>
                                  {
                                      {WorkflowFilterTypes.WorkflowSpecificParameters, metadata.WorkflowParameters},
                                      {WorkflowFilterTypes.JobMetadata, metadata.JobMetadata},
                                      {WorkflowFilterTypes.CommonParameters, metadata.PSWorkflowCommonParameters},
                                      {WorkflowFilterTypes.PrivateMetadata, metadata.PrivateMetadata}
                                  };
            foreach(var filter in searchTable.Keys)
            {
                object searchResult;
                if (!type.HasFlag(filter) || !SearchOneFilterType(searchTable[filter], key, out searchResult)) continue;
                value = searchResult;
                return true;
            }

            return false;

        }
Beispiel #3
0
        /// <summary>
        /// Loads and returns all workflow jobs based on provided filters.
        /// </summary>
        /// <param name="searchList">PSWorkflowJob search list</param>
        /// <param name="type">Represent which type of data needs to be used to apply filter.</param>
        /// <param name="filters">Filters represents the key value pair of conditions.</param>
        /// <returns>Returns the collection of workflow instances.</returns>
        internal IEnumerable<Job2> GetJobs(ICollection<PSWorkflowJob> searchList, WorkflowFilterTypes type, IDictionary<String, object> filters)
        {
            List<Job2> selectedJobs = new List<Job2>();
            var filters2 = new Dictionary<string, object>(filters, StringComparer.CurrentCultureIgnoreCase);

            // do a quick search on the basic v2 parameters
            List<Job2> narrowedList = WorkflowJobSourceAdapter.SearchJobsOnV2Parameters(searchList, filters2);

            // we have already searched based on Id, InstanceId, Name and Command
            // these can now be removed from the list of items to search
            string[] toRemove = {
                                 Constants.JobMetadataSessionId, 
                                 Constants.JobMetadataInstanceId,
                                 Constants.JobMetadataName, 
                                 Constants.JobMetadataCommand
                                };

            foreach (var key in toRemove.Where(filters2.ContainsKey))
            {
                filters2.Remove(key);
            }

            if (filters2.Count == 0)
                return narrowedList;

            foreach (var job in narrowedList)
            {                
                // we intend to find jobs that match ALL criteria in filters
                // NOT ANY criteria
                bool computerNameMatch = true;
                bool credentialMatch = true;
                object value;
                bool filterMatch = true;

                foreach (KeyValuePair<string, object> filter in filters2)
                {
                    string key = filter.Key;
                    if (!SearchAllFilterTypes((PSWorkflowJob)job, type, key, out value))
                    {
                        computerNameMatch = credentialMatch = filterMatch = false;
                        break;
                    }

                    // if guid is passed as a string try to convert and use the same                    
                    if (value is Guid)
                    {
                        Guid filterValueAsGuid;
                        LanguagePrimitives.TryConvertTo(filter.Value, CultureInfo.InvariantCulture,
                                                                         out filterValueAsGuid);
                        if (filterValueAsGuid == (Guid)value)
                            continue;
                        filterMatch = false;
                        break;
                    }

                    // PSComputerName needs to be special cased because it is actually an array.
                    if (key.Equals(Constants.ComputerName, StringComparison.OrdinalIgnoreCase))
                    {
                        string[] instanceComputers = value as string[];
                        if (instanceComputers == null)
                        {
                            string instanceComputer = value as string;
                            if (instanceComputer == null) break;

                            instanceComputers = new string[] { instanceComputer };
                        }

                        object[] filterComputers = filter.Value as object[];
                        if (filterComputers == null)
                        {
                            string computer = filter.Value as string;
                            if (computer == null) break;

                            filterComputers = new object[] { computer };
                        }

                        foreach(var instanceComputer in instanceComputers)
                        {
                            computerNameMatch = false;
                            // for every computer we want atleast one filter
                            // match
                            foreach(var filterComputer in filterComputers)
                            {
                                string stringFilter = filterComputer as string;
                                if (stringFilter == null) break;
                                WildcardPattern filterPattern = new WildcardPattern(stringFilter, WildcardOptions.IgnoreCase | WildcardOptions.Compiled);

                                if (!filterPattern.IsMatch(instanceComputer)) continue;
                                computerNameMatch = true;
                                break;                                
                            }
                            if (!computerNameMatch) break;
                        }
                        if (!computerNameMatch) break;
                        continue;
                    }

                    if ((key.Equals(Constants.Credential, StringComparison.OrdinalIgnoreCase)))
                    {
                        credentialMatch = false;
                        // PSCredential is a special case because it may be stored as a PSObject.
                        object credentialObject = filter.Value;
                        PSCredential credential = credentialObject as PSCredential;

                        if (credential == null)
                        {
                            PSObject credPsObject = credentialObject as PSObject;
                            if (credPsObject == null) break;

                            credential = credPsObject.BaseObject as PSCredential;
                            if (credential == null) break;
                        }

                        Debug.Assert(credential != null);
                        credentialMatch = WorkflowUtils.CompareCredential(credential, value as PSCredential);
                        continue;
                    }

                    if ((filter.Value is string || filter.Value is WildcardPattern) && value is string)
                    {
                        // at this point we are guaranteed that the key exists somewhere                    
                        WildcardPattern pattern;
                        string stringValue = filter.Value as string;
                        if (stringValue != null)
                            pattern = new WildcardPattern(stringValue,
                                                          WildcardOptions.IgnoreCase | WildcardOptions.Compiled);
                        else pattern = (WildcardPattern)filter.Value;

                        // if the pattern is a match, matched is still true, and don't check
                        // to see if the selected dictionary contains the actual key value pair.
                        if (!pattern.IsMatch((string)value))
                        {
                            // if the pattern is not a match, this doesn't match the given filter.
                            filterMatch = false;
                            break;
                        }
                        continue;
                    }

                    if (value != null && value.Equals(filter.Value)) continue;
                    filterMatch= false;
                    break;
                } // end of foreach - filters

                bool matched = computerNameMatch && credentialMatch && filterMatch;

                if (matched)
                {
                    selectedJobs.Add(job);
                }
            } //end of outer foreach    

            return selectedJobs;
        }
Beispiel #4
0
		private static bool SearchAllFilterTypes(PSWorkflowJob job, WorkflowFilterTypes type, string key, out object value)
		{
			object obj = null;
			bool flag;
			PSWorkflowContext pSWorkflowContext = job.PSWorkflowInstance.PSWorkflowContext;
			value = null;
			if (pSWorkflowContext != null)
			{
				Dictionary<WorkflowFilterTypes, Dictionary<string, object>> workflowFilterTypes = new Dictionary<WorkflowFilterTypes, Dictionary<string, object>>();
				workflowFilterTypes.Add(WorkflowFilterTypes.WorkflowSpecificParameters, pSWorkflowContext.WorkflowParameters);
				workflowFilterTypes.Add(WorkflowFilterTypes.JobMetadata, pSWorkflowContext.JobMetadata);
				workflowFilterTypes.Add(WorkflowFilterTypes.CommonParameters, pSWorkflowContext.PSWorkflowCommonParameters);
				workflowFilterTypes.Add(WorkflowFilterTypes.PrivateMetadata, pSWorkflowContext.PrivateMetadata);
				Dictionary<WorkflowFilterTypes, Dictionary<string, object>> workflowFilterTypes1 = workflowFilterTypes;
				Dictionary<WorkflowFilterTypes, Dictionary<string, object>>.KeyCollection.Enumerator enumerator = workflowFilterTypes1.Keys.GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						WorkflowFilterTypes current = enumerator.Current;
						if (!type.HasFlag(current) || !PSWorkflowJobManager.SearchOneFilterType(workflowFilterTypes1[current], key, out obj))
						{
							continue;
						}
						value = obj;
						flag = true;
						return flag;
					}
					return false;
				}
				finally
				{
					enumerator.Dispose();
				}
				return flag;
			}
			else
			{
				return false;
			}
		}
Beispiel #5
0
		internal IEnumerable<Job2> GetJobs(ICollection<PSWorkflowJob> searchList, WorkflowFilterTypes type, IDictionary<string, object> filters)
		{
			object obj = null;
			Guid guid;
			WildcardPattern value;
			bool flag;
			List<Job2> job2s = new List<Job2>();
			Dictionary<string, object> strs = new Dictionary<string, object>(filters, StringComparer.CurrentCultureIgnoreCase);
			List<Job2> job2s1 = WorkflowJobSourceAdapter.SearchJobsOnV2Parameters(searchList, strs);
			string[] strArrays = new string[4];
			strArrays[0] = "Id";
			strArrays[1] = "InstanceId";
			strArrays[2] = "Name";
			strArrays[3] = "Command";
			string[] strArrays1 = strArrays;
			foreach (string str in strArrays1.Where<string>(new Func<string, bool>(strs.ContainsKey)))
			{
				strs.Remove(str);
			}
			if (strs.Count != 0)
			{
				foreach (Job2 job2 in strs)
				{
					bool flag1 = true;
					bool flag2 = true;
					bool flag3 = true;
					Dictionary<string, object>.Enumerator enumerator = strs.GetEnumerator();
					try
					{
						while (enumerator.MoveNext())
						{
							KeyValuePair<string, object> keyValuePair = job2;
							string key = keyValuePair.Key;
							if (PSWorkflowJobManager.SearchAllFilterTypes((PSWorkflowJob)job2, type, key, out obj))
							{
								if (obj as Guid == null)
								{
									if (!key.Equals("PSComputerName", StringComparison.OrdinalIgnoreCase))
									{
										if (!key.Equals("PSCredential", StringComparison.OrdinalIgnoreCase))
										{
											if ((keyValuePair.Value as string != null || keyValuePair.Value as WildcardPattern != null) && obj as string != null)
											{
												string value1 = keyValuePair.Value as string;
												if (value1 == null)
												{
													value = (WildcardPattern)keyValuePair.Value;
												}
												else
												{
													value = new WildcardPattern(value1, WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
												}
												if (value.IsMatch((string)obj))
												{
													continue;
												}
												break;
											}
											else
											{
												if (obj != null && obj.Equals(keyValuePair.Value))
												{
													continue;
												}
												break;
											}
										}
										else
										{
											object obj1 = keyValuePair.Value;
											PSCredential baseObject = obj1 as PSCredential;
											if (baseObject == null)
											{
												PSObject pSObject = obj1 as PSObject;
												if (pSObject == null)
												{
													break;
												}
												baseObject = pSObject.BaseObject as PSCredential;
												if (baseObject == null)
												{
													break;
												}
											}
											flag2 = WorkflowUtils.CompareCredential(baseObject, obj as PSCredential);
										}
									}
									else
									{
										string[] strArrays2 = obj as string[];
										if (strArrays2 == null)
										{
											string str1 = obj as string;
											if (str1 == null)
											{
												break;
											}
											string[] strArrays3 = new string[1];
											strArrays3[0] = str1;
											strArrays2 = strArrays3;
										}
										object[] objArray = keyValuePair.Value as object[];
										if (objArray == null)
										{
											string value2 = keyValuePair.Value as string;
											if (value2 == null)
											{
												break;
											}
											object[] objArray1 = new object[1];
											objArray1[0] = value2;
											objArray = objArray1;
										}
										string[] strArrays4 = strArrays2;
										for (int i = 0; i < (int)strArrays4.Length; i++)
										{
											string str2 = strArrays4[i];
											flag1 = false;
											object[] objArray2 = objArray;
											int num = 0;
											while (num < (int)objArray2.Length)
											{
												object obj2 = objArray2[num];
												string str3 = obj2 as string;
												if (str3 == null)
												{
													break;
												}
												WildcardPattern wildcardPattern = new WildcardPattern(str3, WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
												if (!wildcardPattern.IsMatch(str2))
												{
													num++;
												}
												else
												{
													flag1 = true;
													break;
												}
											}
											if (!flag1)
											{
												break;
											}
										}
										if (flag1)
										{
											continue;
										}
										break;
									}
								}
								else
								{
									LanguagePrimitives.TryConvertTo<Guid>(keyValuePair.Value, CultureInfo.InvariantCulture, out guid);
									if (guid == (Guid)obj)
									{
										continue;
									}
									break;
								}
							}
							else
							{
								break;
							}
						}
					}
					finally
					{
						enumerator.Dispose();
					}
					if (!flag1 || !flag2)
					{
						flag = false;
					}
					else
					{
						flag = flag3;
					}
					bool flag4 = flag;
					if (!flag4)
					{
						continue;
					}
					job2s.Add(job2);
				}
				return job2s;
			}
			else
			{
				return job2s1;
			}
		}
Beispiel #6
0
		internal IEnumerable<Job2> GetJobs(WorkflowFilterTypes type, Dictionary<string, object> filters)
		{
			PSWorkflowJobManager.Tracer.WriteMessage("WorkflowManager : Geting workflow instances based on filters");
			return this.GetJobs(this._wfJobTable.Values, type, filters);
		}