-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-LabVM.ps1
62 lines (57 loc) · 2.18 KB
/
New-LabVM.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
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[string]$LabName,
[Parameter(Mandatory = $true)]
[string]$VMHostname,
[Parameter(Mandatory = $true)]
[int]$MemoryStartupMB,
[Parameter(Mandatory = $true)]
[int]$MemoryMaximumMB,
[Parameter(Mandatory = $true)]
[int]$ProcessorCount,
[Parameter(Mandatory = $true)]
[string]$VirtualSwitchName,
[Parameter(Mandatory = $true)]
[int]$DiskCount,
[Parameter(Mandatory = $true)]
[string]$VHDPath,
[Parameter(Mandatory = $true)]
[int]$VHDSizeGB,
[Parameter(Mandatory = $true)]
[string]$ISOPath
)
$VMName = "$LabName-$VMHostname"
# Create VM
$Parameters = @{
Name = $VMName
MemoryStartupBytes = $MemoryStartupMB * 1048576
SwitchName = $VirtualSwitchName
NewVHDPath = "$VHDPath\$VMName-01.vhdx"
NewVHDSizeBytes = $VHDSizeGB * 1073741824
Generation = 2
}
New-VM @Parameters
# Configure VM
Set-VM -Name $VMName -AutomaticCheckpointsEnabled $false
Set-VM -Name $VMName -MemoryMaximumBytes ($MemoryMaximumMB * 1048576)
Set-VM -VMName $VMName -ProcessorCount $ProcessorCount
Add-VMDvdDrive -VMName $VMName
Set-VMDvdDrive -VMName $VMName -Path $ISOPath
Set-VMFirmware -VMName $VMName -BootOrder (Get-VMDvdDrive -VMName $VMName), (Get-VMHardDiskDrive -VMName $VMName)
# Enable TPM
$HgsGuardian = Get-HgsGuardian -Name UntrustedGuardian
$HgsKeyProtector = New-HgsKeyProtector -Owner $HgsGuardian -AllowUntrustedRoot
Set-VMKeyProtector -VMName $VMName -KeyProtector $HgsKeyProtector.RawData
Enable-VMTPM -VMName $VMName
Set-VMSecurity -VMName $VMName -EncryptStateAndVmMigrationTraffic $true
# Add additional disks if specified
if ($DiskCount -gt 1) {
Add-VMScsiController -VMName $VMName
for ($i = 2; $i -le $DiskCount; $i++) {
$DiskNumber = $i.ToString('00')
New-VHD -Path "$VHDPath\$VMName-$DiskNumber.vhdx" -SizeBytes ($VHDSizeGB * 1073741824)
Add-VMHardDiskDrive -VMName $VMName -ControllerType SCSI -ControllerNumber 1 -ControllerLocation ($i - 2)
Set-VMHardDiskDrive -VMName $VMName -ControllerType SCSI -ControllerNumber 1 -ControllerLocation ($i - 2) -Path "$VHDPath\$VMName-$DiskNumber.vhdx"
}
}