19
19
using System . Management . Automation . Language ;
20
20
using System . Globalization ;
21
21
using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
22
+ using System . Management . Automation . Runspaces ;
22
23
23
24
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer
24
25
{
@@ -103,14 +104,14 @@ internal set
103
104
private string [ ] functionScopes = new string [ ] { "global:" , "local:" , "script:" , "private:" } ;
104
105
105
106
private string [ ] variableScopes = new string [ ] { "global:" , "local:" , "script:" , "private:" , "variable:" , ":" } ;
106
-
107
107
#endregion
108
108
109
109
/// <summary>
110
110
/// Initializes the Helper class.
111
111
/// </summary>
112
112
private Helper ( )
113
113
{
114
+
114
115
}
115
116
116
117
/// <summary>
@@ -236,25 +237,28 @@ public bool IsDscResourceModule(string filePath)
236
237
/// <param name="errorRecord"></param>
237
238
/// <returns>Returns a object of type PSModuleInfo</returns>
238
239
public PSModuleInfo GetModuleManifest ( string filePath , out IEnumerable < ErrorRecord > errorRecord )
239
- {
240
+ {
240
241
errorRecord = null ;
241
242
PSModuleInfo psModuleInfo = null ;
242
243
Collection < PSObject > psObj = null ;
243
244
var ps = System . Management . Automation . PowerShell . Create ( ) ;
244
- if ( ps == null )
245
- {
246
- return null ;
247
- }
248
245
try
249
- {
246
+ {
250
247
ps . AddCommand ( "Test-ModuleManifest" ) ;
251
248
ps . AddParameter ( "Path" , filePath ) ;
252
-
253
- // Suppress warnings emitted during the execution of Test-ModuleManifest
254
249
ps . AddParameter ( "WarningAction" , ActionPreference . SilentlyContinue ) ;
255
250
psObj = ps . Invoke ( ) ;
256
251
}
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
+ }
258
262
if ( ps . HadErrors && ps . Streams != null && ps . Streams . Error != null )
259
263
{
260
264
var errorRecordArr = new ErrorRecord [ ps . Streams . Error . Count ] ;
@@ -265,7 +269,7 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorReco
265
269
{
266
270
psModuleInfo = psObj [ 0 ] . ImmediateBaseObject as PSModuleInfo ;
267
271
}
268
- ps . Dispose ( ) ;
272
+ ps . Dispose ( ) ;
269
273
return psModuleInfo ;
270
274
}
271
275
@@ -1364,8 +1368,7 @@ public static string[] ProcessCustomRulePaths(string[] rulePaths, SessionState s
1364
1368
1365
1369
}
1366
1370
1367
-
1368
- #endregion
1371
+ #endregion Methods
1369
1372
}
1370
1373
1371
1374
0 commit comments