Deploying virtual machines in Microsoft Azure is straightforward; however, post-deployment configuration often requires manual steps. Whether it’s setting the time zone, installing apps, or making registry changes, repeating these tasks across multiple VMs can quickly become tedious.
In this guide, we’ll explore how to use Azure Custom Script Extensions to automate configuration on both new and existing Windows-based Azure VMs. You’ll learn how the extension works, how to prepare a PowerShell script, and how to deploy it through the Azure portal. This approach can save valuable time and help enforce consistency across your environment.
What Is a Custom Script Extension?
Azure Custom Script Extensions let you download and run scripts on an Azure VM post-deployment. PowerShell is used for Windows VM’s, Bash for Linux. This enables the automation of OS-level configuration, application installation, and other tasks that are typically handled manually.
Key characteristics:
- Works on new and existing VMs (including VM scale sets).
- Compatible with deployments through the Azure Portal, ARM templates, Bicep, PowerShell, and the Azure CLI.
- Scripts run under the Local System account without user interaction.
- Scripts must be idempotent (safe to run multiple times).
Why Use It?
Using a Custom Script Extension is ideal for:
- Applying consistent configurations across test or production environments.
- Installing apps like Chrome using package managers such as Chocolatey.
- Automating one-time setup tasks on new servers.
- Reducing human error from manual configuration.
Best Practices for Scripting
Before uploading your script:
- Test locally first: Ensure it runs without errors.
- Add logging: Include verbose output and error handling.
- Avoid restarts: Don’t reboot the VM, stop services, or update the VM agent inside the script.
- Scripts have a 90-minute timeout window, which is usually sufficient for most tasks.
PowerShell Script Overview
The PowerShell script used in the accompanying video is available from the links below. The script is provided for demonstration purposes. The functionality may not align with your environment’s needs, but the examples can be used to develop your own.
- Defines logging and download directories.
- Sets the system time zone.
- Updates registry entries for organization and owner (as an example).
- Downloads and extracts BGInfo from Sysinternals.
- Adds a BGInfo shortcut to the startup folder.
- Installs Chrome using Chocolatey.
The script uses extensive try/catch blocks for error handling and logs output to a file for easier troubleshooting. The output is available in the log directory configured in the script.
The script is hosted on an Azure storage account. Additional resources used by the script can be downloaded at runtime from the same storage account securely with a SAS-based URL.
Deploying the Script on an Existing VM
- Go to the existing VM in the Azure Portal.
- Navigate to Extensions + Applications.
- Click Add, then choose Custom Script Extension.
- Browse to the storage account and select the script.
- Complete the wizard and click Create.
After deployment, log in to the VM to verify results. Check for installed apps, changed settings, and view the log file output created by the script.
Script Logs
The Custom Script Extension generates logging information in addition to the log files generated by the script. Those logs are located on the VM in the following directories:
C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension
C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.*\Downloads\<n>
Using the Extension During a New VM
When deploying a new VM:
- In the Azure Portal from the new VM wizard, go to the Advanced tab.
- Under Extensions, select Custom Script Extension.
- Browse and attach the script from your storage container.
- Complete the VM creation.
After the VM boots, the script will automatically execute.
Troubleshooting Tips
If the extension fails:
- Check logs in the plugin directory.
- Review your script for missing error handling or interactive prompts.
- Ensure URLs to hosted files are valid and accessible.
- Make sure any PowerShell commands are compatible with Windows Server Core if using that image.
Summary
Azure Custom Script Extensions are a powerful tool for automating the configuration of Windows VMs. By preparing a reusable PowerShell script and integrating it into your deployment process, you can save time, enforce consistency, and reduce errors in your Azure environment.
Whether you’re configuring a single VM or scaling across a fleet, Custom Script Extensions can simplify your workflow.
Links
A Beginner’s Guide to the AZ-900
https://www.udemy.com/course/beginners-guide-az-900/?referralCode=C74C266B74E837F86969
Zero to Hero with Azure Virtual Desktop
https://www.udemy.com/course/zero-to-hero-with-windows-virtual-desktop/?referralCode=B2FE49E6FCEE7A7EA8D4
Hybrid Identity with Windows AD and Azure AD
https://www.udemy.com/course/hybrid-identity-and-azure-active-directory/?referralCode=7F62C4C6FD05C73ACCC3
Windows 365 Enterprise and Intune Management
https://www.udemy.com/course/windows-365-enterprise-and-intune-management/?referralCode=4A1ED105341D0AA20D2E
Link to the code on GitHub
https://github.com/tsrob50/CiraltosTools