Skip to content

Commit 5e29444

Browse files
author
Kapil Borle
committed
Modify a helper method
Adds exception handling for a particular case in GetModuleManifest method
1 parent 6fcf059 commit 5e29444

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

Engine/Helper.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Management.Automation.Language;
2020
using System.Globalization;
2121
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
22+
using System.Management.Automation.Runspaces;
2223

2324
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
2425
{
@@ -103,14 +104,14 @@ internal set
103104
private string[] functionScopes = new string[] { "global:", "local:", "script:", "private:" };
104105

105106
private string[] variableScopes = new string[] { "global:", "local:", "script:", "private:", "variable:", ":" };
106-
107107
#endregion
108108

109109
/// <summary>
110110
/// Initializes the Helper class.
111111
/// </summary>
112112
private Helper()
113113
{
114+
114115
}
115116

116117
/// <summary>
@@ -236,25 +237,28 @@ public bool IsDscResourceModule(string filePath)
236237
/// <param name="errorRecord"></param>
237238
/// <returns>Returns a object of type PSModuleInfo</returns>
238239
public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorRecord> errorRecord)
239-
{
240+
{
240241
errorRecord = null;
241242
PSModuleInfo psModuleInfo = null;
242243
Collection<PSObject> psObj = null;
243244
var ps = System.Management.Automation.PowerShell.Create();
244-
if (ps == null)
245-
{
246-
return null;
247-
}
248245
try
249-
{
246+
{
250247
ps.AddCommand("Test-ModuleManifest");
251248
ps.AddParameter("Path", filePath);
252-
253-
// Suppress warnings emitted during the execution of Test-ModuleManifest
254249
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
255250
psObj = ps.Invoke();
256251
}
257-
catch { }
252+
catch (CmdletInvocationException e)
253+
{
254+
// Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys
255+
// throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence
256+
// we consume it to allow execution of the of this method.
257+
if (e.InnerException == null || e.InnerException.GetType() != typeof(System.NullReferenceException))
258+
{
259+
throw;
260+
}
261+
}
258262
if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null)
259263
{
260264
var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count];
@@ -265,7 +269,7 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorReco
265269
{
266270
psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo;
267271
}
268-
ps.Dispose();
272+
ps.Dispose();
269273
return psModuleInfo;
270274
}
271275

@@ -1364,8 +1368,7 @@ public static string[] ProcessCustomRulePaths(string[] rulePaths, SessionState s
13641368

13651369
}
13661370

1367-
1368-
#endregion
1371+
#endregion Methods
13691372
}
13701373

13711374

0 commit comments

Comments
 (0)