-
Notifications
You must be signed in to change notification settings - Fork 1
/
deployVM-HyperV.ps1
279 lines (220 loc) · 8.95 KB
/
deployVM-HyperV.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#Requires -Version 5 –Modules Hyper-V -RunAsAdministrator
param(
$VhdPath = "C:\Virtualharddisks\win2016core.vhdx",
$VSwitchName = "DSC-vSwitch1",
$HostIpAddress = "192.0.0.247",
$DnsServer = "192.0.0.25",
[parameter(mandatory)]
[pscredential]
$Credential,
$AdapterCount = 1,
$TargetSubnet = "255.255.255.0",
[switch]$Clobber = $true,
[switch]$DifferencingDisks = $true,
$NodeDefinitionFilePath = "C:\Library\deploy\DSCPushSetup\DefinitionStore\NodeDefinition.ps1"
)
Import-Module Hyper-V
if (!(Get-CimInstance -Namespace root\virtualization\v2 -Class Msvm_VirtualSystemManagementService -ErrorAction Ignore))
{
throw "Incorrect version of Hyper-V. Please update to continue"
}
#Region Ensure paths are valid
if (! (Test-Path $VhdPath))
{
throw "Could not find Parent VHDX."
}
if (! (Test-Path $NodeDefinitionFilePath))
{
throw "Could not find Node Definition File."
}
#endregion
#region Build the VM configs list
$nodeDefinition = . $NodeDefinitionFilePath
$vmList = $nodeDefinition.Configs.ForEach({
@{
TargetItemName = $_.Variables.ComputerName
TargetIP = $_.TargetAdapter.NetworkAddress.IPAddressToString
MacAddress = $_.TargetAdapter.PhysicalAddress
VmMemory = 1024MB
}
})
#endregion
function Set-VmGuestIpAddress
{
param(
$VmName,
$MacAddress,
$IpAddress,
[parameter(mandatory)]
$Subnet,
$Gateway = $HostIpAddress,
$Dns = $dnsServer
)
#WMI required throughout this function because CIM functionality is not completely transcribed at this time 12/5/2016
$hypervManagement = Get-WmiObject -ClassName 'Msvm_VirtualSystemManagementService' -Namespace 'root\virtualization\v2'
$guestVM = Get-WmiObject -ClassName 'Msvm_ComputerSystem' -Namespace 'root\virtualization\v2' -Filter "ElementName='$vmName'"
$guestVMCim = Get-CimInstance -ClassName 'Msvm_ComputerSystem' -Namespace 'root\virtualization\v2' -Filter "ElementName='$vmName'"
$hyperVGuestSettings = Get-CimAssociatedInstance -ResultClassName Msvm_VirtualSystemSettingData -InputObject $guestVMCim
$hyperVGuestEthernetSettings = Get-CimAssociatedInstance -ResultClassName Msvm_SyntheticEthernetPortSettingData -InputObject $hyperVGuestSettings[0]
#Need a loop because there is no filter option and using a "Where" clause changes object type from cimInstance to List
foreach ($nic in $hyperVGuestEthernetSettings)
{
if ($nic.Address -eq $MacAddress)
{
$targetNic = $nic
}
}
$instance = ($targetnic.instanceid.Split('\'))[1]
#Need to use WMI here instead of CIM because there's no straightforward way to convert the NIC Config into
#text, required by the SetGuestNetworkAdapterConfiguration method. There is not a comparable CIM method
$targetNicConfig = Get-WmiObject -Namespace root\virtualization\v2 -Class 'Msvm_GuestNetworkAdapterConfiguration' -Filter "InstanceID like '%$($instance)'"
$targetNicConfig.DHCPEnabled = $false
$targetNicConfig.IPAddresses = @("$IpAddress")
$targetNicConfig.Subnets = @("$Subnet")
$targetNicConfig.DefaultGateways = @("$Gateway")
$targetNicConfig.DNSServers = @("$Dns")
$null = $hypervManagement.SetGuestNetworkAdapterConfiguration($guestVM, $targetNicConfig.GetText(1))
}
#region TrustedHosts
#Add the IP list of the target VMs to the trusted host list
$currentTrustedHost = (Get-Item WSMan:\localhost\Client\TrustedHosts).Value
if(($currentTrustedHost -ne '*') -and ([string]::IsNullOrEmpty($currentTrustedHost) -eq $false))
{
$scriptTrustedHost = $currentTrustedHost + ", " + $($vmList.TargetIP -join ", ")
Set-Item -Path WSMan:\localhost\Client\TrustedHosts -Value $scriptTrustedHost -Force
}
elseif($currentTrustedHost -ne '*')
{
$scriptTrustedHost = $Targets.TargetIP -join ", "
Set-Item -Path WSMan:\localhost\Client\TrustedHosts -Value $scriptTrustedHost -Force
}
#endregion
#region host networking
$switch = Get-VMSwitch $VSwitchName -ErrorAction Ignore
if (!($switch))
{
$null = New-VMSwitch $VSwitchName -SwitchType Internal
}
#Set host IP for pre-build
$localIPList = Get-NetIPAddress
$targetNIC = Get-NetAdapter -Name "vEthernet ($VSwitchName)"
if ($localIPList.IPAddress -notcontains $HostIpAddress)
{
$null = New-NetIPAddress –IPAddress $HostIpAddress –PrefixLength 24 -InterfaceIndex $targetNIC.ifIndex -ErrorAction Ignore
$null = Set-NetIPAddress –IPAddress $HostIpAddress –PrefixLength 24 -InterfaceIndex $targetNIC.ifIndex -ErrorAction Ignore
}
#Set host network adapter/switch to enabled
if ($targetNIC.Status -eq 'Disabled')
{
$null = Enable-NetAdapter -InputObject $targetNIC -Confirm:$false
}
#endregion
#region provisioning VMs
workflow ConfigureVM
{
param(
$VmList,
$VHDPath,
$VSwitchName,
$Subnet,
$AdapterCount,
$RemoteCred,
[bool]$Clobber,
[bool]$DifferencingDisks
)
$vhdParentPath = Split-Path -Path $VHDPath -Parent
foreach -parallel ($vm in $VmList)
{
sequence
{
$vmName = $vm.TargetItemName
$vmIp = $vm.TargetIP
$vmMac = $vm.MacAddress
$vmMemory = $vm.vmMemory
#First cleanup the environment by removing the existing VMs and VHDXs, if clobber is set to true
Write-Output "Deploying infrastructure for target item $vmName"
if (Test-Path "$vhdParentPath\$vmName.vhdx")
{
if ($Clobber)
{
Stop-VM $vmName -TurnOff -Force -ErrorAction Ignore -WarningAction Ignore
Remove-VM $vmName -Force -ErrorAction Ignore
Remove-Item "$vhdParentPath\$vmName.vhdx" -ErrorAction Ignore
if ($DifferencingDisks)
{
Write-Output "Creating differencing disk at $vhdParentPath\$vmName.vhdk"
$newVHD = New-VHD -Path "$vhdParentPath\$vmName.vhdx" -ParentPath "$VHDPath" -Differencing
}
else #do a full copy
{
Write-Output "Copying sysprepped VHDX to $vhdParentPath\$vmName.vhdk"
Copy-Item -Path "$VHDPath" -Destination "$vhdParentPath\$vmName.vhdx" -Force
}
}
else
{
Write-Output "VHD for VM $vmName already exists, and Clobber was not specified. Skipping VM VHD creation."
}
}
else
{
#create new differencing disks based on the template disk
if ($DifferencingDisks)
{
Write-Output "Creating differencing disk at $vhdParentPath\$vmName.vhdk"
$newVHD = New-VHD -Path "$vhdParentPath\$vmName.vhdx" -ParentPath "$VHDPath" -Differencing
}
else #do a full copy
{
Write-Output "Copying sysprepped VHDX to $vhdParentPath\$vmName.vhdk"
Copy-Item -Path "$VHDPath" -Destination "$vhdParentPath\$vmName.vhdx" -Force
}
}
if (!(Get-VM $vmName -ErrorAction Ignore))
{
#Create the new VMs based on the template VHDX
$null = New-VM -Name $vmName -MemoryStartupBytes $VmMemory -VHDPath "$vhdParentPath\$vmName.vhdx" -Generation 2
#Create and configure the vNICs, assigning the static MAC addresses to the DscPush-vSwitch1 connected NICs
Connect-VMNetworkAdapter -VMName $vmName -Name "Network Adapter" -SwitchName $VSwitchName
Set-VMNetworkAdapter -VMName $vmName -Name "Network Adapter" -StaticMacAddress $vmMac
}
Write-Output "Starting VM $vmName"
Start-VM -Name $vmName -WarningAction SilentlyContinue
#Remove dashes from MAC address for Set-VmGuestIpAddress function requirements
$vmMac = $vmMac.Replace('-','')
Write-Output "Configuring target item $vmName"
while (!($remoteConnectTest))
{
sleep 1
Set-VmGuestIpAddress -VmName $vmName -MacAddress $vmMac -IpAddress $vmIp -Subnet $Subnet -ErrorAction Ignore
$remoteConnectTest = Test-WSMan $vmIp -ErrorAction Ignore
}
$i = 1
while ($AdapterCount -lt $i)
{
$null = Add-VMNetworkAdapter -VMName $vmName -SwitchName $VSwitchName
$i++
}
}
}
}
try
{
$vmConfig = @{
VmList = $vmList
VHDPath = $VhdPath
VSwitchName = $VSwitchName
Subnet = $TargetSubnet
AdapterCount = $AdapterCount
RemoteCred = $Credential
Clobber = $Clobber
DifferencingDisks = $DifferencingDisks
}
ConfigureVM @vmConfig
}
finally
{
#Always reset the trusted host setting
Set-Item -Path WSMan:\localhost\Client\TrustedHosts -Value $currentTrustedHost -Force
}
#endregion