diff --git a/Engine/Generic/ModuleDependencyHandler.cs b/Engine/Generic/ModuleDependencyHandler.cs index 61d755b04..f107c9a56 100644 --- a/Engine/Generic/ModuleDependencyHandler.cs +++ b/Engine/Generic/ModuleDependencyHandler.cs @@ -461,8 +461,7 @@ public static IEnumerable GetModuleNameFromErrorExtent(ParseError error, var paramValAst = dynamicKywdAst.CommandElements[positionOfModuleNameParamter]; // import-dscresource -ModuleName module1 - var paramValStrConstExprAst = paramValAst as StringConstantExpressionAst; - if (paramValStrConstExprAst != null) + if (paramValAst is StringConstantExpressionAst paramValStrConstExprAst) { modules.Add(paramValStrConstExprAst.Value); @@ -470,15 +469,30 @@ public static IEnumerable GetModuleNameFromErrorExtent(ParseError error, var versionParameterAst = dynamicKywdAst.CommandElements[positionOfModuleVersionParameter] as StringConstantExpressionAst; if (versionParameterAst != null) { - Version.TryParse(versionParameterAst.Value, out Version version); // ignore return value since a module version of null means no version - moduleVersion = version; + Version.TryParse(versionParameterAst.Value, out moduleVersion); // ignore return value since a module version of null means no version } return modules; } - + + // Import-DscResource –ModuleName @{ModuleName="module1";ModuleVersion="1.2.3.4"} + //var paramValAstHashtableAst = paramValAst.Find(oneAst => oneAst is HashtableAst, true) as HashtableAst; + if (paramValAst.Find(oneAst => oneAst is HashtableAst, true) is HashtableAst paramValAstHashtableAst) + { + var moduleNameTuple = paramValAstHashtableAst.KeyValuePairs.SingleOrDefault(x => x.Item1.Extent.Text.Equals("ModuleName")); + var moduleName = moduleNameTuple.Item2.Find(astt => astt is StringConstantExpressionAst, true) as StringConstantExpressionAst; + if (moduleName == null) + { + return null; + } + modules.Add(moduleName.Value); + var moduleVersionTuple = paramValAstHashtableAst.KeyValuePairs.SingleOrDefault(x => x.Item1.Extent.Text.Equals("ModuleVersion")); + var moduleVersionAst = moduleVersionTuple.Item2.Find(astt => astt is StringConstantExpressionAst, true) as StringConstantExpressionAst; + Version.TryParse(moduleVersionAst.Value, out moduleVersion); + return modules; + } + // import-dscresource -ModuleName module1,module2 - var paramValArrLtrlAst = paramValAst as ArrayLiteralAst; - if (paramValArrLtrlAst != null) + if (paramValAst is ArrayLiteralAst paramValArrLtrlAst) { foreach (var elem in paramValArrLtrlAst.Elements) { diff --git a/Tests/Engine/ModuleDependencyHandler.tests.ps1 b/Tests/Engine/ModuleDependencyHandler.tests.ps1 index 0b00c9147..f6d3f2f9a 100644 --- a/Tests/Engine/ModuleDependencyHandler.tests.ps1 +++ b/Tests/Engine/ModuleDependencyHandler.tests.ps1 @@ -105,6 +105,22 @@ Describe "Resolve DSC Resource Dependency" { $moduleVersion | Should -Be ([version]'1.2.3.4') } + It "Extracts 1 module name with version using HashTable syntax" -skip:$skipTest { + $sb = @" +{Configuration SomeConfiguration +{ + Import-DscResource -ModuleName (@{ModuleName="SomeDscModule1";ModuleVersion="1.2.3.4"}) +}} +"@ + $tokens = $null + $parseError = $null + $ast = [System.Management.Automation.Language.Parser]::ParseInput($sb, [ref]$tokens, [ref]$parseError) + $moduleVersion = $null + $resultModuleNames = $moduleHandlerType::GetModuleNameFromErrorExtent($parseError[0], $ast, [ref]$moduleVersion).ToArray() + $resultModuleNames[0] | Should -Be 'SomeDscModule1' + $moduleVersion | Should -Be ([version]'1.2.3.4') + } + It "Extracts more than 1 module names" -skip:$skipTest { $sb = @" {Configuration SomeConfiguration