How to run PowerShell on Azure servers without remote desktop
Run Command, managed Run Command and the Custom Script Extension compared, and how to let someone run an approved script without letting them run any script.
Clearing a print queue, restarting a stuck service, emptying a temp folder. Most of the jobs that bring someone onto a Windows server are one line of PowerShell, and most of them still start with a remote desktop session and an admin password.
Azure has three ways to run a script on a virtual machine without signing in to it. This guide covers each one, what it can and cannot do, and the question none of them answers on its own: how to let someone run this script without letting them run any script.
Why not just use remote desktop
Remote desktop gives a person everything the account can do, for as long as the session lasts, to run a script that takes thirty seconds. Making it safe also costs money:
- Azure Bastion lets people use remote desktop from the portal without a public IP address on the machine. At the time of writing the Basic tier is £0.1449 an hour in UK South, about £106 a month (Azure's Azure Bastion pricing). The free Developer tier connects to one machine at a time and is meant for testing.
- Just-in-time access opens the remote desktop port only when someone asks, but it needs Defender for Servers Plan 2 on the subscription.
Neither changes the basic trade: to run one script, someone gets the whole server.
Option 1: Run Command
Run Command sends a script to the machine through its Azure agent. In the portal it is under Operations, then Run command, then RunPowerShellScript. From the command line:
az vm run-command invoke -g <resource-group> -n <vm-name> \
--command-id RunPowerShellScript \
--scripts "Restart-Service -Name Spooler -Force"No inbound port is opened. The machine only needs to reach Azure outbound on port 443. The limits to know:
| Limit | What it means |
|---|---|
| Output | Only the last 4,096 bytes come back |
| Run time | Up to 90 minutes, then it times out |
| One at a time | Only one script can run at once, and a running script cannot be cancelled |
| Account | Scripts run as System, the most powerful account on the machine |
The last row is the catch. Running a command needs one permission, Microsoft.Compute/virtualMachines/runCommand/action, which the built-in Virtual Machine Contributor role includes. Whoever holds it can run any script as System. It is a permission for your engineers, not for a client.
Option 2: managed Run Command
Managed Run Command is the newer version. It runs several scripts in parallel or in order, takes a time-out you choose so long jobs can finish, passes secrets as protected parameters, and writes output to a blob so nothing is cut off at 4 KB. It works from the Azure CLI, PowerShell and the API rather than the portal, and needs the runCommand/write permission, which, like Option 1, lets whoever holds it run any script on the machine.
Option 3: the Custom Script Extension
The Custom Script Extension downloads a script and runs it as LocalSystem. It was built for setting a machine up after it is deployed, and it shows:
- It runs a script once. Running it again needs changed settings.
- Only one copy of the extension can be on a machine at a time.
- A script gets 90 minutes, and output is cut to the last 4,096 bytes.
Those limits make it awkward by hand and reliable behind a tool that manages it, which is how most products that run scripts on Azure machines use it.
The question none of them answer: this script, not any script
Azure grants the right to run a script. It has no way to grant the right to run this script. So handing routine jobs to a client, or to a first-line engineer, needs a layer in front of Azure that does four things:
- A catalogue of approved scripts, each one written and checked by you, so the person running it never writes code.
- Permissions per person: who can see the scripts, who can run them, and who can change them.
- Parameters you allow, such as the name of the service to restart, rather than free text that reaches the command line.
- A record of every run: who ran what, on which machine, when, and what the script printed.
Without that layer, the choice is between doing every job yourself and handing over the keys.
Which to choose
| Approach | Who can safely use it | What it costs | Best for |
|---|---|---|---|
| Remote desktop with Bastion | Engineers | About £106 a month for Basic | Work that really needs the desktop |
| Run Command | Engineers | Free | A quick fix from the portal |
| Managed Run Command | Engineers and automation | Free, plus blob storage for output | Long or multi-step jobs you script |
| Custom Script Extension | Automation | Free | Setting machines up, or behind a tool |
| Approved scripts in a portal | Clients and first-line staff too | The portal | Routine jobs you want off your desk |
How Task Ember does it
Task Ember is a white-label client portal for MSPs and IT teams. Its Remote PowerShell product is that layer, built on the Custom Script Extension:
- Your scripts, your catalogue. You publish the scripts a workspace may run. People choose from them, fill in the parameters you allow, and see the output.
- Three permissions. Seeing the scripts, running them and changing them are granted separately. Someone who can run but not change scripts can only run a saved chain exactly as it was saved. If the script has changed, it does not run.
- Chains. Scripts run one after another, or bundled into one quick run, on one machine or several side by side.
- Schedules for routine jobs like clean-ups and restarts.
- A record of every run. Who ran what, where and when, with the output, kept for 90 days and ready to run again.
- No open ports and no remote desktop. Nobody needs a password for the server.
It sits in the client's workspace in your portal, with your logo and colours. See Remote PowerShell and the rest of the catalogue at taskember.com/products.