Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Turn on auto logon in a sysprepped Vagrant box
Vagrant.configure("2") do |config|
config.vm.define "tst" do |cfg| = "StefanScherer/windows_2016"
cfg.vm.hostname = "tst"
cfg.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.memory = "2048"
vb.linked_clone = true
cfg.vm.provision "shell", privileged: false, inline: <<-SHELL
Set-ItemProperty "HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon" -Name AutoAdminLogon -Value 1
Set-ItemProperty "HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon" -Name DefaultUserName -Value "vagrant"
Set-ItemProperty "HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon" -Name DefaultPassword -Value "vagrant"
Remove-ItemProperty "HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon" -Name AutoAdminLogonCount -Confirm -ErrorAction SilentlyContinue
cfg.vm.provision "reload"
cfg.vm.provision "shell", privileged: false, inline: <<-SHELL
iwr -useb | iex
choco install -y docker
docker --version

This comment has been minimized.

Copy link

@chkpnt chkpnt commented Apr 24, 2020

Thanks for the snippet!

But I suggest to include the 3rd party dependency with a line like

config.vagrant.plugins = ["vagrant-reload"]

Because otherwise and if the reload-plugin isn't installed, you are wondering what this is about:

> vagrant up --provision
Bringing machine 'default' up with 'virtualbox' provider...
There are errors in the configuration of this machine. Please fix
the following errors and try again:

* The 'reload' provisioner could not be found.

Another thing: What's the purpose of the Remove-ItemProperty statement? I'm asking because it is failing on my VM:

    default: Running: inline PowerShell script
    default: powershell.exe : Remove-ItemProperty : Object reference not set to an instance of an object.
    default:     + CategoryInfo          : NotSpecified: (Remove-ItemProp...e of an object.:String) [], RemoteException
    default:     + FullyQualifiedErrorId : NativeCommandError
    default: At C:\tmp\vagrant-shell.ps1:4 char:9
    default: +         Remove-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\Curr ...
    default: +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    default:     + CategoryInfo          : NotSpecified: (:) [Remove-ItemProperty], NullReferenceException
    default:     + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.RemoveIt
    default:    emPropertyCommand
The following WinRM command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

powershell -ExecutionPolicy Bypass -OutputFormat Text -file "c:\tmp\vagrant-shell.ps1"

Stdout from the command:

Stderr from the command:

powershell.exe : Remove-ItemProperty : Object reference not set to an instance of an object.
    + CategoryInfo          : NotSpecified: (Remove-ItemProp...e of an object.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
At C:\tmp\vagrant-shell.ps1:4 char:9
+         Remove-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\Curr ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Remove-ItemProperty], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.RemoveIt

It doesn't seem to be necessary: Without that statement, the provisioning went through and my VM (Win 10) is now applying the auto-login.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment