-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateVM.ps1
240 lines (192 loc) · 7.01 KB
/
CreateVM.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# Parse Params:
[CmdletBinding()]
Param(
[Parameter(
Position=1,
Mandatory=$True,
HelpMessage="What is the name of the virtual machine (or machines) to be created?"
)]
[string]$VMMachineName,
[Parameter(
Position=2,
Mandatory=$False,
HelpMessage="What Generation VM should be created? Valid values are 1 (legacy) and 2 (UEFI, requires Win8/Server 2012 or higher!) - default is 1."
)]
[ValidateRange(1,2)]
[int16]$Generation = '1',
[Parameter(
Position=3,
Mandatory=$False,
HelpMessage="Define the memory size for the VM(s). The minimum is 1GB, max is 64GB, default is 2GB."
)]
[ValidateRange(1GB,64GB)]
[int64]$vMemory = '2147483648',
[Parameter(
Position=4,
Mandatory=$False,
HelpMessage="Dynamic Memory enabled, true or false."
)]
[ValidateSet($True, $False)]
[bool]$DynamicMem = $True,
[Parameter(
Position=5,
Mandatory=$False,
HelpMessage="How many vCPUs should each machine be configured with? The minimum is 1, max is 8, default is 2."
)]
[ValidateRange(1,8)]
[int]$vCPU = '2',
[Parameter(
Position=6,
Mandatory=$False,
HelpMessage="What virtual switch should the default NIC be bound to? External, Internal, Corp - default is 'Internal'."
)]
[ValidateSet('External','Internal','Corp')]
[string]$vSwitch = 'Internal',
[Parameter(
Position=7,
Mandatory=$False,
HelpMessage="Should the vNIC be Synthetic or Legacy (for PXE booting)? Default is 'Synthetic' - note this param is ignored if 'Generation' param set to '2'."
)]
[ValidateSet('Synthetic','Legacy')]
[string]$vNIC = 'Synthetic',
[Parameter(
Position=8,
Mandatory=$False,
HelpMessage="VLAN ID?"
)]
[int]$VlanId,
[Parameter(
Position=9,
Mandatory=$False,
HelpMessage="Create VHD or VHDX?"
)]
[ValidateSet('vhd','vhdx')]
[string]$VHDType = 'vhdx',
[Parameter(
Position=9,
Mandatory=$False,
HelpMessage="Is this a Linux VM? (default is False)"
)]
[ValidateSet($True, $False)]
[bool]$LinuxVM = $False
)
# Thanks to Tome Tanasovski:
# http://powertoe.wordpress.com/2012/03/13/powerbits-8-opening-a-hyper-v-console-from-powershell/
function Connect-VM {
param(
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]
[String[]]$ComputerName
)
PROCESS {
foreach ($name in $computername) {
vmconnect localhost $name
}
}
}
# Check if 'Legacy' passed for vNIC, and set $vNICType accordingly:
$vNICType = $False
If ($vNIC -eq 'Legacy')
{
$vNICType = $True
}
# Edit default VM path as necessary, these are my usual defaults:
$VMPath = "C:\VM"
$VHDPath = $VMPath + "\" + $VMMachineName + "\Virtual Hard Disks\" + $VMMachineName + "." + $VHDType
# Check if VM exists and remove:
$VMExist = Get-VM -Name $VMMachineName -ErrorAction SilentlyContinue
If ($VMExist)
{
If ($VMExist.State -eq "Off")
{
Write-Host "Deleting virtual machine $VMMachineName in 5 seconds..." -ForegroundColor Yellow
Start-Sleep 5
Remove-VM $VMMachineName -Force -Confirm:$False -ErrorAction Stop
}
Else
{
Write-Host "Stopping virtual machine $VMMachineName in 5 seconds..." -ForegroundColor Yellow
Start-Sleep 5
Stop-VM -Name $VMMachineName -TurnOff -Force -Confirm:$false -ErrorAction Stop
Write-Host "Deleting virtual machine $VMMachineName in 5 seconds..." -ForegroundColor Yellow
Start-Sleep 5
Remove-VM $VMMachineName -Force -Confirm:$False -ErrorAction Stop
}
}
# Check if $VMMachineName folder exists and remove:
If (Test-Path -Path "$VMPath\$VMMAchineName")
{
Write-Host "Deleting existing data in path $VMPath\$VMMachineName in 5 seconds..." -ForegroundColor Yellow
Start-Sleep 5
Remove-Item -Path "$VMPath\$VMMachineName" -Recurse -Force -Confirm:$False -ErrorAction Stop
}
# Let's go:
Write-Host ""
Write-Host "Creating virtual machine" $VMMachineName "..." -ForegroundColor Cyan
Write-Host ""
# Create the new VM, including generation type - attach no VHD yet:
New-VM -Path $VMPath -Generation $Generation -MemoryStartupBytes $vMemory -Name $VMMachineName -NoVHD | Out-Null
# The default adapter is only created as legacy if you specify it as the
# default boot adapter, so remove and re-create later:
Remove-VMNetworkAdapter -VMName $VMMachineName -Name "Network Adapter"
# Configure VM-specific CPU, vMem, and VHD - if Linux, set VHDX block size to 1MB:
If ($DynamicMem -eq $True)
{
Set-VMMemory $VMMachineName -DynamicMemoryEnabled $True -MinimumBytes 768MB -StartupBytes 1024MB -MaximumBytes $vMemory -Priority 80 -Buffer 20
}
Else
{
Set-VMMemory $VMMachineName -DynamicMemoryEnabled $False
}
Set-VMProcessor $VMMachineName -Count $vCPU
If ($LinuxVM -eq $True)
{
New-VHD -Dynamic -Path $VHDPath -SizeBytes 127GB –BlockSizeBytes 1MB | Out-Null
}
Else
{
New-VHD -Dynamic -Path $VHDPath -SizeBytes 127GB | Out-Null
}
Add-VMHardDiskDrive -VMName $VMMachineName -Path $VHDPath
# Configre Gen1 vs Gen2-specific features:
If ($Generation -eq '1')
{
Add-VMNetworkAdapter -VMName $VMMachineName -Name "$vSwitch Network" -IsLegacy $vNICType -SwitchName $vSwitch
Set-VMBios $VMMachineName -StartupOrder @("LegacyNetworkAdapter","CD","IDE","Floppy")
Set-VMBios $VMMachineName -EnableNumLock
}
else
{
Add-VMNetworkAdapter -VMName $VMMachineName -Name "$vSwitch Network" -SwitchName $vSwitch
#Change to Hyper-V module 1.1 - bug in Windows 10/Server 2016
Remove-Module Hyper-V
Import-Module Hyper-V -RequiredVersion 1.1 -Force
Add-VMDvdDrive -VMName $VMMachineName
#Change back to Hyper-V module 2.0
Remove-Module Hyper-V
Import-Module Hyper-V
$DVD = Get-VMDvdDrive -VMName $VMMachineName
Set-VMFirmware -VMName $VMMachineName –EnableSecureBoot Off -PreferredNetworkBootProtocol IPv4 -FirstBootDevice $DVD
}
# Set the virtual COM port for kernel debugging:
Set-VMComPort -VMName $VMMachineName -Path \\.\pipe\$VMMAchineName -Number 1
# Enable Guest Service Interface Integration (allows copy to/from VM via
# PowerShell):
Enable-VMIntegrationService -VMName $VMMachineName -Name "Guest Service Interface"
# Set VM Start/Stop behavior:
Set-VM -Name $VMMachineName -AutomaticStartAction Nothing -AutomaticStopAction Save
# Configure VLAN if param passed:
If ($VlanId)
{
Set-VMNetworkAdapterVlan –VMName $VMMachineName –Access –VlanId $VlanId
}
# Uncomment to add an ISO at path $ISO:
#$ISO = '\\Server\share\isoname.iso'
#Set-VMDvdDrive -VMName $VMMachineName -Path $ISO
# Start the VM:
#Write-Host "Starting virtual machine $VMMachineName..." -ForegroundColor Cyan
#Start-Sleep 1
#Start-VM -Name $VMMachineName
# Connect to the VM:
Write-Host "Connecting to virtual machine $VMMachineName..." -ForegroundColor Cyan
Start-sleep 1
Connect-VM $VMMachineName