使用 azurerm_virtual_machine_extension 时未执行 PowerShell 脚本
PowerShell Script is not executing when using azurerm_virtual_machine_extension
我正在尝试在我的 terraform 执行中使用以下内容:
resource "azurerm_virtual_machine_extension" "vmex" {
name = "myVM"
location = "eastus"
resource_group_name = "${azurerm_resource_group.rg.name}"
virtual_machine_name = "${azurerm_virtual_machine.vm.name}"
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
settings = <<SETTINGS
{
"fileUris": [
"https://example.com/scripts/test.ps1"
],
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File 'test.ps1'"
}
SETTINGS
}
执行时,我得到以下输出:
Error: Error applying plan:
1 error(s) occurred:
azurerm_virtual_machine_extension.vmex: 1 error(s) occurred:
azurerm_virtual_machine_extension.vmex: Long running operation terminated with status 'Failed': Code="VMExtensionProvisioningError" Message="VM has reported a failure when processing extension 'myVM'. Error message: \"Finished executing command\"."
Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.
当我通过 RDP 进入 VM 时,我可以看到文件已正确下载到它应该位于的位置,但是,似乎 commandToExecute
从未执行过。
如果我直接从下载文件夹 运行 脚本,它会成功完成。
任何人都可以就解决此问题的方法提供任何建议吗?
注意:我尝试了各种尝试引用该文件的组合,但它们似乎都有相同的结果。
更新:按照评论中的建议检查日志后,尝试 运行 这段代码时出现的错误是:
"message": "Processing -File ''test.ps1'' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again."
我有这样的问题,在我的情况下是与脚本和参数的内容有关。但是因为它可能是许多其他的东西,所以您需要检索一些额外的数据。
尝试了解扩展本身是否有更多信息。转到您的 Azure 帐户并遵循以下路径:主页 > 虚拟机 > [your_virtual_machine]。在显示所有选项的 blade 中,选择扩展以查看您的扩展及其状态(它应该是 "Provisioning failed"。单击该扩展。您将看到有关它的一些信息。检查状态和详细状态.
详细状态 是一个 link,它将向您显示一个 JSON,其中可以包含有关正在发生的事情的重要信息。可能它会包含来自您的 PS1 脚本的错误输出。
让我知道这是否有帮助,如果这些信息还不够,请post在这里,这样我可以获得更多信息,我将能够检查并尝试提供帮助。
此致,
劳尔.
编辑:所以在检查了我们的代码示例之间的差异之后,单引号最终导致了您的问题。很高兴您发现了问题。
您应该连接到 VM(通过 RDP、Powershell 远程或其他方式)并查看 C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension
文件夹中的日志。
根本原因是 commandToExecute
您不能使用单引号。您可以删除单引号或使用双引号(但您需要使用 \
)。如下图:
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File \"test.ps1\""
我正在尝试在我的 terraform 执行中使用以下内容:
resource "azurerm_virtual_machine_extension" "vmex" {
name = "myVM"
location = "eastus"
resource_group_name = "${azurerm_resource_group.rg.name}"
virtual_machine_name = "${azurerm_virtual_machine.vm.name}"
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
settings = <<SETTINGS
{
"fileUris": [
"https://example.com/scripts/test.ps1"
],
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File 'test.ps1'"
}
SETTINGS
}
执行时,我得到以下输出:
Error: Error applying plan:
1 error(s) occurred:
azurerm_virtual_machine_extension.vmex: 1 error(s) occurred:
azurerm_virtual_machine_extension.vmex: Long running operation terminated with status 'Failed': Code="VMExtensionProvisioningError" Message="VM has reported a failure when processing extension 'myVM'. Error message: \"Finished executing command\"."
Terraform does not automatically rollback in the face of errors. Instead, your Terraform state file has been partially updated with any resources that successfully completed. Please address the error above and apply again to incrementally change your infrastructure.
当我通过 RDP 进入 VM 时,我可以看到文件已正确下载到它应该位于的位置,但是,似乎 commandToExecute
从未执行过。
如果我直接从下载文件夹 运行 脚本,它会成功完成。
任何人都可以就解决此问题的方法提供任何建议吗?
注意:我尝试了各种尝试引用该文件的组合,但它们似乎都有相同的结果。
更新:按照评论中的建议检查日志后,尝试 运行 这段代码时出现的错误是:
"message": "Processing -File ''test.ps1'' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again."
我有这样的问题,在我的情况下是与脚本和参数的内容有关。但是因为它可能是许多其他的东西,所以您需要检索一些额外的数据。
尝试了解扩展本身是否有更多信息。转到您的 Azure 帐户并遵循以下路径:主页 > 虚拟机 > [your_virtual_machine]。在显示所有选项的 blade 中,选择扩展以查看您的扩展及其状态(它应该是 "Provisioning failed"。单击该扩展。您将看到有关它的一些信息。检查状态和详细状态.
详细状态 是一个 link,它将向您显示一个 JSON,其中可以包含有关正在发生的事情的重要信息。可能它会包含来自您的 PS1 脚本的错误输出。
让我知道这是否有帮助,如果这些信息还不够,请post在这里,这样我可以获得更多信息,我将能够检查并尝试提供帮助。
此致, 劳尔.
编辑:所以在检查了我们的代码示例之间的差异之后,单引号最终导致了您的问题。很高兴您发现了问题。
您应该连接到 VM(通过 RDP、Powershell 远程或其他方式)并查看 C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension
文件夹中的日志。
根本原因是 commandToExecute
您不能使用单引号。您可以删除单引号或使用双引号(但您需要使用 \
)。如下图:
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File \"test.ps1\""