-
Notifications
You must be signed in to change notification settings - Fork 3
/
configure-hyperv-guest.ps1
40 lines (36 loc) · 1.4 KB
/
configure-hyperv-guest.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
param(
[Parameter(ValueFromRemainingArguments=$true)]
[string[]]$addresses
)
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
}
# bail when not running over hyperv.
$systemVendor = (Get-WmiObject Win32_ComputerSystemProduct Vendor).Vendor
if ($systemVendor -ne 'Microsoft Corporation') {
Exit 0
}
# NB the first network adapter is the vagrant management interface
# which we do not modify.
# NB this is somewhat brittle: InterfaceIndex sometimes does not enumerate
# the same way, so we use MacAddress instead, as it seems to work more
# reliably; but this is not ideal either.
# TODO somehow use the MAC address to set the IP addresses.
$adapters = @(Get-NetAdapter -Physical | Sort-Object MacAddress | Select-Object -Skip 1)
for ($n = 0; $n -lt $adapters.Length; ++$n) {
$adapter = $adapters[$n]
Write-Output "Setting the $($adapter.Name) ($($adapter.MacAddress)) adapter IP address to $($addresses[$n])..."
$adapter | New-NetIPAddress `
-IPAddress $addresses[$n] `
-PrefixLength 24 `
| Out-Null
$adapter | Set-NetConnectionProfile `
-NetworkCategory Private `
| Out-Null
}