internal static List<OrderByPropertyEntry> CreateOrderMatrix(PSCmdlet cmdlet, List<PSObject> inputObjects, List<MshParameter> mshParameterList) { List<OrderByPropertyEntry> list = new List<OrderByPropertyEntry>(); foreach (PSObject obj2 in inputObjects) { if ((obj2 != null) && (obj2 != AutomationNull.Value)) { List<ErrorRecord> errors = new List<ErrorRecord>(); List<string> propertyNotFoundMsgs = new List<string>(); OrderByPropertyEntry item = OrderByPropertyEntryEvaluationHelper.ProcessObject(obj2, mshParameterList, errors, propertyNotFoundMsgs); foreach (ErrorRecord record in errors) { cmdlet.WriteError(record); } foreach (string str in propertyNotFoundMsgs) { cmdlet.WriteDebug(str); } list.Add(item); } } return list; }
public Dictionary<string, List<string>> CheckRuleExtension(string[] path, PSCmdlet cmdlet) { Dictionary<string, List<string>> results = new Dictionary<string, List<string>>(); List<string> invalidPaths = new List<string>(); List<string> validDllPaths = new List<string>(); List<string> validModPaths = new List<string>(); // Gets valid module names foreach (string childPath in path) { try { cmdlet.WriteVerbose(string.Format(CultureInfo.CurrentCulture, Strings.CheckModuleName, childPath)); string resolvedPath = string.Empty; // Users may provide a valid module path or name, // We have to identify the childPath is really a directory or just a module name. // You can also consider following two commands. // Get-ScriptAnalyzerRule -RuleExtension "ContosoAnalyzerRules" // Get-ScriptAnalyzerRule -RuleExtension "%USERPROFILE%\WindowsPowerShell\Modules\ContosoAnalyzerRules" if (Path.GetDirectoryName(childPath) == string.Empty) { resolvedPath = childPath; } else { resolvedPath = cmdlet.SessionState.Path .GetResolvedPSPathFromPSPath(childPath).First().ToString(); } using (System.Management.Automation.PowerShell posh = System.Management.Automation.PowerShell.Create()) { string script = string.Format(CultureInfo.CurrentCulture, "Get-Module -Name '{0}' -ListAvailable", resolvedPath); PSModuleInfo moduleInfo = posh.AddScript(script).Invoke<PSModuleInfo>().First(); // Adds original path, otherwise path.Except<string>(validModPaths) will fail. // It's possible that user can provide something like this: // "..\..\..\ScriptAnalyzer.UnitTest\modules\CommunityAnalyzerRules\CommunityAnalyzerRules.psd1" if (moduleInfo.ExportedFunctions.Count > 0) validModPaths.Add(childPath); } } catch { // User may provide an invalid module name, like c:\temp. // It's a invalid name for a Windows PowerShell module, // But we need test it further since we allow user to provide a folder to extend rules. // You can also consider following two commands. // Get-ScriptAnalyzerRule -RuleExtension "ContosoAnalyzerRules", "C:\Temp\ExtendScriptAnalyzerRules.dll" // Get-ScriptAnalyzerRule -RuleExtension "ContosoAnalyzerRules", "C:\Temp\" continue; } } // Gets valid dll paths foreach (string childPath in path.Except<string>(validModPaths)) { try { string resolvedPath = cmdlet.SessionState.Path .GetResolvedPSPathFromPSPath(childPath).First().ToString(); cmdlet.WriteDebug(string.Format(CultureInfo.CurrentCulture, Strings.CheckAssemblyFile, resolvedPath)); if (String.Equals(Path.GetExtension(resolvedPath),".dll", StringComparison.OrdinalIgnoreCase)) { if (!File.Exists(resolvedPath)) { invalidPaths.Add(resolvedPath); continue; } } else { if (!Directory.Exists(resolvedPath)) { invalidPaths.Add(resolvedPath); continue; } } validDllPaths.Add(resolvedPath); } catch { invalidPaths.Add(childPath); } } // Resloves relative paths. try { for (int i = 0; i < validModPaths.Count; i++) { validModPaths[i] = cmdlet.SessionState.Path .GetResolvedPSPathFromPSPath(validModPaths[i]).First().ToString(); } for (int i = 0; i < validDllPaths.Count; i++) { validDllPaths[i] = cmdlet.SessionState.Path .GetResolvedPSPathFromPSPath(validDllPaths[i]).First().ToString(); } } catch { // If GetResolvedPSPathFromPSPath failed. We can safely ignore the exception. // Because GetResolvedPSPathFromPSPath always fails when trying to resolve a module name. } // Returns valid rule extensions results.Add("InvalidPaths", invalidPaths); results.Add("ValidModPaths", validModPaths); results.Add("ValidDllPaths", validDllPaths); return results; }