问题
I am building an ARM template that uses Azure templates for deployment, so that it can be used as 'stock' image for users to deploy. One of the requirements is that an end user inputs the computer description as a parameter.
Parameter:
"psVariable": {
"value": "My Super Awesome Description"
}
I'm using a Custom Script Extension to execute a PowerShell script that changes the computer description.
PowerShell Script:
Param ( [string] $psVariable )
New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\" -Name "srvcomment" -Value $psVariable -PropertyType String
Custom Script Extension commandToExecute:
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File ', variables('asdfcseScriptFolder'), '/', variables('asdfcseScriptFileName'), ' ', parameters('psVariable'))]"
When the template runs it does run the PowerShell script, but names the computer My
and misses Super Awesome Description
. Obviously if I change my Parameter
to My-Super-Awesome-Description
(joining the spaces) it will change the description to exactly that. But sadly I need the spaces.
I did look at: How to escape single quote in ARM template
I tried to use a variable as "singleQuote": "'"
, and change the commandToExecute
to:
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File ', variables('asdfcseScriptFolder'), '/', variables('asdfcseScriptFileName'), ' ', variables('singleQuote'), parameters('psVariable'), variables('singleQuote'))]"
But this only changed my computer description to 'My
Does anyone know how to pass a parameter to commandToExecute with spaces?
回答1:
As Bill said, The commandToExecute
would be:
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File \"', variables('asdfcseScriptFolder'), '/', variables('asdfcseScriptFileName'), '\" \"', parameters('psVariable'))]\""
It is a json file, \"
escapes "
.
For example: "{\"location\": {\"value\": \"westus\"}}"
escapes {"location": {"vaule": "westus"}}
I add this as an answer so that other community members will be benefited.
Here a similar case, please refer to the answer.
来源:https://stackoverflow.com/questions/47163124/arm-how-to-pass-a-parameter-to-commandtoexecute-with-spaces