Skip to content

Instantly share code, notes, and snippets.

@ducke
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ducke/3caa27a1eb9482b4f259 to your computer and use it in GitHub Desktop.
Save ducke/3caa27a1eb9482b4f259 to your computer and use it in GitHub Desktop.
# run on the VM which will provide the image for the vagrant box
# to save a lot of time and disk space, delete any unwanted snapshots from the VM
# based, almost entirely, on http://www.hurryupandwait.io/blog/in-search-of-a-light-weight-windows-vagrant-box
# rename built-in admin account username and pwd to 'vagrant'
$admin=[adsi]"WinNT://./Administrator,user"
$admin.psbase.rename("vagrant")
$admin.SetPassword("vagrant")
$admin.UserFlags.value = $admin.UserFlags.value -bor 0x10000
$admin.CommitChanges()
# IMPORTANT: reboot now if you are logged in as 'Administrator'
# configure the computer to receive remote commands
# http://technet.microsoft.com/en-us/library/hh849694.aspx
Enable-PSRemoting -Force
# configure WinRM to work with vagrant
winrm set winrm/config/client/auth '@{Basic="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
# allow remote desktop connections
$obj = Get-WmiObject -Class "Win32_TerminalServiceSetting" -Namespace root\cimv2\terminalservices
$obj.SetAllowTsConnections(1,1)
# enable firewall rules for RDP and winrm outside of subnet
Set-NetFirewallRule -Name WINRM-HTTP-In-TCP -Enabled true -RemoteAddress Any
Set-NetFirewallRule -Name RemoteDesktop-UserMode-In-TCP -Enabled True
# change the powershell execution policy
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force
# cleanup WinSXS update debris
Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase
# optimize the drive
Optimize-Volume -DriveLetter C
# purge any hidden data
wget http://download.sysinternals.com/files/SDelete.zip -OutFile "$($env:TEMP)\sdelete.zip"
Add-Type -A System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory("$($env:TEMP)\sdelete.zip", "$($env:TEMP)")
& "$($env:TEMP)\sdelete.exe" -z c:
# shutdown
Stop-Computer
# run on the host
# edit these variables accordingly
$vmname = "myvirtualmachine"
$vmdir = "C:\path\to\Virtual Hard Disks\"
# compact the virtual hard disks
Get-ChildItem -File -Path $vmdir | Optimize-VHD -Mode Full
# export the VM
Export-VM -Name $vmname -Path .\boxes
# delete the Snapshots directory
Remove-Item .\boxes\$vmname\Snapshots -Recurse -Force
# create metadata.json
Add-Content .\boxes\$vmname\metadata.json "{`n ""provider"": ""hyperv""`n}"
# create Vagrantfile
Add-Content .\boxes\$vmname\Vagrantfile "# -*- mode: ruby -*-`n# vi: set ft=ruby :`n`nVagrant.configure(""2"") do |config|`n config.vm.guest = :windows`n config.vm.communicator = ""winrm""`nend"
# get bsdtar
Invoke-WebRequest https://github.com/benesch/libarchive/releases/download/v3.1.2/libarchive-3.1.2-bin-windows.zip -OutFile "$($env:TEMP)\bsdtar.zip"
Add-Type -A System.IO.Compression.FileSystem
If (Test-Path "$($env:TEMP)\bsdtar") { Remove-Item "$($env:TEMP)\bsdtar" -Recurse -Force }
[System.IO.Compression.ZipFile]::ExtractToDirectory("$($env:TEMP)\bsdtar.zip", "$($env:TEMP)\bsdtar")
# create the final box file
cd .\boxes\$vmname
& "$($env:TEMP)\bsdtar\bin\bsdtar.exe" --lzma -cvf "$vmname.box" *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment