-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDemoVMs-Helper.psm1
419 lines (355 loc) · 14.5 KB
/
DemoVMs-Helper.psm1
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
Function New-DemoVMs
{
param(
[Parameter(Mandatory = $true)]
[string[]]$VMName,
[Parameter(Mandatory = $true)]
[string]$VirtualSwitchName,
[Parameter(Mandatory = $true)]
[int]$ProcessorCount,
[Parameter(Mandatory = $true)]
[int]$Generation,
[Parameter(Mandatory = $false)]
[string]$ImagesLocation = 'D:\Hyper-V\Images',
[Parameter(Mandatory = $false)]
[string]$VMsLocation = 'D:\Hyper-V\VMs',
[Parameter(Mandatory = $false)]
[int]$Datadisks = 0,
[Parameter(Mandatory = $false)]
[System.Int64]$Datadisksize = 40GB,
[Parameter(Mandatory = $false)]
[System.Int64]$Memory = 2GB,
[Parameter(Mandatory = $false)]
[switch]$Snapshot,
[Parameter(Mandatory = $false)]
[string]$Snapshotname = 'Clean Build'
)
#region runas administrator
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] 'Administrator'))
{
Write-Warning -Message "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
Break
}
#endregion
#region check Hyper-V Role Installed
if (((Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V).state) -ne 'Enabled')
{
Write-Warning -Message 'Hyper-V: Hyper-V Role has not yet been enabled!'
if ((Read-Question -Question 'Would you like to Enable Hyper-V?') -eq '0')
{
$null = Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
if ((Read-Question -Question 'Would you like to restart your computer to finish the Hyper-V Installation?') -eq '0')
{
Restart-Computer -Force
break
}
else
{
Write-Warning -Message 'Hyper-V: Hyper-V Role has now been installed, however a reboot is required!'
Pause
break
}
}
else
{
Write-Warning -Message 'Hyper-V: Hyper-V Role is required to continue. Exiting Script.'
Pause
break
}
}
elseif (((Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V).RestartNeeded) -eq $true)
{
Write-Warning -Message 'Hyper-V: Hyper-V Role has already been installed, however a reboot is required!'
if ((Read-Question -Question 'Would you like to restart your computer to finish the Hyper-V Installation?') -eq '0')
{
Restart-Computer -Force
break
}
else
{
Pause
break
}
}
else
{
Write-Verbose -Message 'Hyper-V: Hyper-V Role was found' -Verbose
}
#endregion
#region check hyper-v switch
if (!(Get-VMSwitch | Where-Object -FilterScript {
$_.name -eq $VirtualSwitchName
}))
{
Write-Warning -Message "Hyper-V: Switch [$($VirtualSwitchName)] could not be found!"
if ((Read-Question -Question "Would you like to create the virtual switch [$($VirtualSwitchName)]?") -eq '0')
{
$null = New-VMSwitch -Name $VirtualSwitchName -SwitchType Private
}
else
{
Pause
break
}
}
else
{
Write-Verbose -Message "Hyper-V: Switch [$($VirtualSwitchName)] was found." -Verbose
}
#endregion
#region scanning images from imagelocation
try
{
# Get image name
$Imagename = Show-Dropdownbox -Question 'Pick an Image' -Answers (((Get-Item -Path "$ImagesLocation\*.vhd*" -ErrorAction SilentlyContinue)).name)
}
catch
{
}
$imageextension = $Imagename.Split('.') | Select-Object -Last 1
#endregion
#region create virtual machines
if (!$Imagename)
{
Write-Warning -Message "No images could be found in location $ImagesLocation, please select a different location using [DeployDemoVMs.ps1 -ImagesLocation 'Your Images Location']"
break
}
else
{
foreach ($VM in $VMName)
{
if (Get-VM -Name $VM -ErrorAction SilentlyContinue)
{
Write-Warning -Message "$($VM): Virtual Machine already exists!"
break
}
Write-Verbose -Message "$($VM): Creating Virtual Machine" -Verbose
$null = New-VM -Name $VM -Generation $Generation -Path $VMsLocation -SwitchName $VirtualSwitchName -NoVHD
Write-Verbose -Message "$($VM): Creating differencing disk" -Verbose
$null = New-VHD -Path "$VMsLocation\$VM\C-Drive.$($imageextension)" -ParentPath "$ImagesLocation\$Imagename" -Differencing
Write-Verbose -Message "$($VM): Adding differencing disk to Virtual Machine" -Verbose
$null = Get-VM -name $VM | Add-VMHardDiskDrive -Path "$VMsLocation\$VM\C-Drive.$($imageextension)"
if ($Generation -eq 2)
{
Write-Verbose -Message "$($VM): Changing Harddisk to first boot device" -Verbose
$null = Get-VM $VM | Set-VMFirmware -FirstBootDevice (Get-VMHardDiskDrive -VMName $VM)
}
if ($Datadisks -ne 0)
{
$i = 0
do
{
$i++
Write-Verbose -Message "$($VM): Creating data disk $i" -Verbose
$null = New-VHD -Path "$VMsLocation\$VM\DataDisk$($i).$($imageextension)" -Dynamic -SizeBytes $Datadisksize
Write-Verbose -Message "$($VM): Adding data disk $i to Virtual Machine" -Verbose
$null = Get-VM -name $VM | Add-VMHardDiskDrive -Path "$VMsLocation\$VM\DataDisk$($i).$($imageextension)"
}
until ($Datadisks -eq $i)
}
Write-Verbose -Message "$($VM): Changing configuration to $ProcessorCount vCPUs and $($Memory/1gb)GB Memory" -Verbose
$null = Get-VM -name $VM |
Set-VM -ProcessorCount $ProcessorCount -DynamicMemory -MemoryMaximumBytes $Memory -Passthru |
Start-VM
if ($Snapshot)
{
Write-Verbose -Message "$($VM): Creating '$Snapshotname' Snapshot" -Verbose
$null = Get-VM -name $VM | Checkpoint-VM -SnapshotName $Snapshotname
}
}
}
Write-Verbose -Message 'Deploy Completed!' -Verbose
#endregion
}
Function Remove-DemoVMs
{
param(
[Parameter(Mandatory = $true)]
[string[]]$VMNames
)
#region runas administrator
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] 'Administrator'))
{
Write-Warning -Message "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
Break
}
#endregion
Foreach ($VMName in $VMNames)
{
$VM = Get-VM -Name $VMName -ErrorAction SilentlyContinue
if ($VM -eq $null)
{
Write-Warning -Message "$($VMName): Virtual Machine could not be found!"
Break
}
Write-Verbose -Message "$($VMName): Virtual Machine was found with state [$($VM.state)]!" -Verbose
if ($VM.state -eq 'Running')
{
Write-Warning -Message "$($VMName): Virtual Machine was found in state [$($VM.state)]!"
if ((Read-Question -Question "Would you like to continue to remove $VMName ?") -eq 1)
{
Write-Warning -Message "$($VMName): Virtual Machine removal was aborted!"
break
}
}
if ($VM.state -ne 'Off')
{
Write-Verbose -Message "$($VMName): Attempting to stop Virtual Machine! Previous state [$($VM.state)]" -Verbose
$null = Stop-VM -Name $VMName -Force
}
if ($VM.state -ne 'Off')
{
Write-Warning -Message "$($VMName): Virtual Machine is still running! Please resolve the issues and then try again!"
Break
}
$VMLocation = $VM.Path
Write-Verbose -Message "$($VMName): Removing Virtual Machine Configuration" -Verbose
$null = Remove-VM -Name $VMName -Force
Write-Verbose -Message "$($VMName): Removing Virtual Machine Directory" -Verbose
$null = Remove-Item $VMLocation -Recurse -Force
Write-Verbose -Message "$($VMName): Remove Completed!" -Verbose
}
}
Function Show-Dropdownbox
{
<#
.SYNOPSIS
Gives a pop-up to select data, that can be used in other scripts
.DESCRIPTION
The Show-Dropdownbox function is used to provide a friendly way for users to input data into script variables.
This script is for example used during the postbuild script, so that information can be injected in a friendly way.
.PARAMETER Question
The question you want to have displayed on screen.
.PARAMETER Answers
The answers you would like the users to have as options. (Static list)
.EXAMPLE
$variable1 = Show-Dropdownbox -question "Select one of the following:" -answers "A","B","C"
Create a question and write the output to variable $variable1
.NOTES
This function was created for friendly input into scripts that are performed manually.
Do not use this function in scripts that should run in the background.
This script was written by Danny den Braver @2013, for questions please contact [email protected]
#>
param(
[Parameter(Mandatory = $true)]
[string]$Question,
[Parameter(Mandatory = $true)]
[array]$Answers
)
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Drawing')
$objForm = New-Object -TypeName System.Windows.Forms.Form
$objForm.Text = 'Take your selection'
$objForm.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (300, 275)
$objForm.StartPosition = 'CenterScreen'
$objForm.KeyPreview = $true
$objForm.Add_KeyDown({
if ($_.KeyCode -eq 'Enter')
{
$objForm.Close()
}
})
$objForm.Add_KeyDown({
if ($_.KeyCode -eq 'Escape')
{
$objForm.Close()
}
})
$OKButton = New-Object -TypeName System.Windows.Forms.Button
$OKButton.Location = New-Object -TypeName System.Drawing.Size -ArgumentList (75, 200)
$OKButton.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (75, 23)
$OKButton.Text = 'OK'
$OKButton.Add_Click({
$objForm.Close()
})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object -TypeName System.Windows.Forms.Button
$CancelButton.Location = New-Object -TypeName System.Drawing.Size -ArgumentList (150, 200)
$CancelButton.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (75, 23)
$CancelButton.Text = 'Cancel'
$CancelButton.Add_Click({
$objForm.Close()
})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object -TypeName System.Windows.Forms.Label
$objLabel.Location = New-Object -TypeName System.Drawing.Size -ArgumentList (10, 20)
$objLabel.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (280, 20)
$objLabel.Text = $Question
$objForm.Controls.Add($objLabel)
$objListBox = New-Object -TypeName System.Windows.Forms.ListBox
$objListBox.Location = New-Object -TypeName System.Drawing.Size -ArgumentList (10, 40)
$objListBox.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (275, 20)
$objListBox.Height = 150
foreach ($answer in $Answers)
{
[void]$objListBox.Items.Add($answer)
}
$objListBox.SelectedItem = $objListBox.Items[0]
$objForm.Controls.Add($objListBox)
$objForm.Topmost = $true
$objForm.Add_Shown({
$objForm.Activate()
})
[void]$objForm.ShowDialog()
$objListBox.SelectedItem
}
Function Read-Question
{
param(
[Parameter(Mandatory = $true)]
[string]$Question
)
$subject = 'Read-Question'
$yes = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes', ''
$no = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&No', ''
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$Host.UI.PromptForChoice($subject,$Question,$choices,1)
}
Function Set-DemoVMsIP
{
param(
[Parameter(Mandatory = $true)]
[string]$VMName,
[Parameter(Mandatory = $true)]
[string]$IPAddress,
[Parameter(Mandatory = $false)]
[string]$Subnet = '255.255.255.0',
[Parameter(Mandatory = $false)]
[string]$Gateway,
[Parameter(Mandatory = $false)]
[string[]]$DNS
)
#region runas administrator
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] 'Administrator'))
{
Write-Warning -Message "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
Break
}
#endregion
$VSManService = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_VirtualSystemManagementService
$getVMobj = Get-WmiObject -Namespace 'root\virtualization\v2' -Class 'msvm_computersystem' | Where-Object -FilterScript {
$_.ElementName -like "*$VMName*"
}
$VMSettings = $getVMobj.GetRelated('MSVM_VirtualSystemSettingData') | Where-Object -FilterScript {
$_.VirtualSystemType -eq 'Microsoft:Hyper-V:System:Realized'
}
$VMNetAdapter = $VMSettings.GetRelated('Msvm_SyntheticEthernetPortSettingData')
$VMNetSettings = $VMNetAdapter.GetRelated('msvm_guestnetworkadapterconfiguration')
$LANSettings = $VMNetSettings | Select-Object -First 1
Write-Verbose -Message "$($VMName): Changing IP for Server to $IPAddress" -Verbose
$LANSettings.DHCPEnabled = $false
$LANSettings.IPAddresses = "$IPAddress"
$LANSettings.Subnets = "$Subnet"
if ($Gateway)
{
$LANSettings.DefaultGateways = $Gateway
}
if ($DNS)
{
$LANSettings.DNSServers = $DNS
}
$null = $VSManService.SetGuestNetworkAdapterConfiguration($getVMobj.Path, $LANSettings.GetText(1))
}