/********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using Dbg = System.Management.Automation.Diagnostics; namespace System.Management.Automation { /// /// Searcher class for finding DscResources on the system. /// internal class DscResourceSearcher : IEnumerable, IEnumerator { internal DscResourceSearcher( string resourceName, ExecutionContext context) { Diagnostics.Assert(context != null, "caller to verify context is not null"); Diagnostics.Assert(!string.IsNullOrEmpty(resourceName), "caller to verify commandName is valid"); _resourceName = resourceName; _context = context; } #region private properties private string _resourceName = null; private ExecutionContext _context = null; private DscResourceInfo _currentMatch = null; private IEnumerator _matchingResource = null; private Collection _matchingResourceList = null; #endregion #region public methods /// /// Reset the Iterator. /// public void Reset() { _currentMatch = null; _matchingResource = null; } /// /// Reset and dispose the Iterator. /// public void Dispose() { Reset(); GC.SuppressFinalize(this); } /// /// Get the Enumerator. /// /// IEnumerator IEnumerable.GetEnumerator() { return this; } /// /// Get the Enumerator /// /// IEnumerator IEnumerable.GetEnumerator() { return this; } /// /// Move to the Next value in the enumerator. /// /// public bool MoveNext() { _currentMatch = GetNextDscResource(); if (_currentMatch != null) return true; return false; } /// /// Return the current DscResource /// DscResourceInfo IEnumerator.Current { get { return _currentMatch; } } /// /// Return the current DscResource as object /// object IEnumerator.Current { get { return ((IEnumerator)this).Current; } } #endregion #region private methods /// /// Invoke command Get-DscResource with resource name to find the resource. /// When found add them to the enumerator. If we have already got it, return the next resource. /// /// Next DscResource Info object or null if none are found. private DscResourceInfo GetNextDscResource() { var ps = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Get-DscResource"); WildcardPattern resourceMatcher = WildcardPattern.Get(_resourceName, WildcardOptions.IgnoreCase); if (_matchingResourceList == null) { Collection psObjs = ps.Invoke(); _matchingResourceList = new Collection(); bool matchFound = false; foreach (dynamic resource in psObjs) { if (resource.Name != null) { string resourceName = resource.Name; if (resourceMatcher.IsMatch(resourceName)) { DscResourceInfo resourceInfo = new DscResourceInfo(resourceName, resource.ResourceType, resource.Path, resource.ParentPath, _context ); resourceInfo.FriendlyName = resource.FriendlyName; resourceInfo.CompanyName = resource.CompanyName; PSModuleInfo psMod = resource.Module as PSModuleInfo; if (psMod != null) resourceInfo.Module = psMod; if (resource.ImplementedAs != null) { ImplementedAsType impType; if (Enum.TryParse(resource.ImplementedAs.ToString(), out impType)) resourceInfo.ImplementedAs = impType; } var properties = resource.Properties as IList; if (properties != null) { List propertyList = new List(); foreach (dynamic prop in properties) { DscResourcePropertyInfo propInfo = new DscResourcePropertyInfo(); propInfo.Name = prop.Name; propInfo.PropertyType = prop.PropertyType; propInfo.UpdateValues(prop.Values); propertyList.Add(propInfo); } resourceInfo.UpdateProperties(propertyList); } _matchingResourceList.Add(resourceInfo); matchFound = true; } //if }//if }// foreach if (matchFound) _matchingResource = _matchingResourceList.GetEnumerator(); else return null; }//if if (!_matchingResource.MoveNext()) { _matchingResource = null; } else { return _matchingResource.Current; } return null; } #endregion } }