Skip to content

Commit 3036d40

Browse files
author
Kapil Borle
committed
Fix invoking DscClassCache.ImportClasses method
In most version of PowerShell the method has 3 parameters but in some it has 4. This commit checks the number of parameters before invoking the method.
1 parent 602a706 commit 3036d40

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Rules/UseIdenticalMandatoryParametersDSC.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.IO;
2020
using System.Linq;
2121
using System.Management.Automation.Language;
22+
using System.Reflection;
2223
using Microsoft.Management.Infrastructure;
2324
using Microsoft.PowerShell.DesiredStateConfiguration.Internal;
2425
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Extensions;
@@ -215,7 +216,19 @@ private IDictionary<string, string> GetKeys(string fileName)
215216
isDSCClassCacheInitialized = true;
216217
}
217218

218-
cimClasses = DscClassCache.ImportClasses(mofFilepath, moduleInfo, errors);
219+
var importClassesMethod = typeof(DscClassCache).GetMethod("ImportClasses", BindingFlags.Public | BindingFlags.Static);
220+
if (importClassesMethod != null)
221+
{
222+
var parameters = new List<object>(new object[] { mofFilepath, moduleInfo, errors });
223+
224+
// In some version of S.M.A ImportClasses classes method takes 4 parameters
225+
// while in others it takes 3.
226+
if (importClassesMethod.GetParameters().Count() == 4)
227+
{
228+
parameters.Add(false);
229+
}
230+
cimClasses = importClassesMethod.Invoke(null, parameters.ToArray()) as List<CimClass>;
231+
}
219232
}
220233
catch
221234
{

0 commit comments

Comments
 (0)