Azure – Auto login Azure VM

automationazure

I've some processes I want to automate using an Azure VM. I want to automatically start it up, run something, then shut it down and deprovision it until the next scheduled startup.

I can start something on boot by using Task Scheduler and setting something to run on startup, before login. This works fine with a console app I've written which just does stuff and writes to a database without trying to open a window. It doesn't work with a batch file, which is how I originally wanted to go- use a batch file to run a PowerShell script which runs my app and then uses the return code to decide whether to shut down and deprovision. The obvious difference is the console window, so I suspect there's no UI at this point and cmd is choking waiting for it.

This poses a problem with the next task- periodically use screen scraping software to do operations on a remote website, pull records down and store them in the local DB. There's no API for the remote service. Without a UI, I can't launch Firefox in marionette mode and do the record updates.

I've tried adding registry entries as described in here and setting a task to start after login, but the task never gets run and although I can't be sure it looks like the UI comes up after the remote desktop session connects.

I've seen a solution that uses Powershell on a local laptop to script the startup of the VM and the establishment of a remote desktop connection, but if I was going to commit to leaving the laptop on to do this, the laptop may as well be doing everything, which I don't want- that's the whole point of trying to put it in Azure.

So, has anyone done something similar and knows how to get an Azure VM to come fully up into a logged in desktop without any interaction from outside?

Best Answer

If you are using ARM template deployment you can use the following OSProfile for a windows machine to configure AutoLogon:

"osProfile": {
        "computerName": "[parameters('vmName')]",
        "adminUsername": "[parameters('adminUsername')]",
        "adminPassword": "[parameters('adminPassword')]",
        "WindowsConfiguration": {
            "additionalUnattendContent": [
              {
                "passName": "oobesystem",
                "componentName": "Microsoft-Windows-Shell-Setup",
                "settingName": "AutoLogon",
                "content": "[concat('<AutoLogon><Password><Value>', parameters('adminPassword'), '</Value><PlainText>true</PlainText></Password><Enabled>true</Enabled><Username>', parameters('adminUsername'), '</Username></AutoLogon>')]"
              }
            ]
        }
      }
Related Topic