-
Notifications
You must be signed in to change notification settings - Fork 3
/
configure-hyperv-vm.ps1
51 lines (47 loc) · 1.8 KB
/
configure-hyperv-vm.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
param(
$vmId,
$bridgesJson
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
trap {
Write-Host "ERROR: $_"
Write-Host (($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$','ERROR: $1')
Write-Host (($_.Exception.ToString() -split '\r?\n') -replace '^(.*)$','ERROR EXCEPTION: $1')
Exit 1
}
$bridges = ConvertFrom-Json $bridgesJson
$vm = Get-VM -Id $vmId
# reconfigure the network adapters to use the given switch names.
# NB vagrant has already configured ALL network interfaces to use
# the $env:HYPERV_SWITCH_NAME switch.
# NB the first network adapter is the vagrant management interface
# which we do not modify.
$networkAdapters = @(Get-VMNetworkAdapter -VM $vm | Select-Object -Skip 1)
$networkAdapters | Select-Object -Skip $bridges.Length | ForEach-Object {
Write-Host "Removing the VM $vmId from the $($_.SwitchName) switch..."
$_ | Remove-VMNetworkAdapter
}
for ($n = 0; $n -lt $bridges.Length; ++$n) {
$bridge = $bridges[$n]
$switchName = $bridge[0]
$macAddressSpoofing = $bridge[1]
if ($n -lt $networkAdapters.Length) {
Write-Host "Connecting the VM $vmId to the $switchName switch..."
$networkAdapter = $networkAdapters[$n]
$networkAdapter | Connect-VMNetworkAdapter -SwitchName $switchName
$networkAdapter | Set-VMNetworkAdapterVlan -Untagged
} else {
Write-Host "Connecting the VM $vmId to the $switchName switch..."
$networkAdapter = Add-VMNetworkAdapter `
-VM $vm `
-Name $switchName `
-SwitchName $switchName `
-Passthru
}
$networkAdapter | Set-VMNetworkAdapter `
-MacAddressSpoofing "$(if ($macAddressSpoofing) {'On'} else {'Off'})"
}
Write-Host "VM Network Adapters:"
Get-VMNetworkAdapter -VM $vm