Custom Data with Azure Windows VM run PowerSell Script

Viewed 3334

I am trying to download and install an exe during the provisioning of a Windows VM in Azure cloud. I do not want to use Custom Script Extension but instead I want to use "Custom Data". I cannot find any solid examples on Azure documentation.

In AWS, I found enough resources and I could develop the below PowerShell script and add it to the User Data but that doesn't work on Azure, I tried different variations but with no luck. Has anyone done that before? Is there any clear documentation on that? I read Azure uses Cloud-init but again, no clear examples on how to do that with Cloud-init for a Windows machine, all examples are for Linux.

<powershell>
start-transcript
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest 'https://www.domain-name.com/filename.exe' -OutFile C:\filename.exe
C:\filename.exe --quiet
</powershell>

Any help would be appreciated.

1 Answers

You can inject data into a Windows virtual machine on Azure, but you can't execute it using custom data or cloud init unfrotunately. Execution of the custom data using cloud init is only supported in Ubuntu images.

Source: https://azure.microsoft.com/es-es/blog/custom-data-and-cloud-init-on-windows-azure/

To achieve an execution of a script post provisioning, it depends on how you're provisioning the VM.

  1. In ARM templates you can use custom script extensions: https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows
  2. Using Azure CLI you can execute a script using az vm run-command like this:
az vm run-command invoke --command-id RunPowerShellScript --name win-vm -g my-resource-group  \
   --scripts 'param([string]$arg1)' \
   'Write-Host Hello' \
   --parameters 'arg1=kemety'

Sourced from here: https://docs.microsoft.com/en-us/cli/azure/vm/run-command?view=azure-cli-latest

Related