How to reboot VMs in a VMSS after an ARM Template Custom Script Extension is ran?

Jerred Moss 0 Reputation points Microsoft Employee
2025-07-09T19:37:25.8366667+00:00

I created an ARM Template Microsoft.Compute/virtualMachineScaleSets/extensions CustomScriptExtension which is modifying 2 registry values.

In order to pick up the changes I need to either restart the HTTP Service or reboot.

What is the best way to do this?

  • Do I schedule a reboot with a Register-ScheduledTask?
    • Can I just schedule it for say 10 minutes from now?
  • Do I need to manually do a rolling upgrade with a az vmss rolling-upgrade?
  • Another way?

I would rather not need to manually do an upgrade since this will need to be done on every VMSS.

Azure Virtual Machine Scale Sets
Azure Virtual Machine Scale Sets
Azure compute resources that are used to create and manage groups of heterogeneous load-balanced virtual machines.
{count} votes

2 answers

Sort by: Most helpful
  1. Alex Burlachenko 13,330 Reputation points Volunteer Moderator
    2025-07-10T07:47:52.42+00:00

    hi jerred! great question ))

    first, u can actually add a simple reboot command right inside ur custom script extension. just slap a 'restart computer' at the end if u're using powershell. like this

    Restart-Computer -Force 
    

    but hey, if u need a delay (like ur 10-minute idea), u can totally schedule it. this works inside the script too ))

    Register-ScheduledTask -Action (New-ScheduledTaskAction -Execute 'shutdown.exe' -Argument '/r /f') -Trigger (New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(10)) -TaskName 'DelayedReboot'  
    

    or, if u prefer the azure way, u can trigger a rolling reboot after the extension finishes. https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-manage-cli#reboot-vms-in-a-scale-set

    just run az vmss restart --resource-group myResourceGroup --name myScaleSet --instance-ids *

    also, if u're dealing with http service changes, maybe just restarting that service could save u time? worth looking into ))

    Restart-Service -Name 'HTTP' -Force 
    

    as well check this https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows for more on custom script tweaks.

    this scheduling trick might help in other tools too. u never know when u'll need a delayed restart ))

    hope this sorts it out for u! let us know if u hit any snags...

    Best regards,

    Alex

    and "yes" if you would follow me at Q&A - personaly thx.
    P.S. If my answer help to you, please Accept my answer
    

    https://ctrlaltdel.blog/


  2. Anusree Nashetty 5,820 Reputation points Microsoft External Staff Moderator
    2025-07-14T11:11:58.08+00:00

    Hi Jerred Moss,

    You're correct to be cautious, and your interpretation of the Microsoft documentation is spot on. As per documents:

    "Don't put restarts inside the script.": refers to direct calls like Restart-Computer, shutdown.exe, or Restart-Service. The Custom Script Extension (CSE) doesn’t wait for the VM to come back up — it marks the operation as failed or stuck. Rebooting in the middle of script execution may interrupt other extensions (e.g., monitoring agents, antivirus, DSC, etc.) that are still installing.

    Schedule the restart by using a Windows Scheduled Task..." the recommended, supported pattern to defer restarts or service reloads until after CSE has exited cleanly. It guarantees that Azure marks the extension as Provisioning succeeded, preventing the VM or VMSS instance from being flagged as unhealthy.

    "Don't stop or update the VM agent." The Azure VM agent manages extensions. If it’s killed or updated unexpectedly by your script, it can leave the VM in a broken provisioning state.

    At the very end of your script, add a Windows Scheduled Task that delays a reboot or restarts the service — after CSE is long gone.

    If you have any further queries, let me know. If the information is helpful, please click on Upvote.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.