MegaBites-AI/Windows-powershell
0308
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections;5using System.Collections.Generic;6using System.Collections.ObjectModel;7 8using Dbg = System.Management.Automation.Diagnostics;9 10namespace System.Management.Automation11{12 /// <summary>13 /// Searcher class for finding DscResources on the system.14 /// </summary>15 internal class DscResourceSearcher : IEnumerable<DscResourceInfo>, IEnumerator<DscResourceInfo>16 {17 internal DscResourceSearcher(18 string resourceName,19 ExecutionContext context)20 {21 Diagnostics.Assert(context != null, "caller to verify context is not null");22 Diagnostics.Assert(!string.IsNullOrEmpty(resourceName), "caller to verify commandName is valid");23 24 _resourceName = resourceName;25 _context = context;26 }27 28 #region private properties29 30 private readonly string _resourceName = null;31 private readonly ExecutionContext _context = null;32 private DscResourceInfo _currentMatch = null;33 private IEnumerator<DscResourceInfo> _matchingResource = null;34 private Collection<DscResourceInfo> _matchingResourceList = null;35 36 #endregion37 38 #region public methods39 40 /// <summary>41 /// Reset the Iterator.42 /// </summary>43 public void Reset()44 {45 _currentMatch = null;46 _matchingResource = null;47 }48 49 /// <summary>50 /// Reset and dispose the Iterator.51 /// </summary>52 public void Dispose()53 {54 Reset();55 GC.SuppressFinalize(this);56 }57 58 /// <summary>59 /// Get the Enumerator.60 /// </summary>61 /// <returns></returns>62 IEnumerator<DscResourceInfo> IEnumerable<DscResourceInfo>.GetEnumerator()63 {64 return this;65 }66 67 /// <summary>68 /// Get the Enumerator.69 /// </summary>70 /// <returns></returns>71 IEnumerator IEnumerable.GetEnumerator()72 {73 return this;74 }75 76 /// <summary>77 /// Move to the Next value in the enumerator.78 /// </summary>79 /// <returns></returns>80 public bool MoveNext()81 {82 _currentMatch = GetNextDscResource();83 84 if (_currentMatch != null)85 return true;86 87 return false;88 }89 90 /// <summary>91 /// Return the current DscResource.92 /// </summary>93 DscResourceInfo IEnumerator<DscResourceInfo>.Current94 {95 get96 {97 return _currentMatch;98 }99 }100 101 /// <summary>102 /// Return the current DscResource as object.103 /// </summary>104 object IEnumerator.Current105 {106 get107 {108 return ((IEnumerator<DscResourceInfo>)this).Current;109 }110 }111 112 #endregion113 114 #region private methods115 116 /// <summary>117 /// Invoke command Get-DscResource with resource name to find the resource.118 /// When found add them to the enumerator. If we have already got it, return the next resource.119 /// </summary>120 /// <returns>Next DscResource Info object or null if none are found.</returns>121 private DscResourceInfo GetNextDscResource()122 {123 var ps = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Get-DscResource");124 125 WildcardPattern resourceMatcher = WildcardPattern.Get(_resourceName, WildcardOptions.IgnoreCase);126 127 if (_matchingResourceList == null)128 {129 Collection<PSObject> psObjs = ps.Invoke();130 131 _matchingResourceList = new Collection<DscResourceInfo>();132 133 bool matchFound = false;134 135 foreach (dynamic resource in psObjs)136 {137 if (resource.Name != null)138 {139 string resourceName = resource.Name;140 141 if (resourceMatcher.IsMatch(resourceName))142 {143 DscResourceInfo resourceInfo = new DscResourceInfo(resourceName,144 resource.ResourceType,145 resource.Path,146 resource.ParentPath,147 _context148 );149 150 resourceInfo.FriendlyName = resource.FriendlyName;151 152 resourceInfo.CompanyName = resource.CompanyName;153 154 PSModuleInfo psMod = resource.Module as PSModuleInfo;155 156 if (psMod != null)157 resourceInfo.Module = psMod;158 159 if (resource.ImplementedAs != null)160 {161 ImplementedAsType impType;162 if (Enum.TryParse<ImplementedAsType>(resource.ImplementedAs.ToString(), out impType))163 resourceInfo.ImplementedAs = impType;164 }165 166 var properties = resource.Properties as IList;167 168 if (properties != null)169 {170 List<DscResourcePropertyInfo> propertyList = new List<DscResourcePropertyInfo>();171 172 foreach (dynamic prop in properties)173 {174 DscResourcePropertyInfo propInfo = new DscResourcePropertyInfo();175 propInfo.Name = prop.Name;176 propInfo.PropertyType = prop.PropertyType;177 propInfo.UpdateValues(prop.Values);178 179 propertyList.Add(propInfo);180 }181 182 resourceInfo.UpdateProperties(propertyList);183 }184 185 _matchingResourceList.Add(resourceInfo);186 187 matchFound = true;188 }189 }190 }191 192 if (matchFound)193 _matchingResource = _matchingResourceList.GetEnumerator();194 else195 return null;196 }197 198 if (!_matchingResource.MoveNext())199 {200 _matchingResource = null;201 }202 else203 {204 return _matchingResource.Current;205 }206 207 return null;208 }209 210 #endregion211 }212}213 