Beefcake
Beefcake

Reputation: 801

Azure ARM template deployment via CLI failing with .ps1 script extension error

I have been deploying my ARM template via Visual Studio with a custom script extension (.ps1) just fine, it was actually compiled via VS too. However, I've been trying to deploy it via CLI with a modification to the .ps1 file being on an Azure Storage Location so it can be deployed by others who don't have VS. However, each time I do a deployment it fails, errors that the script doesn't have a .ps1 extension.

The custom script extension part of my ARM template:

      "name": "formatDataDisk",
      "type": "extensions",
      "location": "[resourceGroup().location]",
      "apiVersion": "2016-03-30",
      "dependsOn": [
        "[resourceId('Microsoft.Compute/virtualMachines', parameters('vmName'))]"
      ],
      "tags": {
        "displayName": "formatDataDisk"
      },
      "properties": {
        "publisher": "Microsoft.Compute",
        "type": "CustomScriptExtension",
        "typeHandlerVersion": "1.4",
        "autoUpgradeMinorVersion": true,
        "settings": {
          "fileUris": [
            "https://--MYSTORAGEACCOUNTNAME--.blob.core.windows.net/customscript/formatDataDisk.ps1"
          ],
          "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -File './customscript/formatDatadisk1.ps1'"
        },
        "protectedSettings": {
          "storageAccountName": "--MYSTORAGEACCOUNTNAME--",
          "storageAccountKey": "--MYSTORAGEKEY--"
        }

At the end of the CLI deployment it fails with:

msrest.http_logger : b'{"status":"Failed","error":{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.","details":[{"code":"Conflict","message":"{\\r\\n  \\"status\\": \\"Failed\\",\\r\\n  \\"error\\": {\\r\\n    \\"code\\": \\"ResourceDeploymentFailure\\",\\r\\n    \\"message\\": \\"The resource operation completed with terminal provisioning state \'Failed\'.\\",\\r\\n    \\"details\\": [\\r\\n      {\\r\\n        \\"code\\": \\"VMExtensionProvisioningError\\",\\r\\n        \\"message\\": \\"VM has reported a failure when processing extension \'formatDataDisk\'. Error message: \\\\\\"Finished executing command\\\\\\".\\"\\r\\n      }\\r\\n    ]\\r\\n  }\\r\\n}"}]}}'
    msrest.exceptions : At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.
    Deployment failed. {
      "status": "Failed",
      "error": {
        "code": "ResourceDeploymentFailure",
        "message": "The resource operation completed with terminal provisioning state 'Failed'.",
        "details": [
          {
            "code": "VMExtensionProvisioningError",
            "message": "VM has reported a failure when processing extension 'formatDataDisk'. Error message: \"Finished executing command\"."
          }

Azure Portal shows this error in the custom script extensions:

[
    {
        "code": "ComponentStatus/StdOut/succeeded",
        "level": "Info",
        "displayStatus": "Provisioning succeeded",
        "message": ""
    },
    {
        "code": "ComponentStatus/StdErr/succeeded",
        "level": "Info",
        "displayStatus": "Provisioning succeeded",
        "message": "Processing -File ''./customscript/formatDatadisk1.ps1'' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again."
    }
]

I have tried:

Any guidance will be much appreciated

Upvotes: 1

Views: 1258

Answers (4)

wonderwall
wonderwall

Reputation: 142

I am not sure If its related to this particular problem or not but what I have noticed in last 3-4 days working on Azure Custom Extension is that If your script fails once and your ARM template deployment of Custom Extension fails then you will have to manually remove the custom extension from the VM under the extension blade from Azure portal on which you are trying to run the script. If you do not do this no matter what changes you made to your script the custom extension will keep on running the old script.

Upvotes: 0

Shui shengbao
Shui shengbao

Reputation: 19195

When I test your template in my lab, I get same error log with you. When I remove ./customscript/, it works for me.

  "resources": [
    {
      "name": "[concat(parameters('virtualMachineName'), '/', 'shuitest')]",
      "type": "Microsoft.Compute/virtualMachines/extensions",
      "location": "eastus",
      "apiVersion": "2016-03-30",
      "dependsOn": [ ],
      "tags": {
        "displayName": "shuitest"
      },
      "properties": {
        "publisher": "Microsoft.Compute",
        "type": "CustomScriptExtension",
        "typeHandlerVersion": "1.4",
        "autoUpgradeMinorVersion": true,
        "settings": {
          "fileUris": ["https://shuiwindisks928.blob.core.windows.net/vhds/open_port.ps1"]

        },
        "protectedSettings": {
          "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File open_port.ps1",
          "storageAccountName" : "shuiwindisks928",
          "storageAccountKey" : "jvYg+1aCo+d4b+FI/kdBOtE*******************+kXa0yZR5xrt7a57qgHw=="
        }
      }
    }],

Update:

You need check extension log on VM, please refer to this link.

Upvotes: 0

Beefcake
Beefcake

Reputation: 801

Finally, I have resolved the issue with the help of the other comments. I tested with a more simple helloworld.ps1 script, and it worked, so obviously there was an issue with my powershell script. When creating it in Visual Studio it placed the following at the top:

<# Custom Script for Windows #>

Followed by the rest of the script. Once I removed that and re-uploaded to my StorageAccount/Container, and removed ./ in commandToExecute, it was fine.

Thanks for the feedback.

Upvotes: 1

4c74356b41
4c74356b41

Reputation: 72171

Wrong path, you need to omit the container in path:

"commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -File './formatDatadisk1.ps1'"

It always puts the downloaded file in the same directory, without nested folders

here's the exact snippet that works for me:

"properties": {
    "publisher": "Microsoft.Compute",
    "type": "CustomScriptExtension",
    "typeHandlerVersion": "1.8",
    "autoUpgradeMinorVersion": true,
    "settings": {
        "fileUris": [
            "https://backupcicd.blob.core.windows.net/deployment/iis-preConfigureScript.ps1"
        ],
        "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File iis-preConfigureScript.ps1"
    }
}

Upvotes: 0

Related Questions