How to Use Powershellscript in Azure classic Release pipeline - script file stored in Azure Devops Secure File

336 views Asked by At

I am using a custom script extension for VM in ARM Template:

         {
   "type": "Microsoft.Compute/virtualMachines/extensions",
 "name": "[concat(parameters('vm-Name'),'-0',copyIndex(1),'/script')]",
   "apiVersion": "2015-05-01-preview",
   "location": "[resourceGroup().location]",
      "copy": {
                "name": "storagepoolloop",
                "count": "[parameters('virtualMachineCount')]"
            },
   "dependsOn": [
       "virtualMachineLoop",
       "nicLoop"
   ],
   "properties": {
       "publisher": "Microsoft.Compute",
       "type": "CustomScriptExtension",
       "typeHandlerVersion": "1.4",
       "settings": {
           "fileUris": [

       ],
      "commandToExecute": "[parameters('commandToExecute')]"
     }
   }
 }

where parameters = "powershell.exe $(Agent.TempDirectory)/$(script.secureFilePath)"

I am using azure devops secure files to store my script. I have Download a secure file task before deploying the vm. I have also tried directly referencing script file name "powershell.exe $(Agent.TempDirectory)/puscript.ps1"

I am using classic Release pipeline, if this is not the right way please guide how to use powershell script stored in secure files.

Any help is appreciated. Thanks in advance.

2

There are 2 answers

1
Bright Ran-MSFT On

You can try like as below steps:

  1. Use the Download Secure File task to download the PowerShell script file. On the task, set a Reference name for use.

    enter image description here

  2. Use the PowerShell task (or Azure PowerShell task) to execute the PowerShell script.

    enter image description here

Consider you want execute the PowerShell script to run ARM Template deployment, you could use the Azure PowerShell task.

0
joelforsyth On

The script will need to be downloaded on to the VM you're creating, not downloaded onto the machine that is deploying the ARM. That command does not actually get executed until the VM starts the extension, so the variable $(Agent.TempDirectory) refers to the directory on the machine executing the pipeline and won't exist when the VM starts up.

I did the same thing for a VM custom extension by including the script in the image that I was using to create the VM. If you're not using a custom image, you can add the storage account information to download it in the protectedSettings like this:

"protectedSettings": {
    "commandToExecute": "powershell.exe puscript.ps1",
    "storageAccountName": "yourstorageaccount",
    "storageAccountKey": "<account key>",
    "fileUris": [
        "https://yourstorageaccount.blob.core.windows.net/container/puscript.ps1"
    ]
}

ref: https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows#extension-schema