为什么我的CloudFormation命令失败?

时间:2013-08-18 09:02:21

标签: json amazon-web-services amazon-cloudformation

我的命令:

"7-set-up-default" : {
        "command" : { "Fn::Join" : ["", [
                                "robocopy /move C:\\inetpub\\wwwroot\\ C:\\inetpub\\wwwroot\\Default\\\n",
                                "powershell \"Import-Module WebAdministration;",
                                             "Set-ItemProperty 'IIS:\\Sites\\Default Web Site' -name physicalpath -value 'C:\\inetpub\\wwwroot\\Default\\'\""
                            ]]},
        "waitAfterCompletion" : "0"
      },

这个不太有用的堆栈跟踪失败:

Error encountered during build of config: Command 07-set-up-default 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 07-set-up-default failed
2013-08-18 08:35:29,611 [ERROR] Unhandled exception during build: Command 07-set-up-default failed
Traceback (most recent call last):
  File "cfn-init", line 122, 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 07-set-up-default failed

但是,将我的命令直接粘贴到cmd窗口中可以正常工作:

robocopy /move C:\inetpub\wwwroot\ C:\inetpub\wwwroot\Default\
powershell "Import-Module WebAdministration;Set-ItemProperty 'IIS:\Sites\Default Web Site' -name physicalpath -value 'C:\inetpub\wwwroot\Default\'"

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

因为robocopy不使用标准返回码。它实际上并没有失败,但命令行将其返回代码解释为失败。请参阅此Robocopy return code reference

您需要做的是包装robocopy并解释返回值。

"a-sync" : {
  "command" : {"Fn::Join" : ["",[
  "(robocopy c:\\latest c:\\inetpub\\", {"Ref" : "Name"}, " /MIR) ^& IF %ERRORLEVEL% LEQ 4 exit /B 0"
  ]]},
  "waitAfterCompletion" : "1"
},

这是从一个有效的sript剪切和粘贴,因此你需要更改参数 - 但关键部分是将robocopy命令包含在括号中,并且在^&amp;如果最后的东西。

实际上,它将所有成功返回代码从robocopy转换为操作系统识别的成功代码。