powershell - ARM - 如何将带有空格的参数传递给 commandToExecute?

标签 powershell azure command

我正在构建一个使用 Azure 模板进行部署的 ARM 模板,以便它可以用作用户部署的“库存”镜像。要求之一是最终用户输入计算机描述作为参数。

参数:

"psVariable": {
  "value": "My Super Awesome Description"
}

我正在使用自定义脚本扩展来执行更改计算机描述的 PowerShell 脚本。

PowerShell 脚本:

Param ( [string] $psVariable )
New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\" -Name "srvcomment" -Value $psVariable -PropertyType String

自定义脚本扩展命令执行:

"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File ', variables('asdfcseScriptFolder'), '/', variables('asdfcseScriptFileName'), ' ', parameters('psVariable'))]"

当模板运行时,它确实运行 PowerShell 脚本,但将计算机命名为 My 并错过了Super Awesome Description。显然,如果我将 Parameter 更改为 My-Super-Awesome-Description (加入空格),它会将描述更改为完全相同的内容。但遗憾的是我需要空间。

我确实看过: How to escape single quote in ARM template

我尝试使用变量作为 "singleQuote": "'",并将 commandToExecute 更改为:

"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File ', variables('asdfcseScriptFolder'), '/', variables('asdfcseScriptFileName'), ' ', variables('singleQuote'), parameters('psVariable'), variables('singleQuote'))]"

但这只是将我的计算机描述更改为'我的

有人知道如何将带有空格的参数传递给commandToExecute吗?

最佳答案

正如 Bill 所说,commandToExecute 将是:

"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File \"', variables('asdfcseScriptFolder'), '/', variables('asdfcseScriptFileName'), '\" \"', parameters('psVariable'))]\""

这是一个json文件,\"转义了"。 例如: "{\"location\": {\"value\":\"westus\"}}" 转义 {"location": {"vaule": "westus"}}

我将此添加为答案,以便其他社区成员受益。

这里有类似案例,请引用answer .

关于powershell - ARM - 如何将带有空格的参数传递给 commandToExecute?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47163124/

相关文章:

Azure AD B2C 密码过期通知

linux - 我怎样才能显示正确答案?

powershell - 尝试发布电容器插件时,“rm”未被识别为内部或外部命令

http - 调用-RestMethod : The operation has timed out

powershell - $MyInvocation.MyCommand.Path 返回 NULL

azure - 重启后 - 在 Azure webrole 上安装证书

azure - 我可以通过自己的登录表单使用 Azure B2C 吗?

Linux 面向行的文本文件操作

MySQL - 如何创建复合表?

powershell - 获取子项 : Cannot find path 'env' because it does not exist