-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreateLinuxPxeVM.ps1
More file actions
165 lines (140 loc) · 4.91 KB
/
CreateLinuxPxeVM.ps1
File metadata and controls
165 lines (140 loc) · 4.91 KB
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
# CreateLinuxPXE.ps1
# ------------------
# Written as part of a blog on the AMIS website, see https://technology.amis.nl or the pdf in this repository for more information
#
# Please look carefully to these settings, these will be used for configuration of the Hyper-V VM on your local machine
#
# Debugging
$VerbosePreference="Continue"
# HostName = name of the physical machine where Hyper-V runs (this machine?)
# VM name = name of the VM that we want to build
$HostName="Gebruiker-PC"
$VMName="LinuxPxe"
# Directories
$VMBaseDirPath="D:\VMs"
$VirtualHardDiskPath="$VMBaseDirPath\Virtual Hard Disks"
$VMPath="$VMBaseDirPath\Virtual Machines"
$DVDISOFile="D:\Install\CentOS\CentOS-8-x86_64-1905-dvd1.iso"
# Components of the VM
$DiskName="$VirtualHardDiskPath\$VMName.vhdx"
# Other parameters
$continue=$True
$NumberOfProcesses=2
$DiskSize=129Gb
$MemoryStartupBytes=2048Mb
$Generation=1
$SwitchType="external"
$SwitchName=""
$VMExists=$false
function createDirectory {
param ($DirName)
if (Test-Path $DirName -pathtype Container) {
Write-Verbose "Path $DirName already exists, no problem"
} else {
Write-Verbose "Create path $DirName"
Try {
New-Item -path $DirName -ItemType directory -ErrorAction Stop | Out-Null
} Catch {
Write-Verbose "Unable to create path $DirName"
exit 1
}
}
}
function createVHD {
param($DiskName)
if (Test-Path $DiskName -PathType Leaf) {
Write-Warning "Disk $DiskName already exists, will be used for further deployment"
} else {
Write-Verbose "Create disk $DiskName"
Try {
New-VHD -Path "$DiskName" -SizeBytes $DiskSize -ErrorAction Stop | Out-Null
} Catch {
Write-Error "Create disk failed, command was: New-VHD -Path ${DiskName} -SizeBytes ${DiskSize} -ErrorAction Stop"
exit 2
}
}
}
function getSwitchName {
param($SwitchType)
Write-Verbose "Get name of the $SwitchType switch"
Try {
$SwitchName = (Get-VMSwitch -SwitchType $SwitchType -ErrorAction Stop -ErrorVariable $ev)[0].Name
Write-Verbose "Name of the $SwitchType switch = $SwitchName"
} Catch {
Write-Error "Error: $v"
Write-Error "Cannot determine switchname for $SwitchType switch"
exit 3
}
return $SwitchName
}
function testVMExists {
param($HostName, $VMName)
Try {
Get-VM -ComputerName $HostName -VMName $VMName -ErrorAction Stop | Out-Null
Write-Error "$VMName already exists on $HostName, please configure by hand"
exit 4
} Catch {
# Assume the error is caused by a non-existing VM, continue
Write-Verbose "VM $VMName doesn't exist on $HostName, continue..."
}
}
function createVM {
param($VMName,$MemoryStartupBytes, $Generation, $VMPath, $DiskName, $SwitchName)
Write-Verbose "Create new VM $VMName..."
Try {
New-VM -Name $VMName -MemoryStartupBytes $MemoryStartupBytes -Generation $Generation -BootDevice CD -Path $VMPath -VHDPath $DiskName -SwitchName $SwitchName -ErrorAction Stop -ErrorVariable $v
} Catch {
Write-Error "Error creating new VM: $v"
exit 5
}
}
function insertDVD {
param($VMName, $DVDISOFile)
Write-Verbose "Insert DVD $DVDISOFile ..."
Try {
Set-VMDVDDrive -VMName $VMName -Path $DVDISOFile -Controllernumber 1 -ControllerLocation 0 -ErrorAction Stop
} Catch {
Write-Error "Error while inserting DVD ISO $DVDISOFile in the VM $VMName"
exit 6
}
}
function useDynamicMemory {
param($VMName)
$currentDynamicMemoryEnabled = (Get-VMMemory -VMName $VMName | Select -Property DynamicMemoryEnabled).DynamicMemoryEnabled
if ($CurrentDynamicMemoryEnabled) {
Write-Verbose "Dynamic Memory is already enabled"
} else {
Try {
Write-Verbose "Enable Dynamic Memory"
Set-VMMemory -VMName $VMName -DynamicMemoryEnabled:$true
} Catch {
Write-Error "Unable to enable Dynamic Memory, continue..."
}
}
}
function numberOfProcessors {
param($VMName, $numberOfProcessors)
$currentNumberOfProcessors = (Get-VMProcessor -VMName $VMName | Select -Property Count).count
if ($CurrentNumberOfProcessors -eq $numberOfProcessors) {
Write-Verbose "Already $numberOfProcessors processors"
} else {
Try {
Write-Verbose "Change number of processors from $currentNumberOfProcessors to $numberOfProcessors"
Set-VMProcessor -VMName $VMName -count $numberOfProcessors
} Catch {
Write-Error "Unable to change number of processors to $numberOfProcessors ..."
}
}
return $continue
}
# Main program
createDirectory -DirName $VMBaseDirPath
createDirectory -DirName $VirtualHardDiskPath
createDirectory -DirName $VMPath
createVHD -DiskName $DiskName
$SwitchName = getSwitchName -SwitchType $SwitchType
testVMExists -HostName $HostName -VMName $VMName
createVM -VMName $VMName -MemoryStartupBytes $MemoryStartupBytes -Generation $Generation -VMPath $VMPath -DiskName $DiskName -SwitchName $SwitchName
insertDVD -VMName $VMName -DVDISOFile $DVDISOFile
useDynamicMemory -VMName $VMName
numberOfProcessors -VMName $VMName -NumberOfProcessors $NumberOfProcesses