What Does Custom script extension Mean?
On This Page
Quick Definition
The Custom Script Extension lets you run scripts on an Azure virtual machine after it is created. You can use it to install software, apply settings, or run any command. It is often used to configure a VM automatically after it starts for the first time. The extension handles downloading the script files and executing them on the VM.
Commonly Confused With
DSC is a configuration management platform that continuously ensures a VM stays in a desired state, correcting drift automatically. Custom Script Extension runs a script once only. DSC is for ongoing compliance, while the extension is for one-time setup or on-demand fixes.
Use Custom Script Extension to install an application during VM creation. Use DSC to ensure that application is always running and configured correctly.
An Automation Runbook runs in Azure Automation, not on the VM itself. It can run scripts against multiple VMs from a central service. Custom Script Extension runs directly on the VM. Use runbooks for cross-VM orchestration and extensions for per-VM configuration.
Use a runbook to check the disk space on 100 VMs. Use Custom Script Extension to install a specific security update on one VM.
The VM Agent is a background service that allows Azure to communicate with the VM and enables extensions to run. The Custom Script Extension is one of many extensions that the VM Agent can execute. The agent is the platform; the extension is a tool that runs on that platform.
The VM Agent must be installed and running for the Custom Script Extension to work. If the agent is disabled, no extensions will function.
Must Know for Exams
The Custom Script Extension is a core topic in the AZ-104 Microsoft Azure Administrator exam. It falls under the "Manage Azure virtual machines" objective, specifically covering post-deployment configuration and automation. Expect questions that test your understanding of when to use the Custom Script Extension versus other automation tools like Desired State Configuration (DSC) or Azure Automation.
Exam questions often present a scenario where a company deploys multiple VMs and needs to install a specific application on each one after provisioning. You must know that the Custom Script Extension is the correct tool for running a one-time script, while DSC is better for ongoing configuration management. Another common question type involves troubleshooting: you are given an error where the script did not run, and you need to identify the cause, such as incorrect storage account permissions, wrong file URL, or the VM agent not being installed.
The exam also tests your ability to configure the extension using ARM templates, Azure CLI, or PowerShell. You might see a question asking for the correct syntax to deploy the extension via ARM template, including the fileUris and commandToExecute properties. The exam covers the difference between the Windows and Linux versions of the extension, including the correct extension names and script types.
You should know that the Custom Script Extension can be applied to an existing VM using the portal, CLI, or PowerShell, and that you can monitor its status in the Azure portal or via the Azure CLI. The exam may also test your knowledge of the extension's limitations, such as the fact that it does not handle dependencies between scripts automatically, and that scripts must be self-contained or download their dependencies. Another important nuance is that for Linux VMs, the extension runs as root, so you need to be careful with permissions.
The exam expects you to know how to pass parameters to the script and how to retrieve the script output for debugging. Questions about scaling and automation often combine the Custom Script Extension with Virtual Machine Scale Sets, where you use it in the scale set's extension profile to ensure new instances are configured automatically.
Simple Meaning
Imagine you have a brand new computer that arrived in a box. It has the operating system installed, but you still need to install your applications, set up your user accounts, and configure your network settings. Instead of logging in and doing all of that manually, you could hand a flash drive with a list of instructions to someone who will plug it in and follow every step automatically.
In Azure, the Custom Script Extension is like that flash drive and the helpful assistant combined. You tell Azure which scripts to run on your virtual machine, and the extension downloads the scripts from a storage location (like Azure Blob Storage or GitHub) and runs them on the VM. This happens after the VM is provisioned, so you can ensure every new VM has the same software and configuration without any manual effort.
The script can install antivirus, set up a web server, configure firewall rules, or even join the VM to a domain. It is a powerful tool for automating the setup and ongoing management of your VMs, saving you time and reducing human errors. The extension runs only once by default, but you can trigger it again if you need to update the VM later.
Because it runs as a system process, it can execute commands that require administrator privileges.
Full Technical Definition
The Custom Script Extension is a virtual machine (VM) extension in Microsoft Azure that enables post-deployment configuration and software installation on both Windows and Linux VMs. It is part of the Azure VM Extensions ecosystem, which includes agents installed on each VM that communicate with the Azure platform. For Windows VMs, the extension is called CustomScriptExtension and runs PowerShell scripts.
For Linux VMs, it is called customScript, and it runs bash scripts. The extension works by downloading script files from a source such as Azure Blob Storage, GitHub, or a public URL, then executing the primary script with any specified arguments. It can also download multiple files if needed.
The script output and errors are written to specific log files: for Windows, the log is located at C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension and for Linux at /var/lib/waagent/custom-script.
The extension supports specifying command arguments, file URLs, and storage account credentials via SAS tokens or keys. It is invoked during VM provisioning or can be applied to existing VMs through the Azure portal, CLI, PowerShell, ARM templates, or REST API. The extension runs as a local system service, so it has full administrative permissions on the VM.
It can install software, configure operating system settings, modify registry keys, create user accounts, and perform any command that the operating system supports. The execution is handled by the Azure VM Agent, which manages communication between the VM and Azure Fabric. The agent ensures that the script is downloaded securely and executed reliably.
The extension status is reported back to Azure, allowing you to check whether the script succeeded or failed. For Windows, the extension uses the PowerShell execution policy to run scripts, and for Linux, it uses the standard shell interpreter. The extension can be used in conjunction with Azure Policy to ensure compliance, and with Azure Automation State Configuration for more complex configuration management.
The Custom Script Extension is idempotent if the script is written carefully; otherwise, running it multiple times can cause side effects. It does not uninstall software or revert changes automatically.
Real-Life Example
Think about moving into a new apartment. The apartment comes with basic walls, floors, and plumbing, but you need to set up your furniture, connect your appliances, and hang your pictures. If you had to do this for dozens of identical apartments for a hotel chain, it would take forever and you might miss something in each room.
Instead, you write a detailed checklist for each room: "Place the bed frame, attach the headboard, plug in the mini-fridge, hang the TV on the wall, set the thermostat to 72 degrees." Then you give this checklist to a team of workers who go into each apartment and follow it exactly. In Azure, the Custom Script Extension is that checklist and the workers combined.
When you create a new virtual machine, you can attach a script that says: "Install the web server, enable the firewall, create a user called webadmin, and set the time zone to Eastern Time." The extension downloads your script and runs it automatically as soon as the VM is ready. Just like the worker who follows the checklist, the extension does exactly what you told it without any deviation.
If the checklist says to install the mini-fridge on the left side of the room, the worker will do it exactly that way every time. Similarly, if your script installs antivirus, every VM will have the same antivirus configuration. This consistency is crucial in IT environments where you need every server to be identical and compliant with company policies.
Why This Term Matters
In real-world IT operations, manually configuring each server is inefficient and error-prone. The Custom Script Extension matters because it allows you to automate post-deployment tasks, ensuring consistency across hundreds or thousands of VMs. When you scale up your infrastructure, you cannot afford to log into each machine and install software or apply settings by hand.
With the extension, you define a script once and run it on every new VM automatically. This reduces the risk of configuration drift, where servers that should be identical develop subtle differences over time. It also speeds up deployment; instead of taking hours to set up a server, the extension can complete the setup in minutes.
The extension is also critical for security compliance. You can use it to enforce security settings such as enabling Windows Defender, applying firewall rules, or installing security updates immediately after a VM is provisioned. It integrates with CI/CD pipelines, so you can include custom script execution as part of an automated deployment workflow.
For example, when a new application version is released, you can run a script on the VM to update the application binaries. The extension supports both Windows and Linux, making it a universal tool in hybrid environments. IT professionals also use it for troubleshooting; if a VM needs a quick fix, you can run a script remotely to apply a registry change or restart a service without RDP or SSH.
The extension logs provide visibility into what was executed, which helps with auditing and debugging. Without the Custom Script Extension, managing Azure VMs at scale would be much more complex and time-consuming.
How It Appears in Exam Questions
AZ-104 exam questions about Custom Script Extension typically fall into several patterns. One common scenario question describes a company deploying 50 Windows VMs for a web application. Each VM needs IIS installed and the default website replaced with a custom application.
The question asks which method to use for automated post-deployment configuration. The correct answer is the Custom Script Extension because it can run a PowerShell script to install IIS and copy the application files. Another question might involve troubleshooting a failed extension deployment.
The scenario mentions that the script output shows an error that a file could not be downloaded. The question asks for the most likely cause. Options could include that the storage account key expired, the blob URL is incorrect, the VM has no internet access, or the extension is not supported on that OS.
The correct answer is usually related to access permissions or network connectivity. A third pattern tests the difference between provisioning a new VM and applying the extension to an existing VM. For example, a question might ask: "You have an existing VM that needs to run a configuration script.
Which tool should you use?" The answer is the Custom Script Extension, but learners might confuse it with Azure Automation or Azure Policy. Another configuration-style question provides an ARM template snippet with the extension resource type, and asks you to identify the missing property that specifies which script to run.
The answer is "fileUris" or "commandToExecute". Some questions ask about the extension's behavior when running multiple times. For example, if a script creates a file each time it runs, you need to know that running it again will execute the script again, potentially creating duplicate files unless the script is written idempotently.
Troubleshooting questions might show a Linux VM where the script failed because bash was not installed, or because the script shebang lines were incorrect. You should also expect questions that test the sequence of operations: the extension runs after the VM is provisioned and the agent is running, but before the VM is fully usable. Knowing that the extension execution order can be controlled via dependencies in ARM templates is an advanced point that could appear in scenario-based questions.
Finally, the exam may combine the Custom Script Extension with Virtual Machine Scale Sets, asking how to ensure every new instance in the scale set gets the same configuration.
Practise Custom script extension Questions
Test your understanding with exam-style practice questions.
Example Scenario
A company called CloudVault needs to deploy 10 Windows Server 2022 VMs to host a new document management application. The IT team has already created a base VM image with the operating system, but they need to install the application, configure the firewall to open port 443, and set the local admin password to a secure value. Using the Azure portal, the administrator creates a new VM and, in the "Advanced" tab, selects "Custom Script Extension" and uploads a PowerShell script.
The script does three things: it downloads the application installer from a storage account, runs the installer with silent parameters, and then modifies the Windows Firewall rule. The administrator also adds a second script that runs a validation command to ensure the application is running. After the VM is provisioned, the extension automatically downloads and executes the script.
The administrator can see the script execution status in the Azure portal under the VM's extensions blade. Later, the company decides to apply the same script to the remaining 9 VMs. Instead of creating each VM from scratch, the administrator uses Azure PowerShell to apply the Custom Script Extension to the existing VMs one by one.
They use the Set-AzVMCustomScriptExtension command, specifying the resource group, VM name, storage account name, container name, and script file name. The extension runs on each VM and completes successfully for 8 of them. Two VMs show a status of "Failed".
The administrator checks the logs by connecting to the VM via RDP and looking at the extension log files. They discover that the two VMs had a different version of Windows installed, causing the firewall rule syntax to be invalid. They update the script to handle different OS versions and re-run the extension on the failed VMs.
This scenario shows how the Custom Script Extension saves time, enables automation, and provides logging for troubleshooting.
Common Mistakes
Thinking the Custom Script Extension can be used for ongoing configuration management across reboots.
The extension runs once per deployment unless explicitly triggered again. It does not monitor or maintain configuration state. Use Azure Automation State Configuration or Desired State Configuration for continuous compliance.
Understand that Custom Script Extension is for one-time or on-demand script execution. For persistent configuration management, use DSC or Azure Policy.
Assuming the script will run automatically if the VM is restarted.
The extension runs only when you deploy or redeploy it. A VM reboot does not re-run the script. If you need a script to run at startup, use VM startup scripts or a scheduled task.
If you need a script to run on every boot, configure that within the script itself (e.g., create a startup task) or use a different mechanism like cloud-init on Linux.
Forgetting to provide network access for the VM to reach the script source.
The extension downloads scripts from a URL or Azure Blob Storage. If the VM is in a locked-down subnet with no internet access, the download will fail.
Ensure the VM has outbound connectivity to the script source. Use a private endpoint or copy the script to a storage account in the same virtual network.
Using a script that relies on user interaction or GUI elements.
The extension runs in a non-interactive, headless environment. GUI-based installers or scripts that require user input will hang or fail.
Write scripts that use silent or unattended installation parameters. Test your script on a VM in a headless mode before deploying via the extension.
Not checking the extension logs when it fails.
The extension logs contain detailed information about the script execution, including download status, execution output, and error messages. Many beginners ignore these logs and try random fixes.
Always inspect the extension logs first. For Windows, check C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension. For Linux, check /var/lib/waagent/custom-script. Look for specific error messages.
Exam Trap — Don't Get Fooled
{"trap":"You need to ensure that a custom script runs on a VM every time it starts. The exam lists several options including Custom Script Extension, cloud-init, Azure Automation, and a scheduled task.","why_learners_choose_it":"Learners see 'custom script' and think the extension is the answer because it runs scripts on VMs.
They do not realize the extension does not re-run on boot.","how_to_avoid_it":"Remember that the Custom Script Extension runs only when you deploy it or redeploy it. For a per-boot script, use cloud-init (Linux) or a scheduled task triggered at startup (Windows).
The trap is in thinking the extension is a boot-time solution."
Step-by-Step Breakdown
Prepare the script and store it in a reachable location
Write your script (PowerShell for Windows, bash for Linux) and upload it to Azure Blob Storage, GitHub, or a public HTTP endpoint. Ensure the storage location is accessible from the VM, either via public internet or a private endpoint. Generate a SAS token if using private storage.
Deploy the Custom Script Extension to the VM
You can deploy the extension during VM creation (via portal, ARM template, CLI, or PowerShell) or apply it to an existing VM. Provide the file URIs pointing to your scripts, the command to execute, and any arguments. Azure will inject the extension configuration into the VM.
The Azure VM Agent receives the extension request
The Azure VM Agent running on the VM picks up the extension configuration from Azure Fabric. It validates that the extension is supported and downloads the necessary extension packages. For Custom Script Extension, the agent already has the logic built-in.
Download script files to the VM
The extension downloads the files specified in fileUris from the provided URLs. It stores them in a temporary directory: for Windows, it uses C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension, and for Linux, /var/lib/waagent/custom-script. Files are downloaded with appropriate permissions.
Execute the primary script with specified arguments
The extension runs the script specified in commandToExecute using the appropriate interpreter (PowerShell on Windows, bash on Linux). It forwards any arguments you defined. The script runs with full administrator (root) privileges. Output and errors are streamed to log files.
Report the execution result back to Azure
After the script completes, the extension reports its status (Succeeded, Failed, or Transitioning) to the Azure platform. This status is visible in the Azure portal under the VM's extensions blade or through Azure CLI. Detailed logs remain on the VM for troubleshooting.
Practical Mini-Lesson
In practice, the Custom Script Extension is one of the most commonly used VM extensions in Azure because it solves a fundamental problem: how to configure a VM after it is created without manual intervention. As an IT professional, you will use it in many scenarios. For example, when deploying a new application, you can include the extension in your ARM template to install prerequisites, configure the application, and join the VM to a domain.
You need to ensure your script is written to be idempotent, meaning it can run multiple times without causing issues. Many scripts check if a component is already installed before attempting installation, which is a best practice. Another key point is that the extension logs are your first line of defense when something goes wrong.
Always check the extension status first; if it shows "Failed", go to the VM and read the log files. For Windows, the logs are in C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension, and for Linux, in /var/lib/waagent/custom-script.
The specific log subfolder will have a timestamp and the extension version. The main log file is often named script-0.log or similar. Look for error messages related to file download, command execution, or script syntax.
Another practical tip: when using storage accounts, use SAS tokens with expiration times to secure your scripts. Avoid storing scripts in public containers. Also, keep your scripts simple and test them on a single VM before scaling out.
The extension does not support scripts that require user input; all installation must be silent. For Linux VMs, be aware that the extension runs as root, so your script can modify any system file. For Windows, the extension runs as SYSTEM, which also has full privileges.
This power means you must be careful not to break the OS accidentally. The Custom Script Extension can be used with VM Scale Sets. When you define an extension profile for the scale set, every new VM instance automatically runs the script as soon as it is provisioned.
This is a powerful way to ensure all instances in a scale set are configured identically. Finally, remember that extensions can be applied to multiple VMs using the Azure CLI or PowerShell in a loop, but for larger deployments, ARM templates or Azure Policy are more efficient.
Memory Tip
Think of Custom Script Extension as a "one-time setup assistant" for Azure VMs. It runs your script once, then waits for you to call it again. Not for startup.
Covered in These Exams
Current Exam Context
Current exam versions that test this topic — use these objectives when studying.
AZ-104AZ-104 →PCAGoogle PCA →Related Glossary Terms
Two-factor authentication (2FA) is a security method that requires two different types of proof before granting access to an account or system.
AAA (Authentication, Authorization, and Accounting) is a security framework that controls who can access a network, what they are allowed to do, and tracks what they did.
A 3D printer is a device that creates physical objects by depositing layers of material based on a digital model.
5G is the fifth generation of cellular network technology, designed to deliver faster speeds, lower latency, and support for many more connected devices than previous generations.
A/B testing is a controlled experiment that compares two versions of a single variable to determine which one performs better against a predefined metric.
Frequently Asked Questions
Can I run a Custom Script Extension on a Linux VM that has no outbound internet access?
Yes, but you must store the script in a location that the VM can reach, such as a storage account in the same virtual network using a private endpoint. Without internet or private access, the download will fail.
Does the Custom Script Extension run every time I reboot the VM?
No. It runs only when you deploy or redeploy the extension. For scripts that need to run at startup, use cloud-init (Linux) or a scheduled task (Windows).
How do I pass parameters to the script?
You specify parameters in the commandToExecute property of the extension configuration. For example: 'powershell -ExecutionPolicy Unrestricted -File script.ps1 -Param1 Value1'. On Linux, you can pass arguments directly in the command string.
What happens if the script fails?
The extension reports a status of Failed. You can view the error details in the extension logs on the VM. The VM itself remains running and usable. You can fix the script or configuration and re-run the extension.
Can I use the Custom Script Extension to uninstall software?
Yes, you can write a script that uninstalls software. However, the extension itself does not track what was installed or uninstalled. You are responsible for the script logic.
Is the Custom Script Extension supported on all Azure VM sizes?
Yes, it is supported on all VM sizes that have the Azure VM Agent installed. The agent is present on most marketplace images. If you use a custom image, you must ensure the agent is installed and running.
How is the Custom Script Extension different from Azure Automation State Configuration?
The Custom Script Extension runs a script once. Azure Automation State Configuration (DSC) continuously monitors and maintains a desired state on the VM. Use the extension for initial setup; use DSC for ongoing compliance.
Summary
The Custom Script Extension is an essential tool for Azure VM automation, allowing you to run scripts on VMs after they are deployed. It simplifies post-deployment configuration by letting you install software, apply settings, and perform any administrative task without manual intervention. The extension works by downloading scripts from a storage location and executing them with full system privileges.
It is a one-time execution tool; it does not run on reboot or monitor configuration drift. For ongoing configuration management, you would combine it with other services like Azure Automation or Desired State Configuration. In the AZ-104 exam, you will be tested on when to use this extension, how to deploy it, and how to troubleshoot failures.
Common mistakes include using it for startup scripts, ignoring network access requirements, and not checking extension logs. Understanding the difference between the Custom Script Extension and similar tools like DSC, runbooks, and cloud-init is crucial. By mastering this concept, you can automate server setup at scale, ensure consistency, and speed up deployments.