VM tools: Run Command, extensions, Custom Script
-
Phase 0: Before you start
-
Phase 1: The foundation
-
Phase 2: Day-to-day operations
-
Phase 3: Enterprise architecture
-
Phase 4: Troubleshooting step by step
-
Phase 5: Final project and career
Azure Run Command runs a script inside a VM from the outside, through the Azure VM agent, so you need no SSH, no RDP and no open port. Along with VM extensions like Custom Script, it’s how admins fix, set up and look into servers they can’t log in to.
You’ve been using Run Command since Lesson 15 without looking at how it works. This time we open it up and look at the old and new versions and their limits, how extensions run, and where the logs are when something goes wrong.
You’ll check the VM agent, try both versions of Run Command and run a Custom Script. Then you’ll fix a script that “didn’t run” and some output that went missing.
| Time | You need | Cost | By the end you can |
|---|---|---|---|
| About 40 minutes | web01, the CLI | Covered by the free account’s B2ats v2 hours | Run scripts through the VM agent, manage extensions, and troubleshoot both |
What it is, in one line
Run Command and extensions let Azure run scripts inside a VM through its agent, without anyone logging in.
In Simple Words
Notes passed through the school office
The head teacher can’t walk into every classroom, so she writes a note, the office runner carries it to the class teacher, and the teacher writes the answer back. That’s Run Command, with the VM agent as the runner. Some notes are standing orders, like “every new class gets a fire drill on day one”, and those are extensions. If the runner is off sick, no note gets through however urgent it is, which is why your first check is always whether the agent is alive.
Engineer's View
Run Command, managed Run Command and extensions
| Action Run Command | Managed Run Command | Custom Script extension | |
|---|---|---|---|
| CLI | az vm run-command invoke | az vm run-command create | az vm extension set |
| At once per VM | One | Several | One CustomScript per VM |
| Output | Last 4,096 bytes in the reply | Instance view, or full output to a blob | Status message; logs on the VM |
| Timeout | 90 minutes | You set it | 90 minutes (Linux handler) |
| Best for | Quick checks and fixes | Repeatable or long tasks, run as a user | Setup at deploy time |
- The VM agent (waagent on Linux, the Windows guest agent) has to be Ready, and you check that with
az vm get-instance-view. If it isn’t, nothing in this lesson works. - Scripts run elevated: as root on Linux and SYSTEM on Windows, unless you set a user on a managed run command.
- Custom Script runs once per configuration. If you apply the same settings again nothing happens, so either change the settings or use
--force-update. - Secrets belong in protected settings, which are encrypted and never shown back to you.
- Logs on Linux:
/var/log/azure/run-command/handler.log,/var/log/azure/custom-script/handler.logand the script output under/var/lib/waagent/custom-script/download/0/. - Logs on Windows:
C:\WindowsAzure\Logs\Plugins\and downloads underC:\Packages\Plugins\.
How it works, step by step
What happens when you run az vm run-command invoke:
- Azure Resource Manager logs the request against the VM.
- The VM agent collects it through the host (the wire server at 168.63.129.16).
- The agent installs the Run Command extension, or reuses it if it’s there, and starts your script as root.
- Your script runs, and the extension holds on to the last 4,096 bytes of output.
- The agent reports the status back and the CLI prints it.
Extensions take the same route. Azure keeps the extension settings you want in the VM model, and the agent applies them and reports back through the extension’s provisioning state.
So when a script or extension “does nothing”, go in this order: the agent first, then the extension’s status message, and then the logs on the VM.
Hands-on lab: Azure Run Command and extensions on web01
Before you run any command, double-check it
Read every command twice before you press Enter. Check the target: the system or account, the IDs, the names and the sizes. These commands are for learning and were tested in our lab, not on your system. At work, run the read-only list commands first, follow your change process, make sure a backup exists, and ask a second person to check anything that changes or deletes data. You run them at your own risk.
Start web01:
az vm start --resource-group rg-saat-vms --name web011Check the VM agent
Run this:
az vm get-instance-view --resource-group rg-saat-vms --name web01 --query "instanceView.vmAgent.{version:vmAgentVersion, status:statuses[0].displayStatus}" -o tableYou should see something like this:
Version Status
--------- --------
2.12.0.4 Ready2Action Run Command: a quick check
Run this:
az vm run-command invoke --resource-group rg-saat-vms --name web01 --command-id RunShellScript --scripts 'uptime -p; df -h / | tail -1; systemctl is-active saat-web' --query "value[0].message" -o tsvYou should see something like this:
Enable succeeded:
[stdout]
up 2 minutes
/dev/root 62G 2.1G 60G 4% /
active
[stderr]3Managed Run Command: named, stored, with an exit code
Run this:
az vm run-command create --resource-group rg-saat-vms --vm-name web01 --name rc-health --script "uptime -p; systemctl is-active saat-web" --timeout-in-seconds 120 -o none
az vm run-command show --resource-group rg-saat-vms --vm-name web01 --name rc-health --instance-view --query "instanceView.{state:executionState, exitCode:exitCode, output:output}" -o yamlYou should see something like this:
exitCode: 0
output: |
up 3 minutes
active
state: SucceededAction Run Command isn’t a resource, but this one is. You can run several of them, see their history and exit codes, and send long output to a blob with --output-blob-uri.
4Custom Script extension
Put the settings in a file, apply them and check what happened:
Set-Content -Path C:\azure-lab\cse-web01.json -Value '{"commandToExecute": "echo cse ran at $(date -u +%H:%M:%S) >> /var/www/cse.log"}'
az vm extension set --resource-group rg-saat-vms --vm-name web01 --name CustomScript --publisher Microsoft.Azure.Extensions --version 2.1 --settings '@C:\azure-lab\cse-web01.json' -o none
az vm extension list --resource-group rg-saat-vms --vm-name web01 --query "[].{name:name, version:typeHandlerVersion, state:provisioningState}" -o table
az vm run-command invoke --resource-group rg-saat-vms --name web01 --command-id RunShellScript --scripts 'cat /var/www/cse.log' --query "value[0].message" -o tsvYou should see something like this:
Name Version State
---------------- --------- ---------
CustomScript 2.1 Succeeded
RunCommandLinux 1.0 Succeeded
[stdout]
cse ran at 17:02:41RunCommandLinux is the extension that sits behind action Run Command, and it got installed the first time you used it in Lesson 15. We keep the settings in a file so PowerShell 5.1 doesn’t strip out the quotes.

✅ Checkpoint: The agent is Ready, both Run Command versions work, and Custom Script wrote one line to /var/www/cse.log.
Break It, Then Fix It
Neither of these throws an error, and that’s exactly why they’re hard to catch.
1Break it: “I ran the extension again and nothing happened”
Apply the very same Custom Script settings a second time and then check the log:
az vm extension set --resource-group rg-saat-vms --vm-name web01 --name CustomScript --publisher Microsoft.Azure.Extensions --version 2.1 --settings '@C:\azure-lab\cse-web01.json' -o none
az vm run-command invoke --resource-group rg-saat-vms --name web01 --command-id RunShellScript --scripts 'cat /var/www/cse.log' --query "value[0].message" -o tsvYou should see this:
[stdout]
cse ran at 17:02:41Still one line. The command “succeeded”, but Azure saw no change in the extension’s settings, so the agent had nothing new to do. People lose hours this way, rerunning a fix that never runs.
Fix it: force the update, or change something in the settings such as a timestamp:
az vm extension set --resource-group rg-saat-vms --vm-name web01 --name CustomScript --publisher Microsoft.Azure.Extensions --version 2.1 --settings '@C:\azure-lab\cse-web01.json' --force-update -o none
az vm run-command invoke --resource-group rg-saat-vms --name web01 --command-id RunShellScript --scripts 'cat /var/www/cse.log; tail -3 /var/log/azure/custom-script/handler.log' --query "value[0].message" -o tsvYou should see something like this:
[stdout]
cse ran at 17:02:41
cse ran at 17:06:15
time=2026-10-20T17:06:16Z version=v2.1.x operation=enable seq=1 event="enabled"
...2Break it: the missing output
Run a command that prints a lot of lines:
az vm run-command invoke --resource-group rg-saat-vms --name web01 --command-id RunShellScript --scripts 'seq 1 2000' --query "value[0].message" -o tsv | Select-Object -First 4You should see something like this:
Enable succeeded:
[stdout]
1182So where did lines 1 to 1181 go? Action Run Command keeps only the last 4,096 bytes of output, and seq 1 2000 prints 8,893 bytes. A long log dump or an inventory script quietly loses its top half.
Fix it: write the full output to a file on the VM and send back a summary, and for long jobs use a managed run command with an output blob:
az vm run-command invoke --resource-group rg-saat-vms --name web01 --command-id RunShellScript --scripts 'seq 1 2000 > /tmp/report.txt; wc -l /tmp/report.txt; head -2 /tmp/report.txt' --query "value[0].message" -o tsvYou should see this:
Enable succeeded:
[stdout]
2000 /tmp/report.txt
1
2
[stderr]To clean up, remove the extension, the managed run command and the files, and then stop web01:
az vm extension delete --resource-group rg-saat-vms --vm-name web01 --name CustomScript
az vm run-command delete --resource-group rg-saat-vms --vm-name web01 --name rc-health --yes
az vm run-command invoke --resource-group rg-saat-vms --name web01 --command-id RunShellScript --scripts 'rm -f /var/www/cse.log /tmp/report.txt' --query "value[0].message" -o tsv
az vm deallocate --resource-group rg-saat-vms --name web01
✅ Checkpoint: You re-ran a Custom Script with a forced update and got the full output back. Only RunCommandLinux is left on web01, and web01 is deallocated.
This happened at work
The certificate fix that never ran
A payments team used the Custom Script extension to push a renewed TLS certificate to 40 Linux VMs. The pipeline went green and the portal showed every extension as Succeeded, so everyone moved on. Two days later the old certificate expired and the API went down on all 40 servers.
It turned out the pipeline had redeployed the same Custom Script settings as the month before, and only the certificate file in storage was new. Azure saw identical settings, so not one VM ran the script, and the handler logs on every VM showed no new enable operation.
What they changed: the pipeline now adds a timestamp parameter to the extension settings each time it runs, and it checks the script’s output file on every VM before it marks the job done. They also monitor certificate expiry separately now, 30 days ahead, through Key Vault (Lesson 55).
Common mistakes and troubleshooting
| Mistake | What you see | What to do |
|---|---|---|
| Re-applying unchanged CSE settings | Succeeded, but the script didn’t run | Use a forced update or change the settings. |
| Large output from action Run Command | Top of the output missing | Write to a file; return a summary. |
| VM agent not Ready | Run Command and extensions time out | Fix the agent first (Lesson 60). |
| Two Run Commands at once | Second one refused or queued | Wait, or use managed run commands. |
| Secrets in public settings | Passwords visible in the portal and logs | Use protected settings or Key Vault. |
| Scripts that wait for input | Run hangs until timeout | Make scripts non-interactive. |
| fileUris from the internet on a private subnet | Download fails | Host scripts in same-region storage. |
Interview questions
Click any question to open it. They’re worded the way an interviewer would ask them, and under each one you’ll find why they ask it and one way you could answer in your own words.
Fresher level
1. What is Azure Run Command?
Why they ask this: To see if you know the basic idea.
How you might answer: It’s a way to run scripts inside a VM through the Azure VM agent, from the portal, CLI or API, without RDP or SSH. It’s handy when you can’t log in, or when you don’t want to open ports.
2. What does the VM agent do?
Why they ask this: It’s the foundation for the rest.
How you might answer: The agent runs inside the VM and talks to the Azure host. It reports health, and it installs and runs extensions, Run Command and Custom Script included. If the agent isn’t Ready, none of that works.
3. What is the Custom Script extension used for?
Why they ask this: It’s a configuration tool you’ll see all the time.
How you might answer: You use it to run a script on a VM, usually at deployment, to install software or change settings. It can pull files down from storage and run a command, and the result shows up as the extension status.
2–5 years experience
4. A Custom Script extension shows Succeeded but the change isn’t there. Why?
Why they ask this: It’s a trap that catches people in production.
How you might answer: Most of the time the settings didn’t change, so Azure never reran it, or the script exited 0 even though one step failed. I’d read the handler log and stdout on the VM, and then rerun it with a forced update or changed settings.
5. When would you use managed Run Command instead of action Run Command?
Why they ask this: They want to hear that you know the difference.
How you might answer: I’d use managed Run Command for long or repeated jobs, when I need several scripts at once, an exit code and history, a particular user, or the full output saved to a blob. For a quick one-off check, action Run Command is better.
6. Run Command times out on a VM. How do you troubleshoot?
Why they ask this: They want to see you have a method.
How you might answer: I’d look at the VM agent status in the instance view first, then boot diagnostics, and whether the VM is under heavy load. I’d make sure the guest can reach the host at 168.63.129.16 and has the outbound access Run Command needs, and if I can get in some other way, I’d read the handler log.
Senior level
7. How do you configure 500 VMs consistently after deployment?
Why they ask this: They want to see you think at scale.
How you might answer: I’d bake as much as I can into the images, then use Azure Policy with machine configuration or policy-applied extensions, plus a tool like Ansible. Custom Script is fine for small bootstrap steps, but at that scale it’s hard to keep track of.
8. How would you control who can use Run Command?
Why they ask this: They’re checking the security side.
How you might answer: Run Command runs as root or SYSTEM, so the runCommand actions are as powerful as admin access. I’d restrict them with custom roles and PIM, make sure they’re in the Activity log, and alert whenever someone uses them in production.
9. How do you keep secrets out of extension settings?
Why they ask this: They want to hear secure patterns.
How you might answer: At the very least they go in protected settings. Better still, give the VM a managed identity and have the script read the secrets from Key Vault when it runs, so nothing sensitive sits in the VM model at all.
Quick quiz
Q1. How much output does action Run Command return?
- All of it
- The first 4,096 bytes
- The last 4,096 bytes
- Nothing
Show answer
C — Output is cut to the last 4,096 bytes.
Q2. What must be Ready for Run Command to work?
- Bastion
- The VM agent
- Public IP
- NSG
Show answer
B — Run Command and extensions run through the agent.
Q3. You re-apply the same Custom Script settings. What happens?
- It runs again
- Nothing runs
- The VM restarts
- An error
Show answer
B — Unchanged settings don’t trigger a new run.
Q4. Which runs several scripts on one VM at the same time?
- Action Run Command
- Managed Run Command
- Bastion
- JIT
Show answer
B — Managed run commands are separate resources.
Q5. Where should secrets for an extension go?
- Public settings
- Protected settings or Key Vault
- The script file name
- Tags
Show answer
B — Protected settings are encrypted and hidden.
Quick recap
You can now run code inside any VM without logging in, and you can tell when it didn’t really run.
- The VM agent is the gateway for Run Command and extensions.
- Action Run Command: one at a time, last 4,096 bytes, 90 minutes.
- Managed Run Command: several, exit codes, output to a blob.
- Custom Script reruns only when its settings change.
- Read the handler logs when status says Succeeded but nothing changed.
Next lesson
Lesson 30: Azure Update Manager: patch Windows and Linux → Next, you check, schedule and install updates on web01 and win01, and fix a VM that refuses to patch.
Further reading: Azure Run Command
Want to go deeper into Azure Run Command? Microsoft’s Run scripts in your VM by using Run Command and Custom Script Extension for Linux are good next reads, and the full plan for this course is on the Azure Cloud Administration course page.
