Home > database >  why running powershell scripts in two different ways gives different results
why running powershell scripts in two different ways gives different results

Time:03-04

hi I have a very simple script trying to read json input, and convert

param(
    $proxyinfosjson
)
write-output "proxyinfosjson is $proxyinfosjson"

try {
    $proxyinfos = ConvertFrom-Json -InputObject $proxyinfosjson
    write-output $proxyinfos
    
}
catch {
    Write-Output "could not convert json input"
}

if I run

.\testjson.ps1 -proxyinfosjson '[{"listenport":"443","connectaddress":"10.1.10.20","connectport":"443","firewallrulename":"port443","direction":"Inbound","action":"Allow","protocol":"TCP"},{"listenport":"80","connectaddress":"10.1.10.20","connectport":"80","firewallrulename":"port80","direction":"Inbound","action":"Allow","protocol":"TCP"}]'

it works fine, I can see the json input is showing as

proxyinfosjson is [{"listenport":"443","connectaddress":"10.1.10.20","connectport":"443","firewallrulename":"port443","direction":"Inbound","action":"Allow","protocol":"TCP"},{"listenport":"80","connectaddress":"10.1.10.20","connectport":"80","firewallrulename":"port80","direction":"Inbound","action":"Allow","protocol":"TCP"}]

however, if I run

powershell -ExecutionPolicy Unrestricted -file testjson.ps1 -proxyinfosjson '[{"listenport":"443","connectaddress":"10.1.10.20","connectport":"443","firewallrulename":"port443","direction":"Inbound","action":"Allow","protocol":"TCP"},{"listenport":"80","connectaddress":"10.1.10.20","connectport":"80","firewallrulename":"port80","direction":"Inbound","action":"Allow","protocol":"TCP"}]'

it doesn't work, I can see json input is showing as

proxyinfosjson is [{listenport:443,connectaddress:10.1.10.20,connectport:443,firewallrulename:port443,direction:Inbound,action:Allow,protocol:TCP},{listenport:80,connectaddress:10.1.10.20,connectport:80,firewallrulename:port80,direction:Inbound,action:Allow,protocol:TCP}]

notice how all quotes are missing? as a result of that it couldn't convert from json why?

CodePudding user response:

powershell -ExecutionPolicy Unrestricted -file testjson.ps1 -proxyinfosjson '[{\"listenport\":\"443\",\"connectaddress\":\"10.1.10.20\",\"trimmed\":\"down\"}]'

If you you escape the double-quotes, like above, it seems to pass fine. Wasn't able to find a definitive article or forum post to explain, but... Seems arguments passed to the powershell.exe get treated to the same shell interpretation rules as cmd (windows command prompt). You have to escape(\) quotes there as well.

Here's a post I found that touches on it. Microsoft PowerShell.exe doc touches on it as well in the "-file" section.

Escape double quotes in parameter

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1

EDIT: Re: Arm Template Issues (Workaround)

A possible workaround would be to reinsert your parameter's quotations within the .ps1 script with -replace Regex... Like so.

param($proxyinfosjson)
write-output "INPUT proxyinfosjson is $proxyinfosjson"
$proxyinfosjson = $proxyinfosjson -replace '^\[\{','[{"';
$proxyinfosjson = $proxyinfosjson -replace ':','":"';
$proxyinfosjson = $proxyinfosjson -replace ',','","';
$proxyinfosjson = $proxyinfosjson -replace '\}\]$','"}]';
write-output "RESTORED proxyinfosjson is $proxyinfosjson"

Thinking those Regex patterns should cover all quotes in your string. Could possibly collapse those four "-replace" statements into a single, more detailed regex pattern, but that's beyond what I have time for today.

  • Related