5

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

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I have done what you suggested, and the error message that I can find it this: "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." Any ideas? Thanks
The only difference I found with my working code examples are: (1) use of the whole executable name "powershell.exe" (2) file name without single quotes (3) I'm still using type_handler_version 1.8. (1) and (2) should not be a problem. Give it a try with (3)
Ok, thanks I will try ruling out each of those, one by one.
Raul, looks like it all works if I remove the single quotes from the file name. Really not sure why that makes a difference, as the documentation suggests that they should be used, but... If you wanted to add this information to your answer, I will mark is as accepted. Thanks again!
3

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

6 Comments

I check custom script log, it seems Azure agent execute command 'powershell -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -File 'helloworld.ps1'', you could not use singe quotes again. imgur.com/a/p4NbD
Also, this is not a terraform issue. If you use Azure arm template, you will get the same error log.
Agreed. I have sent a PR to here: github.com/MicrosoftDocs/azure-docs/pull/5387 to correct the example that is shown in the documentation.
I will give a feedback to Azure, this official document have this issue. "commandToExecute": "powershell.exe . . . -File './scripts/myscript.ps1'" this is wrong.
I agree, that this documentation is wrong, and that is the PR that I have sent through to correct that documentation.
|
1

You should connect to the VM (via RDP, Powershell remote, or else) and look into the C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension folder for the logs.

I assume you have you the content Custom Script Extension for Windows.

2 Comments

Thanks, I found the logs based on the answer from Raul, however, it doesn't shed any light on the problem. Do you have any ideas? Thanks!
I have some working samples, but not on this PC; I can check tomorrow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.