2

currently I'm using custom script extension to run scripts on demand on my azure vm server as part of our software solution, our other dev team is moving an application to a scale set and i am no longer capable of deploying custom script extension on demand to the scale set instances. the only solution i have found for running custom script extension on scale set instances is to reconfigure the deployment template with it, this method is not good for me as the scripts should be run on demand and are changed frequently and updating the template every time is bad practice.

Is there anyway to configure custom script extension on scale set instances on demand like on regular virtual machines?

powershell example for regular on demand script deployment on vm:

Set-AzureRmVMCustomScriptExtension -ResourceGroupName myResourceGroup `
-VMName myVM `
-Location myLocation `
-FileUri myURL `
-Run 'myScript.ps1' `
-Name DemoScriptExtension
VodkaPlease
  • 31
  • 1
  • 5

2 Answers2

5

I found a workaround for this using PowerShell and ARM JSON templates (I'm using Powershell version 5.1). In commandToExecute under virtualMachineProfile in your json template, specify a value that almost always changes and it will force the command to re-execute to run every time your template is deployed. You will see in my template that I added: ' -Date ', deployment().name to the commandToExecute. The value for deployment().name is specified in my New-AzureRmResourceGroupDeployment command as:

-Name $($(Get-Date -format "MM_dd_yyyy_HH_mm"))

The deployment name is based on the date and time, which will be different per minute.

PowerShell Command:

New-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName -TemplateFile $PathToJsonTemplate -TemplateParameterFile $PathToParametersFile -Debug -Name $($(Get-Date -format "MM_dd_yyyy_HH_mm")) -force

The custom script extension section under virtualMachineProfile in my script appears as such (pay attention to the commandToExecute):

"virtualMachineProfile": {
                    "extensionProfile": {
                        "extensions": [
                            {
                                "type": "Microsoft.Compute/virtualMachines/extensions",
                                "name": "MyExtensionName",
                                "location": "[parameters('location')]",
                                "properties": {
                                    "publisher": "Microsoft.Compute",
                                    "type": "CustomScriptExtension",
                                    "typeHandlerVersion": "1.8",
                                    "autoUpgradeMinorVersion": true,
                                    "settings": {
                                        "fileUris": [
                                            "[concat(parameters('customScriptExtensionSettings').storageAccountUri, '/scripts/MyScript.ps1')]"
                                        ],
                                        "commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File MyScript.ps1', ' -Date ', deployment().name)]"
                                    },
                                    "protectedSettings": {
                                        "storageAccountName": "[parameters('customScriptExtensionSettings').storageAccountName]",
                                        "storageAccountKey": "[listKeys(variables('accountid'),'2015-05-01-preview').key1]"
                                    }
                                }
                            },

This will allow you to update a Custom Script Extension on a Virtual Machine Scale Set that has already been deployed. I hope this helps!

ieelleme
  • 51
  • 1
  • 3
-1

Is there anyway to configure custom script extension on scale set instances on demand like on regular virtual machines?

For now, Azure does not support this.

We only can use VMSS custom script to install software at the time the scale set is provisioned.

More information about VMSS extension, please refer to this link.

Jason Ye
  • 13,710
  • 2
  • 16
  • 25