Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example Vagrant Windows SMB Setup
$username = $args[0]
$password = $args[1]
$domain = 'DOMAIN' # Again, update DOMAIN with your company's Domain
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext $ct,$domain
$valid = $pc.ValidateCredentials("${username}", "${password}")
write-host "Authenticating ${username} against AD..."
if ( $valid -ne $true ) { throw "Invalid $domain Username\\Password" } else { Write-Output "success!" }
exit 0
# Creates a network share from the current working directory
# with the name 'vagrantworkspace'. Also sets up permissions
# such that common user accounts can access it over the network
# so that IIS running inside a Vagrant VM can access it.
# This is mainly a workaround for Virtualbox shared folders
# which do not properly work. DO NOT point IIS at Virtualbox
# shares as it may look to be working but they do not. You've
# been warned.
Write-Output "Setting up a network share on local machine ${env:COMPUTERNAME}"
$sharePath = $args[0]
$shareName = $args[1]
$share = "\\${env:COMPUTERNAME}\$shareName"
Write-Output "Setting up share for local development"
if (net share | Select-String $shareName) {
net share $shareName /delete /y
$result = net share vworkspace=$PWD "/grant:Everyone,FULL" /unlimited
Write-Output "Created new share ${share} on folder ${sharePath}"
exit 0
require 'io/console'
# Capture login details if starting up vagrant or provisioning it
# Required for AD operations.
# Environment variables prevent explicit user input. Useful for CI.
username = ENV["VAGRANT_USER"] || nil
password = ENV["VAGRANT_PASSWORD"] || nil
# Used to generate a unique VM name registered to AD
computerName, shareComputerName =
['v' + `hostname`.sub("", "").strip, `hostname`.strip]
# Setup local code shares for Vagrant
# NOTE: this feature will be available natively in Vagrant 1.7
sharePath = File.dirname(__FILE__)
shareName = 'vworkspace'
shareGuestPath = 'c:\vworkspace'
# Collect credentials from the user (unless specified through ENV vars)
# Only request creds for provisioning type operations
# NOTE: Replace DOMAIN with your company domain
if ARGV[0] != nil && ['up', 'provision', 'reload'].include?(ARGV[0])
if !username
print "Please enter your DOMAIN username: "
username = "DOMAIN\\" + STDIN.gets.chomp
if !password
print "Please enter your password (input is hidden): "
password = STDIN.noecho(&:gets).chomp
puts ""
# Let's fail early and loud if the users'
# credentials supplied are not actually valid.
# Sorry Mac\Linux users - this only works on Windows!
share = false
message = "Unable to create share. Skipping."
if Gem.win_platform?
system("powershell.exe ./.box/authenticate.ps1 #{username} #{password}")
if !$?.success?
puts "Authentication failed, please try again."
exit 1
# Create Local Share
share = system("powershell.exe ./.box/setup-share.ps1 #{sharePath} #{shareName}")
elsif RUBY_PLATFORM.match("darwin")
share = system("sh ./.box/")
message = "Sorry, we haven't created automatic SMB sharing support for your platform yet."
share = false
if !share
puts "#{message}"
# Actual Vagrantfile.
# Vagrant is responsible for running your infrastructure!
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| = "dev-base-v3"
guest_port = 5895 :forwarded_port, guest: 3389, host: 3299, id: "rdp", auto_correct: false :forwarded_port, guest: 5985, host: guest_port, id: "winrm", auto_correct: false :forwarded_port, guest: 80, host: 8001, id: "web", auto_correct: false :forwarded_port, guest: 443, host: 8444, id: "ssl", auto_correct: false
config.winrm.guest_port = guest_port = "localhost"
config.vm.host_name = computerName
config.vm.synced_folder ".", "/vagrant"
# This creates network access into the guest from the host.
# If you prefer to manualy set the IP of the guest,
# this is how you do it:
# "private_network", ip: "" "private_network", type: "dhcp"
config.vm.provider "virtualbox" do |v|
v.gui = true
# Connect to the created network share each time the box is started
$setup_share = <<SCRIPT
$secpasswd = ConvertTo-SecureString "#{password}" -AsPlainText -Force
$DOMAIN_CREDENTIALS = New-Object System.Management.Automation.PSCredential ("#{username}", $secpasswd)
$host_path = "\\\\#{shareComputerName}\\#{shareName}"
net use * $host_path /user:#{username} #{password} /persistent:yes
# The main provisioning script
$do_something = <<SCRIPT
# ...
config.vm.provision "shell", inline: $setup_share, run: always
config.vm.provision "shell", inline: $do_something
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.