From aba0d48ba5fe74842ceb3584753e4fc9bca1bc92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Subileau?= Date: Mon, 27 Aug 2018 12:44:34 +0200 Subject: [PATCH] win_nssm: fix error with app_parameters parameter Fix #25265 --- lib/ansible/modules/windows/win_nssm.ps1 | 34 +++++++----------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/lib/ansible/modules/windows/win_nssm.ps1 b/lib/ansible/modules/windows/win_nssm.ps1 index cad414f0487..130a77a6229 100644 --- a/lib/ansible/modules/windows/win_nssm.ps1 +++ b/lib/ansible/modules/windows/win_nssm.ps1 @@ -20,7 +20,7 @@ $name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $tru $state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent","started","stopped","restarted" -resultobj $result $application = Get-AnsibleParam -obj $params -name "application" -type "str" -$appParameters = Get-AnsibleParam -obj $params -name "app_parameters" -type "str" +$appParameters = Get-AnsibleParam -obj $params -name "app_parameters" $appParametersFree = Get-AnsibleParam -obj $params -name "app_parameters_free_form" -type "str" $startMode = Get-AnsibleParam -obj $params -name "start_mode" -type "str" -default "auto" -validateset "auto","delayed","manual","disabled" -resultobj $result @@ -35,6 +35,9 @@ if (($appParameters -ne $null) -and ($appParametersFree -ne $null)) { Fail-Json $result "Use either app_parameters or app_parameteres_free_form, but not both" } +if (($appParameters -ne $null) -and ($appParameters -isnot [System.Collections.Hashtable])) { + Fail-Json -obj $result -message "The app_parameters parameter must be a dict" +} Function Nssm-Invoke { @@ -167,30 +170,13 @@ Function Nssm-Install } } -Function ParseAppParameters() -{ - [CmdletBinding()] - param( - [Parameter(Mandatory=$true)] - [AllowEmptyString()] - [string]$appParameters - ) - - $escapedAppParameters = $appParameters.TrimStart("@").TrimStart("{").TrimEnd("}").Replace("; ","`n").Replace("\","\\") - - return ConvertFrom-StringData -StringData $escapedAppParameters -} - - Function Nssm-Update-AppParameters { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$name, - [Parameter(Mandatory=$true)] - [AllowEmptyString()] - [string]$appParameters, + $appParameters, [string]$appParametersFree ) @@ -208,10 +194,9 @@ Function Nssm-Update-AppParameters $appParamVals = @() $singleLineParams = "" - if ($appParameters) + if ($null -ne $appParameters) { - $appParametersHash = ParseAppParameters -appParameters $appParameters - $appParametersHash.GetEnumerator() | + $appParameters.GetEnumerator() | % { $key = $($_.Name) $val = $($_.Value) @@ -222,15 +207,14 @@ Function Nssm-Update-AppParameters if ($key -eq "_") { $singleLineParams = "$val " + $singleLineParams } else { - $singleLineParams = $singleLineParams + "$key ""$val""" + $singleLineParams = $singleLineParams + " $key ""$val""" } } - $result.nssm_app_parameters_parsed = $appParametersHash $result.nssm_app_parameters_keys = $appParamKeys $result.nssm_app_parameters_vals = $appParamVals } - elseif ($appParametersFree) { + elseif ($null -ne $appParametersFree) { $result.nssm_app_parameters_free_form = $appParametersFree $singleLineParams = $appParametersFree }