ARM Template - Custom Script Extension Error

pgaddam 151 Reputation points
2022-06-17T14:53:16.51+00:00

I face the following error while deploying my VMs to Az.

At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.  
Details:  
VMExtensionProvisioningError: VM has reported a failure when processing extension 'CustomScriptExtension'. Error message: "Command execution finished, but failed because it returned a non-zero exit code of: '1'. Command execution may have been interrupted by a VM restart"  
More information on troubleshooting is available at https://aka.ms/VMExtensionCSEWindowsTroubleshoot   
Check out the troubleshooting guide to see if your issue is addressed: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-resource-group-deployment?view=azure-devops#troubleshooting  
Task failed while creating or updating the template deployment.  

Below is the portion of Custom Script Extension being used:

"condition": "[equals(parameters('templateType'),'domainJoinAndPuppet')]",  
      "apiVersion": "2015-06-15",  
      "type": "Microsoft.Compute/virtualMachines/extensions",  
      "name": "[concat(variables('vmName'),'/CustomScriptExtension')]",  
      "location": "[parameters('location')]",  
      "dependsOn": [  
        "[concat('Microsoft.Compute/virtualMachines/', variables('vmName'))]"  
      ],  
      "properties": {  
        "publisher": "Microsoft.Compute",  
        "type": "CustomScriptExtension",  
        "typeHandlerVersion": "1.10",  
        "autoUpgradeMinorVersion": true,  
        "settings": {  
          "fileUris": [ "URL of PS Script 1",   
           "URL of PS Script2"  
          ]  
        },  
        "protectedSettings": {  
          "commandToExecute": "[variables('agentCommandToExecute')]"  
        }  
      }  
    },  

This script sometimes work and sometimes doesn't work at all.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,051 questions
0 comments No comments
{count} votes

Accepted answer
  1. dashanan13 930 Reputation points
    2023-05-23T18:34:32.58+00:00

    Hei @pgaddam (Pranith Sudhakar Gaddam) Thanks for reaching out to Microsoft QnA.
    Sorry for the delay in response.

    If this is a script then it is possible that the step before this execution leads to a restart and the execution starts before the VM recovers.

    The easiest way out of this would be to put a check that the VM is available, it is easy to do in powershell and azure cli script.

    Also you could use a Infra as a code solution like bicep or terraform to do this, these solutions are resilient.

    Please mark this as Answer , if it helped

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. srbhatta-MSFT 8,586 Reputation points Microsoft Employee
    2022-06-20T05:41:30.167+00:00

    Hello @Pranith-3606 ,
    Thanks for reaching out to Microsoft QnA.
    Firstly, apologies for the delay in responding here.
    Looking at the error details - "Command execution finished, but failed because it returned a non-zero exit code of: '1'. Command execution may have been interrupted by a VM restart", could you please confirm that the VM was not restarted by any other user? You can check the activity logs and get a confirmation on the same.
    There are some best practices that ideally should be followed while running custom script executions to make sure the script execution gets completed successfully. Could you please also refer to this - Best Practices.

    If you still see the same issue after going through the above two pointers and confirming that all things looks fine but even after that the script at times is not getting executed, then kindly let me know.

    Hope this helps. Thank You.

    ---------
    If the above information was useful then please accept as answer and upvote.

    1 person found this answer helpful.

  2. pgaddam 151 Reputation points
    2022-06-20T13:56:29.117+00:00

    Hello @srbhatta-MSFT

    There is joindomain extension that is running before this custom script extension. That is sending restart signal before the CSE could execute fully.

    Thanks
    Pranith


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.