diff --git a/Engine/Helper.cs b/Engine/Helper.cs index b093d9c0f..c70a876dd 100644 --- a/Engine/Helper.cs +++ b/Engine/Helper.cs @@ -295,35 +295,36 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable psObj = null; - var ps = System.Management.Automation.PowerShell.Create(); - try - { - ps.AddCommand("Test-ModuleManifest"); - ps.AddParameter("Path", filePath); - ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue); - psObj = ps.Invoke(); - } - catch (CmdletInvocationException e) + using (var ps = System.Management.Automation.PowerShell.Create()) { - // Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys - // throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence - // we consume it to allow execution of the of this method. - if (e.InnerException == null || e.InnerException.GetType() != typeof(System.NullReferenceException)) + try { - throw; + ps.AddCommand("Test-ModuleManifest"); + ps.AddParameter("Path", filePath); + ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue); + psObj = ps.Invoke(); + } + catch (CmdletInvocationException e) + { + // Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys + // throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence + // we consume it to allow execution of the of this method. + if (e.InnerException == null || e.InnerException.GetType() != typeof(System.NullReferenceException)) + { + throw; + } + } + if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null) + { + var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count]; + ps.Streams.Error.CopyTo(errorRecordArr, 0); + errorRecord = errorRecordArr; + } + if (psObj != null && psObj.Any() && psObj[0] != null) + { + psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo; } } - if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null) - { - var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count]; - ps.Streams.Error.CopyTo(errorRecordArr, 0); - errorRecord = errorRecordArr; - } - if (psObj != null && psObj.Any() && psObj[0] != null) - { - psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo; - } - ps.Dispose(); return psModuleInfo; } diff --git a/Rules/AvoidUsingDeprecatedManifestFields.cs b/Rules/AvoidUsingDeprecatedManifestFields.cs index c74b3448b..8856745d2 100644 --- a/Rules/AvoidUsingDeprecatedManifestFields.cs +++ b/Rules/AvoidUsingDeprecatedManifestFields.cs @@ -40,83 +40,83 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) } if (Helper.IsModuleManifest(fileName)) { - var ps = System.Management.Automation.PowerShell.Create(); - IEnumerable result = null; - - // hash table in psd1 - var hashTableAst = ast.FindAll(item => item is HashtableAst, false).FirstOrDefault(); - - // no hash table means not a module manifest - if (hashTableAst == null) + using (var ps = System.Management.Automation.PowerShell.Create()) { - yield break; - } + IEnumerable result = null; - var table = hashTableAst as HashtableAst; + // hash table in psd1 + var hashTableAst = ast.FindAll(item => item is HashtableAst, false).FirstOrDefault(); - // needs to find the PowerShellVersion key - foreach (var kvp in table.KeyValuePairs) - { - if (kvp.Item1 != null && kvp.Item1 is StringConstantExpressionAst) + // no hash table means not a module manifest + if (hashTableAst == null) { - var key = (kvp.Item1 as StringConstantExpressionAst).Value; + yield break; + } - // find the powershellversion key in the hashtable - if (string.Equals(key, "PowerShellVersion", StringComparison.OrdinalIgnoreCase) && kvp.Item2 != null) + var table = hashTableAst as HashtableAst; + + // needs to find the PowerShellVersion key + foreach (var kvp in table.KeyValuePairs) + { + if (kvp.Item1 != null && kvp.Item1 is StringConstantExpressionAst) { - // get the string value of the version - var value = kvp.Item2.Find(item => item is StringConstantExpressionAst, false); + var key = (kvp.Item1 as StringConstantExpressionAst).Value; - if (value != null) + // find the powershellversion key in the hashtable + if (string.Equals(key, "PowerShellVersion", StringComparison.OrdinalIgnoreCase) && kvp.Item2 != null) { - Version psVersion = null; + // get the string value of the version + var value = kvp.Item2.Find(item => item is StringConstantExpressionAst, false); - // get the version - if (Version.TryParse((value as StringConstantExpressionAst).Value, out psVersion)) + if (value != null) { - // if version exists and version less than 3, don't raise rule - if (psVersion.Major < 3) + Version psVersion = null; + + // get the version + if (Version.TryParse((value as StringConstantExpressionAst).Value, out psVersion)) { - yield break; + // if version exists and version less than 3, don't raise rule + if (psVersion.Major < 3) + { + yield break; + } } } } } } - } - try - { - ps.AddCommand("Test-ModuleManifest"); - ps.AddParameter("Path", fileName); - - // Suppress warnings emitted during the execution of Test-ModuleManifest - // ModuleManifest rule must catch any violations (warnings/errors) and generate DiagnosticRecord(s) - ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue); - ps.AddParameter("WarningVariable", "Message"); - ps.AddScript("$Message"); - result = ps.Invoke(); - } - catch - {} + try + { + ps.AddCommand("Test-ModuleManifest"); + ps.AddParameter("Path", fileName); + + // Suppress warnings emitted during the execution of Test-ModuleManifest + // ModuleManifest rule must catch any violations (warnings/errors) and generate DiagnosticRecord(s) + ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue); + ps.AddParameter("WarningVariable", "Message"); + ps.AddScript("$Message"); + result = ps.Invoke(); + } + catch + {} - if (result != null) - { - foreach (var warning in result) + if (result != null) { - if (warning.BaseObject != null) + foreach (var warning in result) { - yield return - new DiagnosticRecord( - String.Format(CultureInfo.CurrentCulture, warning.BaseObject.ToString()), ast.Extent, - GetName(), DiagnosticSeverity.Warning, fileName); + if (warning.BaseObject != null) + { + yield return + new DiagnosticRecord( + String.Format(CultureInfo.CurrentCulture, warning.BaseObject.ToString()), ast.Extent, + GetName(), DiagnosticSeverity.Warning, fileName); + } } } - } - ps.Dispose(); + } } - } ///