ARM - How to pass a parameter to commandToExecute with spaces?

你离开我真会死。 提交于 2021-02-10 06:12:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!