Skip to content

Instantly share code, notes, and snippets.

@StefanScherer
Created June 25, 2018 05:32
Show Gist options
  • Save StefanScherer/adbb421dc0adca75b49e0031a99595a5 to your computer and use it in GitHub Desktop.
Save StefanScherer/adbb421dc0adca75b49e0031a99595a5 to your computer and use it in GitHub Desktop.
Turn on auto logon in a sysprepped Vagrant box
Vagrant.configure("2") do |config|
config.vm.define "tst" do |cfg|
cfg.vm.box = "StefanScherer/windows_2016"
cfg.vm.hostname = "tst"
cfg.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.memory = "2048"
vb.linked_clone = true
end
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
SHELL
cfg.vm.provision "reload"
cfg.vm.provision "shell", privileged: false, inline: <<-SHELL
iwr -useb https://chocolatey.org/install.ps1 | iex
choco install -y docker
docker --version
SHELL
end
end
@chkpnt
Copy link

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:

vm:
* 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
    default:
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
   emPropertyCommand

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.

@nurajbihari
Copy link

I cannot get this to work. I edited my vagrant file to exactly what you have.

@chkpnt
Copy link

chkpnt commented Feb 8, 2022

Dunno what's your issue, but perhaps my Vagrantfile / provisioning.ps1 in https://github.com/chkpnt/MSEdge-Vagrant might help, which is used for a similar provisioning.

@nurajbihari
Copy link

I'm not sure what the problem is. But every win 10 box that I've downloaded from vagrant appears to prevent me from configuring auto-logon. I first download the box, do a vagrant up with (even with the vagrant file in this original post) but don't get it to autologon. I even manually checked and confirmed that these registry keys are set correctly:
"HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -Value 1
"HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultUserName -Value "vagrant"
"HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultPassword -Value "vagrant"
What would prevent vagrant boxes that were downloaded from allowing autologon to be confirgured. I also did it via the netplwiz. I appreciate you giving me your scripts but I don't think it will solve the issue because those registry keys are already set and still it doesn't work. I have to manually login every time.

@nurajbihari
Copy link

Actually, after a glass of wine I was able to find the solution. For Hyper-V if you connect via an enhanced session, I guess it requires you to enter the password, but if you connect to the vm via a basic session, you don't need to enter the password. I don't understand by I'm happy to figure it out finally.

chocolatey/boxstarter#318
https://superuser.com/questions/709874/change-server-2012-display-resolution-inside-of-a-hyper-v-vm
Close the session if you have it open. Just 'orange x' it out. No need to shut it down.
When closed, some settings behaviors change. Right click on your VM and under 'Connect', there should now be 'Edit Session Settings'.
This should now bring you back to the dialog that lets you select resolution and attach local devices to the VM.
Once you have a new resolution selected, just re-connect.

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