Search code examples
windowsbatch-fileazurevirtual-machinestartup

Batch file not running on Azure VM Startup until user login


Question: Does anyone know why a batch file is not running on machine startup until a user connects via remote desktop? Also, is there a way to execute a batch file on machine startup without requiring any user interaction?

Background: I have a virtual machine hosted in Microsoft Azure. I opened a remote desktop session, created a simple batch file that starts a game server (i.e. gameserver.exe), created a shortcut to that batch file, and added the shortcut to AppData/Windows/Start Menu/Programs/Startup.

Now, when I restart the VM from the Azure Management console. I'm not seeing the gameserver start, no matter how long I wait.

But, when I remote into the VM as soon as the remote desktop session is connected I see the batch file open and start running, thus starting my game server.

I am able to create a shortcut to gameserver.exe and place that into the startup folder, and that runs as I would expect as soon as the VM is restarted. However, I need to do a couple different things, in order, to configure the server before I start it so I wanted to use a batch file to accomplish that.


Solution

  • The only way I am aware of to make something run on a server would be using Group Policy for the Computer Configuration to autorun something at boot. Next step would be an AutoRun key (RunOnce) in the registry that executes something when the user logs in. Next step from that would be PsExec which can target remote machines and run an executable or script of your choice.

    Additionally you can configure the VM to autologin a user then lock the VM. This way your RunOnce from the Registry would be running or even drop the applications shortcut into the startup folder. See more about auto login and locking here.