如何在json文件中使用Powershell语法?

时间:2019-06-05 13:58:35

标签: json powershell

我正在尝试在用于cloudformation模板的json文件中运行powershell命令。我在使用竖线(|)符号后出现任何错误。

这是一个需要用于我们模板的新脚本。

"commands": {
                        "UpdateAbsolutePath": {
                            "command": "powershell.exe -Command ((Get-Content -path D:\\Tomcat-8.0\\webapps\\lease_accelerator\\WEB-INF\\web.xml -Raw) -Replace \"LA-AWS-SU1-APP1\",\"LA-AWS-SU2-APP1\") | Set-Content -Path D:\\Tomcat-8.0\\webapps\\lease_accelerator\\WEB-INF\\web.xml"
                        }

我希望它运行正常,但是出现以下错误:

[DEBUG] No services specified
2019-06-05 13:49:01,572 [DEBUG] Running command UpdateAbsolutePath
2019-06-05 13:49:01,588 [DEBUG] No test for command UpdateAbsolutePath
2019-06-05 13:49:01,618 [ERROR] Command UpdateAbsolutePath (powershell.exe -Command ((Get-Content -path D:\Tomcat-8.0\webapps\lease_accelerator\WEB-INF\web.xml -Raw) -Replace "LA-AWS-SU1-APP1","LA-AWS-SU2-APP1") | Set-Content -Path D:\Tomcat-8.0\webapps\lease_accelerator\WEB-INF\web.xml) failed
2019-06-05 13:49:01,618 [DEBUG] Command UpdateAbsolutePath output: 'Set-Content' is not recognized as an internal or external command,
operable program or batch file.

2019-06-05 13:49:01,618 [ERROR] Error encountered during build of config: Command UpdateAbsolutePath failed
Traceback (most recent call last):
File "cfnbootstrap\construction.pyc", line 513, in run_config
File "cfnbootstrap\construction.pyc", line 125, in run_commands
File "cfnbootstrap\command_tool.pyc", line 113, in apply
ToolError: Command UpdateAbsolutePath failed
2019-06-05 13:49:01,618 [ERROR] -----------------------BUILD FAILED!----- 
-------------------
2019-06-05 13:49:01,618 [ERROR] Unhandled exception during build: Command UpdateAbsolutePath failed
Traceback (most recent call last):
File "cfn-init", line 123, in <module>
File "cfnbootstrap\construction.pyc", line 117, in build
File "cfnbootstrap\construction.pyc", line 502, in build
File "cfnbootstrap\construction.pyc", line 513, in run_config
File "cfnbootstrap\construction.pyc", line 125, in run_commands
File "cfnbootstrap\command_tool.pyc", line 113, in apply
ToolError: Command UpdateAbsolutePath failed`

1 个答案:

答案 0 :(得分:2)

将命令括在双引号中,或在管道符号前添加^

一些有效和无效的示例:

> powershell -Command Write-Output "OK" | Out-String
'Out-String' is not recognized as an internal or external command, operable program or batch file.
> powershell -Command Write-Output "OK" ^| Out-String
OK
> powershell -Command 'Write-Output "OK" | Out-String'
'Out-String' is not recognized as an internal or external command, operable program or batch file.
> powershell -Command "Write-Output `"OK`" | Out-String"
OK
> powershell -Command { Write-Output "OK" | Out-String }
'Out-String' is not recognized as an internal or external command, operable program or batch file.
> powershell -Command "& { Write-Output "OK" | Out-String}"
OK