gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:39:09 24 4
gpt4 key购买 nike

我正在构建一个使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com