0

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?

Roger Chen
  • 233
  • 3
  • 15
  • the first line is run in powershell so it's parsed using powershell rules. But the second command is run from cmd so obviously it's based on cmd rules, which doesn't support `''` strings, and the output will definitely be different because `"` is usually stripped off from parameters by the cmd argument splitter in the process – phuclv Mar 03 '22 at 00:51
  • thx@phuclv for the explanation – Roger Chen Mar 03 '22 at 05:26

1 Answers1

1
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://learn.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.

tanstaafl
  • 190
  • 5
  • thx man, however I was hoping that parameter can be passed from Arm template parameter, so which makes hard to escape all quotes.. – Roger Chen Mar 02 '22 at 21:25
  • Edited original answer to reply Re: Arm template restrictions – tanstaafl Mar 03 '22 at 15:13
  • 1
    Re: the -replace workaround -- the replace on colons and commas looks to work with the parameter string sample you shared... careful if any of your double-quote encapsulated text has colons or commas. You'd need different patterns – tanstaafl Mar 03 '22 at 15:26