Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Script is not executing when using azurerm_virtual_machine_extension

I am trying to use the following within my terraform execution:

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
}

When this executes, I get the following output:

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.

When I RDP into the VM, I can see that the file is correctly downloaded into the location that it is meant to be, however, it seems as though the commandToExecute is never executed.

If I run the script directly from the downloads folder, it successfully completes.

Can anyone offer any suggestions as to what to try to do to resolve this?

NOTE: I have tried various combinations of trying to reference the file, but all of them seem to have the same result.

UPDATE: After checking the logs as suggested in the comments, the error when trying to run this snippet was:

"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."

like image 575
Gary Ewan Park Avatar asked Mar 06 '18 12:03

Gary Ewan Park


2 Answers

I have that kind of problems and in my case was something related with the content of the script and parameters. But because it can be many other things you would need to retrieve some extra data.

Try to find out if you have more information in the extension itself. Go to your Azure account and follow the path: Home > Virtual Machines > [your_virtual_machine]. In the blade that appears with all the options choose Extensions to see your extension and its status (it should be "Provisioning failed". Click on that extension. You will see some information about it. Check Status and Detailed Status.

Detailed status is a link that will show you a JSON that can contain important information about what is going on. Probably it will contain an error output from your PS1 script.

Let me know if that helps and if that information is not enough, please, post it here so I can have more information and I would be able to check and try to help.

Regards, Raul.

EDIT: So after checking the differences between our code examples finally the single quotes were responsible for your problem. Glad you found the issue.

like image 155
Raul Vega Avatar answered Oct 05 '22 02:10

Raul Vega


The root reason is in commandToExecute you could not use single quotes. You could remove single quotes or use double quotes(but you need use \). Like below:

"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File \"test.ps1\""
like image 28
Shui shengbao Avatar answered Oct 05 '22 02:10

Shui shengbao