Azure az vm run-command invoke silently fails when script includes runuser with arguments (works manually on VM)

1 week ago 3
ARTICLE AD BOX

I am invoking Azure Run Command from Python using:

az vm run-command invoke --command-id RunShellScript --scripts "<script...>"

When I run the same script manually via SSH on the Linux VM, it works.
But when the same script is passed to Azure Run Command, the command appears to run but:

The script never actually executes

No stdout from inside the script is returned

No error is shown

Azure Run Command JSON output contains empty [stdout]

import subprocess import json def invoke(vm_name, rg): # This script *works manually* when executed over SSH. # But Azure Run Command silently does NOT execute it. script_to_execute = """#!/bin/bash runuser -u azureuser -- bash echo "Hi this is working" >> /home/azureuser/test.log """ cmd = [ "az", "vm", "run-command", "invoke", "--command-id", "RunShellScript", "--name", vm_name, "--resource-group", rg, "--scripts", script_to_execute, "--output", "json" ] result = subprocess.run(cmd, capture_output=True, text=True) print("stdout:", result.stdout) print("stderr:", result.stderr) try: output = json.loads(result.stdout) print("Azure message:", output["value"][0]["message"]) except: print("Could not parse JSON.")

Is this a known limitation or escape-parsing issue in the --scripts argument, and what is the recommended way to debug or validate that the script actually executed?

Read Entire Article