private IEnumerable <PSModuleInfo> ModulesFromResult(DynamicPowershellResult result) { if (result.Success && result.Value != null) { foreach (var module in result.OfType <PSModuleInfo>()) { yield return(module); } } }
internal IEnumerable <string> ScanForModules(Request request) { // two places we search for modules // 1. in this assembly's folder, look for all psd1 and psm1 files. // // 2. modules in the PSMODULEPATH // // Import each one of those, and check to see if they have a OneGet.Providers section in their private data using (dynamic ps = new DynamicPowershell()) { if (BaseFolder != null) { var files = Directory.EnumerateFiles(BaseFolder, "*.psd1", SearchOption.AllDirectories); // load the powershell functions into this runspace in case something needed it on module load. var psf = ps.ImportModule(Name: PowerShellProviderFunctions, PassThru: true); #if DEBUG var testProviders = Directory.EnumerateFiles(BaseFolder, "*.psm1", SearchOption.AllDirectories); foreach (var provider in testProviders) { yield return(provider); } #endif foreach (var each in files) { DynamicPowershellResult items = ps.ImportModule(Name: each, PassThru: true); items.WaitForCompletion(); var errors = items.Errors.ToArray(); if (errors.Any()) { request.Debug("\r\n\r\n==================================================================================\r\n===In Module '{0}'", each); foreach (var error in errors) { try { switch (error.CategoryInfo.Category) { case System.Management.Automation.ErrorCategory.ResourceUnavailable: // file not found continue; default: request.Debug(" PowerShell {0} {1} ", error.CategoryInfo.Category, error.Exception.Message); break; } } catch (Exception e) { e.Dump(); } } continue; } foreach (var onegetModule in items.OfType <PSModuleInfo>().SelectMany(GetOneGetModules)) { yield return(onegetModule); } } } foreach (var onegetModule in ModulesFromResult((DynamicPowershellResult)ps.GetModule(ListAvailable: true)).SelectMany(GetOneGetModules)) { yield return(onegetModule); } } }