Skip to content

Instantly share code, notes, and snippets.

@andrealbinop
Last active May 28, 2023 01:54
Show Gist options
  • Save andrealbinop/57e388df5e881937e62a to your computer and use it in GitHub Desktop.
Save andrealbinop/57e388df5e881937e62a to your computer and use it in GitHub Desktop.
Setup modern.ie vagrant boxes

Setup modern.ie vagrant boxes

Since modern.ie released vagrant boxes, it' no longer necessary to manually import the ova file to virtualbox, as mentioned here.

However, the guys at modern.ie didn't configured the box to work with WinRM. This how-to addresses that, presenting steps to proper repackage these boxes, adding WinRM support. Additionally configures chocolatey package manager and puppet provisioner.

Pre-requisites

The host machine must have Vagrant and VirtualBox installed.

1. Configure the Vagrantfile

The same Vagrantfile can be used with different versions of modern.ie available boxes. Read the comments in the file for details.

# -*- mode: ruby -*-
# vi: set ft=ruby :

# box name into env var, same script can be used with different boxes. Defaults to win7-ie11.
box_name = box_name = ENV['box_name'] != nil ? ENV['box_name'].strip : 'win7-ie11'
# box repo into env var, so private repos/cache can be used. Defaults to http://aka.ms
box_repo = ENV['box_repo'] != nil ? ENV['box_repo'].strip : 'http://aka.ms'

Vagrant.configure("2") do |config|
  # If the box is win7-ie11, the convention for the box name is modern.ie/win7-ie11
  config.vm.box = "modern.ie/" + box_name
  # If the box is win7-ie11, the convention for the box url is http://aka.ms/vagrant-win7-ie11
  config.vm.box_url = box_repo + "/vagrant-" + box_name
  # big timeout since windows boot is very slow
  config.vm.boot_timeout = 500

  # rdp forward
  config.vm.network "forwarded_port", guest: 3389, host: 3389, id: "rdp", auto_correct: true

  # winrm config, uses modern.ie default user/password. If other credentials are used must be changed here
  config.vm.communicator = "winrm"
  config.winrm.username = "IEUser"
  config.winrm.password = "Passw0rd!"

  config.vm.provider "virtualbox" do |vb|
    # first setup requires gui to be enabled so scripts can be executed in virtualbox guest screen
    #vb.gui = true
    vb.customize ["modifyvm", :id, "--memory", "1024"]
    vb.customize ["modifyvm", :id, "--vram", "128"]
    vb.customize ["modifyvm", :id,  "--cpus", "2"]
    vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
    vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    vb.customize ["guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000]
  end
end

2. Setup

Create a directory and a Vagrantfile file with the contents from previous section. For the first run, uncomment the vb.gui=true line to disable VirtualBox headless mode. Start the box running the command:

# uncomment below to change the box version and repo
# export box_name=win7-ie11 && export box_repo=http://aka.ms
$ vagrant up

When the machine is booted, do the following steps on VirtualBox guest window:

@echo off
set WINRM_EXEC=call %SYSTEMROOT%\System32\winrm
%WINRM_EXEC% quickconfig -q
%WINRM_EXEC% set winrm/config/winrs @{MaxMemoryPerShellMB="300"}
%WINRM_EXEC% set winrm/config @{MaxTimeoutms="1800000"}
%WINRM_EXEC% set winrm/config/client/auth @{Basic="true"}
%WINRM_EXEC% set winrm/config/service @{AllowUnencrypted="true"}
%WINRM_EXEC% set winrm/config/service/auth @{Basic="true"}
  • At this time the up command will be probably verifying if the guest booted properly. Since you just configured WinRM, the command should terminate successfully.

3. Chocolatey and Puppet

Run the following script to configure chocolatey and puppet on the guest machine:

@echo off
@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin
choco install -y puppet

4. Package

Since there's a lot of Windows specific configuration, you can include the Vagrantfile in the package command so winrm and virtualbox configuration get's default values when the repackaged is used for other purposes. Remember to run the command in the same directory the Vagrantfile resides:

$ vagrant package --output "yourboxname" --Vagrantfile Vagrantfile

After that you're all set!

@snfnwgi
Copy link

snfnwgi commented Sep 24, 2017

@snfnwgi
Copy link

snfnwgi commented Sep 30, 2017

How do I solve the synchronization problem? I want to use NFS or RSYNC, but error

@jmhublar
Copy link

jmhublar commented Oct 8, 2017

Get-NetConnectionProfile
Which lists the profiles and from which you need to find the index number

Set-NetConnectionProfile -InterfaceIndex 1 -NetworkCategory Private
Where 1 is the index number of the profile to change so may be something other than 1

@hazmeister
Copy link

WinRM seems to start on a delay by default, run this as admin to get it to start when the machine does.
sc config winrm start= auto

@clement-igonet
Copy link

clement-igonet commented Feb 9, 2018

To activate winrm (work on win10 modern.ie machine):

# (...)
  config.vm.communicator = ENV['communicator'] || 'winrm'
  config.ssh.username = "IEUser"
  config.ssh.password = "Passw0rd!"
  config.ssh.insert_key = false
  config.ssh.sudo_command = ''
# (...)
  config.ssh.shell = 'sh -l'
    config.vm.provision "shell",
      binary: true,
      privileged: false,
      inline: <<-SHELL
      # winrm - Switch to private network
      /cygdrive/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -InputFormat None -NoProfile -ExecutionPolicy Bypass -Command '$networkListManager = [Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]"{DCB00C01-570F-4A9B-8D69-199FDBA5723B}")) ; $connections = $networkListManager.GetNetworkConnections() ; $connections | % {$_.GetNetwork().SetCategory(1)}'
    SHELL
# (...)

And then, at first boot:

$ communicator=ssh vagrant up

@clement-igonet
Copy link

When connecting to win7 modern.ie machine with vagrant/ssh (net-ssh ruby lib) and using "sh" shell and IEUser/Passw0rd!, whoami return "sshd_server".
When connecting directly with openssh clent, it returns "ieuser".
The issue it creates is bad right access to setup winrm. Anybody already met the same issue?

@xywang68
Copy link

@clement-igonet, on Windows-10 your above "activate winrm" instruction works great.

@rzapletal
Copy link

Is it still working for you? Newest vagrant and VirtualBox, freshly downloaded Windows 10 box and I still get only PowerShell error.

powershell.exe : Cannot convert the "DCB00C01-570F-4A9B-8D69-199FDBA5723B" value of type
    + CategoryInfo          : NotSpecified: (Cannot convert ... value of type :String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
"System.Management.Automation.ScriptBlock" to type "System.Guid".
At line:1 char:1
+ $networkListManager = [Activator]::CreateInstance([Type]::GetTypeFrom ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : ConvertToFinalInvalidCastException

You cannot call a method on a null-valued expression.
At line:1 char:125
+ ... A5723B})) ; $connections = $networkListManager.GetNetworkConnections( ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:1 char:204
+ ... workConnections() ; $connections | % {$_.GetNetwork().SetCategory(1)}
+                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

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