-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigure-HyperV-Network.ps1
168 lines (133 loc) · 6.52 KB
/
Configure-HyperV-Network.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<#
.SYNOPSIS
.DESCRIPTION
This script will configure the Hyper-V network switches the way I want on a new machine
.TESTING
.NOTES
Author : KyleGW
Version : 0.1
https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-guide/setup-nat-network
https://blogs.technet.microsoft.com/jhoward/2008/06/17/hyper-v-what-are-the-uses-for-different-types-of-virtual-networks/
https://blogs.technet.microsoft.com/heyscriptingguy/2013/10/09/use-powershell-to-create-virtual-switches/
**https://hyperv.veeam.com/blog/how-to-configure-hyper-v-virtual-switch/
https://www.petri.com/using-nat-virtual-switch-hyper-v
https://4sysops.com/archives/native-nat-in-windows-10-hyper-v-using-a-nat-virtual-switch/
* Win 10 post build 16237 - quick create includes default NAT switch
https://blogs.technet.microsoft.com/virtualization/2017/07/26/hyper-v-virtual-machine-gallery-and-networking-improvements/
.MODIFICATIONS
2017-10-21 KyleGW Created script
.EXAMPLE
#>
#Load logging Function
# . .\Write-Log.ps1
# Putting it inline for this script instead of loading it externally
#$global:logFilePath = "d:\logs"
#$global:logFileName = "$((Get-Date).ToString("yyyy-MM-dd HH:mm:ss"))-LogfileName.log"
Function Write-Log
{
Param ([string]$textToWriteToLog)
$LogFile = Join-Path $logFilePath $logFileName
if (!(Test-Path -path $logFile ))
{
$logFile = New-Item -type file $logFile -Force
}
Write-Debug $textToWriteToLog
Try
{
Add-content $logfile -value "$((Get-Date).ToString("yyyy-MM-dd HH:mm:ss.fff")) - $textToWriteToLog"
}
catch [system.UnauthorizedAccessException]
{
throw "Error (logfile): Write access denied to [$logFile]"
}
}
Function Get-NetworkConfiguration
{
#todo -- update to work with write-debug or verbose instead of direct write to log file
#Gather Information on Existing Configurations
Write-Log "---------------------------------------------------------------------------------------------"
Write-Log " Print Configuration"
Write-Log "---------------------------------------------------------------------------------------------"
Write-Log "List existing virtual switches"
Get-VMSwitch | Out-File $LogFile -Encoding ASCII -Append
Get-NetLbfoTeam | Out-File $LogFile -Encoding ASCII -Append
Write-Log "List existing Network Adapter info"
Get-NetAdapter | sort ifIndex | Out-File $LogFile -Encoding ASCII -Append
Write-Log "`n`nList existing IP Information"
Get-NetIPAddress | where -Property InterfaceAlias -NotMatch 'Loopback' | sort ifIndex | ft | Out-File $LogFile -Encoding ASCII -Append
Write-Log "List existing default gateway information"
Get-NetRoute -DestinationPrefix 0.0.0.0/0 | Out-File $LogFile -Encoding ASCII -Append
}
$scriptShortName = "HyperV Network Configuration"
$DebugPreference = "Continue"
$VerbosePreference = "silentlycontinue" #change to Continue/SilentlyContinue to turn on/off verbose messages
$ErrorActionPreference = "Stop"
$logFilePath = "d:\Scriptlogs"
$logFileName = "$((Get-Date).ToString("yyyy-MM-dd HHmmss")) - $env:COMPUTERNAME - $scriptShortName.log"
$LogFile = Join-Path $logFilePath $logFileName
$TranscriptFileName = "$((Get-Date).ToString("yyyy-MM-dd HHmmss")) - $env:COMPUTERNAME - $scriptShortName Transcript.log"
if(Test-Path $MyInvocation.MyCommand.Definition)
{
if (!$MyInvocation.MyCommand.Definition)
{
$0 = split-path -parent $MyInvocation.MyCommand.Definition
}
}
else
{
$0 = " likely ISE Run Selection"
}
#Transcripting this session to catch system output that isn't explicitly logged with Write-Log
$TranscriptFile = Join-Path $logFilePath $TranscriptFileName
Start-Transcript -Path $TranscriptFile -Append -Force
Write-Host "Logged output can be found in $LogFile"
Write-Log "---------------------------------------------------------------------------------------------"
Write-Log "Script instance Started"
Write-Log "Running under context: $env:USERDOMAIN\$env:USERNAME"
Write-Log "Currently running from: $0"
Write-Log "---------------------------------------------------------------------------------------------"
Get-NetworkConfiguration
Write-Log "---------------------------------------------------------------------------------------------"
Write-Log " Updating Configuration"
Write-Log "---------------------------------------------------------------------------------------------"
if ($false)
{
Import-Module Hyper-V
$ethernet = Get-NetAdapter -Name ethernet
$wifi = Get-NetAdapter -Name wi-fi
New-VMSwitch -Name externalSwitch -NetAdapterName $ethernet.Name -AllowManagementOS $true -Notes 'Parent OS, VMs, LAN'
New-VMSwitch -Name privateSwitch -SwitchType Private -Notes 'Internal VMs only'
New-VMSwitch -Name internalSwitch -SwitchType Internal -Notes 'Parent OS, and internal VMs'
}
if ($configureSwitchLaptop)
{
$wifi = Get-NetAdapter -Name wi-fi
#New-VMSwitch -Name WiFiExternalSwitch -NetAdapterName $wifi.Name -AllowManagementOS $true -Notes 'Parent OS, VMs, wifi'
New-VMSwitch -switchname "Internal HyperV NAT Switch" -SwitchType Internal -Notes 'Parent OS, and internal VMs'
New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex 23
New-NetNat -Name HyperVNAT -InternalIPInterfaceAddressPrefix 192.168.0.0/24
}
if ($configureSwitchPrivate)
{
New-VMSwitch -Name privateSwitch -SwitchType Private -Notes 'Internal VMs only - no connection to host'
Get-NetAdapter
(Get-NetAdapter -name wi-fi).ifIndex
# New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex 23
New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex(Get-NetAdapter -name wi-fi).ifIndex
}
#$desktop = $true
if($desktop)
{
$ethernet = Get-NetAdapter -Name ethernet
New-VMSwitch -Name InternetSwitch -NetAdapterName $ethernet.Name -AllowManagementOS $true -Notes 'Parent OS, VMs, LAN'
New-VMSwitch -Name vmOnlySwitch -SwitchType Private -Notes 'Internal VMs only'
}
Write-Log "---------------------------------------------------------------------------------------------"
Write-Log " Completed Updating Configuration"
Write-Log "---------------------------------------------------------------------------------------------"
Get-NetworkConfiguration
Write-Log "---------------------------------------------------------------------------------------------"
Write-Log " Script Ended"
Write-Log "---------------------------------------------------------------------------------------------"
Stop-Transcript
Invoke-Item $LogFile