-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.ps1
274 lines (241 loc) · 8.69 KB
/
create.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
$ErrorActionPreference = "Stop"
Import-Module "$PSScriptRoot\modules\common-utils.psm1"
Import-Module "$PSScriptRoot\modules\network-utils.psm1"
Import-Module "$PSScriptRoot\modules\rqmt-testers.psm1"
$BaseDirectory = $HOME
Set-ColorVariables # $Red, $Green, $Warning
Write-Host "Is PowerShell running as administrator?" -NoNewline
If (Test-PSRunsNotAsAdmin) {
Write-Host " No." @Red
Write-Host "You need to run PowerShell as administrator." @Warning
Return
}
Write-Host " Yes." @Green
Write-Host "Is Hyper-V available for your operating system?" -NoNewline
If (Test-OsNotSupported) {
Write-Host " No." @Red
Write-Host "You need 64-bit Windows 10 Pro, Enterprise, or Education." @Warning
Return
}
Write-Host " Yes." @Green
Write-Host "Is the hardware compatible (DEP, SLAT, V12N, VT-x/AMD-V)?" -NoNewline
If (Test-HardwareNotCompatible) {
Write-Host " No." @Red
Write-Host "For more information, visit https://bit.ly/38YlXL4." @Warning
Return
}
Write-Host " Yes." @Green
Write-Host "Is there at least 4 GB of installed RAM?" -NoNewline
If (Test-RamNotEnough) {
Write-Host " No." @Red
Write-Host "Before proceeding, you need to install more RAM." @Warning
Return
}
Write-Host " Yes." @Green
Write-Host "Is Hyper-V already enabled on your system?" -NoNewline
If (Test-HyperVNotEnabled) {
Write-Host " No." @Red
$Decision = Read-Host "Do you want to enable Hyper-V (restart required)? [y/N]"
If (Test-DecisionPositive $Decision) {
Enable-HyperV
}
Else {
Write-Host "Before proceeding, you need to enable Hyper-V." @Warning
}
Return
}
Write-Host " Yes." @Green
$DriveLetter = $BaseDirectory[0]
Write-Host "Is there at least 15 GB of free storage ($DriveLetter drive)?" -NoNewline
If (Test-StorageNotEnough $DriveLetter) {
Write-Host " No." @Red
Write-Host "Before proceeding, you need to free up some memory." @Warning
Return
}
Write-Host " Yes." @Green
Write-Host
Write-Host "Generating names for VM, vSwitch, and NAT..." -NoNewline
$Trials = 0
While ($True) {
$Trials++
If ($Trials -Gt 100) {
Write-Host " Failed." @Red
Write-Host "We've tried many random names. All of them were taken." @Warning
Return
}
$RandomLetters = Get-RandomLetters -Count 5
$VmName = "Ubuntu-$RandomLetters"
$VmPath = "$BaseDirectory\$VmName"
$SwitchName = "Switch-$RandomLetters"
$NatName = "NAT-$RandomLetters"
$NamesAvailable = Test-NamesAvailable $VmName $SwitchName $NatName
$FolderAvailable = Test-PathAvailable $VmPath
If ($NamesAvailable -And $FolderAvailable) {
Write-Host " Done." @Green
Break
}
}
Write-Host "Looking for an available subnet..." -NoNewline
$Trials = 0
While ($True) {
$Trials++
If ($Trials -Gt 100) {
Write-Host " Failed." @Red
Write-Host "We've tried many random subnets. All of them were taken." @Warning
Return
}
$RandomByte = Get-RandomInteger -Start 0 -End 255
$Subnet = @{ ID = "192.168.$RandomByte.0"; PrefixLength = 24 }
If (Test-SubnetAvailable $Subnet.ID) {
Write-Host " Done." @Green
Break
}
}
Write-Host
$Decision = Read-Host "The virtual machine is ready to be created. Continue? [Y/n]"
If (Test-DecisionNegative $Decision) {
Write-Host
Write-Host "Up until now nothing has been changed or created on your computer."
Write-Host "Run the script again when you're ready to start over. Bye!"
Return
}
Write-Host
Write-Host "Creating '$VmPath' folder for VM..." -NoNewline
New-Item -ItemType "Directory" -Path $VmPath | Out-Null
Set-Location $VmPath
Write-Host " Done." @Green
Write-Host "Downloading installation media (iso file)..." -NoNewline
$IsoUrl = "https://releases.ubuntu.com/20.04.1/ubuntu-20.04.1-live-server-amd64.iso"
$IsoFileName = $IsoUrl.Split("/")[-1]
$BitsTransferParams = @{
Source = $IsoUrl
Destination = $IsoFileName
Description = "File: '$VmPath\$IsoFileName'"
DisplayName = "Downloading installation media..."
}
Start-BitsTransfer @BitsTransferParams
Write-Host " Done." @Green
Write-Host "Checking '$IsoFileName' hash..." -NoNewline
$ActualHash = (Get-FileHash $IsoFileName -Algorithm "SHA256").Hash
$ExpectedHash = "443511f6bf12402c12503733059269a2e10dec602916c0a75263e5d990f6bb93"
If ($ActualHash -Ne $ExpectedHash) {
Write-Host
Write-Host "Something went wrong. Hashes do not match!" @Warning
Write-Host "Try to run the script again later."
Return
}
Write-Host " Done." @Green
Write-Host "Creating an internal virtual switch..." -NoNewline
$NewSwitchParams = @{
Name = $SwitchName
SwitchType = "Internal"
}
New-VMSwitch @NewSwitchParams | Out-Null
Write-Host " Done." @Green
Write-Host "Setting an IP address for the vNIC..." -NoNewline
$VNicAddress = Add-IntToIPAddress -IPAddress $Subnet.ID -Number 1
$NewIPParams = @{
IPAddress = $VNicAddress
InterfaceAlias = "vEthernet ($SwitchName)"
PrefixLength = $Subnet.PrefixLength
}
New-NetIPAddress @NewIPParams | Out-Null
Write-Host " Done." @Green
Write-Host "Configuring network address translation (NAT)..." -NoNewline
$SubnetInCidrFormat = $Subnet.ID + "/" + $Subnet.PrefixLength
$NewNatParams = @{
Name = $NatName
InternalIPInterfaceAddressPrefix = $SubnetInCidrFormat
}
New-NetNat @NewNatParams | Out-Null
Write-Host " Done." @Green
Write-Host
Write-Host " Write Down The Following Values " @Warning
$NumberOfDashes = 39
Write-Host $("-" * $NumberOfDashes) @Warning
$VMAddress = Add-IntToIPAddress -IPAddress $VNicAddress -Number 1
$NetworkInfo = [Ordered]@{
"Subnet" = $SubnetInCidrFormat
"Address" = $VMAddress
"Gateway" = $VNicAddress
"Name servers" = "1.1.1.1, 8.8.8.8"
"Search domains" = ""
}
$NetworkInfo.Keys | ForEach-Object { "{0, 15} ..... {1}" -F ($_, $NetworkInfo.$_) }
Write-Host $("-" * $NumberOfDashes) @Warning
Write-Host
Write-Host "Creating a virtual machine..." -NoNewline
$NewVMParams = @{
Name = $VmName
MemoryStartupBytes = 2GB
SwitchName = $SwitchName
NewVHDPath = "$VmPath\$VmName.vhdx"
NewVHDSizeBytes = 30GB
Path = $BaseDirectory
Generation = 2
}
New-VM @NewVMParams | Out-Null
Write-Host " Done." @Green
Write-Host "Configuring the virtual machine..." -NoNewline
$SetVMParams = @{
Name = $VmName
ProcessorCount = Get-NumberOfLogicalProcessors
DynamicMemory = $True
MemoryMinimumBytes = 0.5GB
MemoryMaximumBytes = Select-MaxRamAmount
AutomaticStartAction = "StartIfRunning"
AutomaticStopAction = "Save"
AutomaticStartDelay = 0
}
Set-VM @SetVMParams
Write-Host " Done." @Green
Write-Host "Adding a virtual DVD drive using the iso file..." -NoNewline
Add-VMDvdDrive -VMName $VmName -Path "$VmPath\$IsoFileName"
Write-Host " Done." @Green
Write-Host "Setting the VM to boot off of the DVD drive..." -NoNewline
$VMDvdDrive = Get-VMDvdDrive -VMName $VmName
$FirmwareParams = @{
VMName = $VmName
FirstBootDevice = $VMDvdDrive
EnableSecureBoot = "Off"
}
Set-VMFirmware @FirmwareParams
Write-Host " Done." @Green
$ScriptsPath = "$VmPath\Scripts"
Write-Host "Copying the scripts to '$ScriptsPath'..." -NoNewline
New-Item -ItemType "Directory" -Path $ScriptsPath | Out-Null
Copy-Item -Path "$PSScriptRoot\provision.ps1" -Destination $ScriptsPath
Copy-Item -Path "$PSScriptRoot\ssh.ps1" -Destination $ScriptsPath
Copy-Item -Path "$PSScriptRoot\uninstall.ps1" -Destination $ScriptsPath
$ModulesPath = "$VmPath\Scripts\modules"
New-Item -ItemType "Directory" -Path $ModulesPath | Out-Null
Copy-Item -Path "$PSScriptRoot\modules\common-utils.psm1" -Destination $ModulesPath
Copy-Item -Path "$PSScriptRoot\modules\value-testers.psm1" -Destination $ModulesPath
Write-Host " Done." @Green
Write-Host "Saving some data to 'Scripts\variables.csv'..." -NoNewline
$DataToExport = @{
VmName = $VmName
VmPath = $VmPath
SwitchName = $SwitchName
NatName = $NatName
VMAddress = $VMAddress
}
Export-VariablesToCsv -Variables $DataToExport -Path "$ScriptsPath\variables.csv"
Write-Host " Done." @Green
Write-Host
$Decision = Read-Host "The virtual machine is ready to be started. Continue? [Y/n]"
If (Test-DecisionNegative $Decision) {
Write-Host
Write-Host "Your virtual machine is available in the Hyper-V Manager."
Write-Host "Thank you for choosing my script. Bye!"
Return
}
Write-Host
Write-Host "Starting and connecting to the virtual machine..." -NoNewline
Start-VM -Name $VmName
$HostName = [System.Net.Dns]::GetHostName()
VMConnect $HostName $VmName
Write-Host " Done." @Green
Write-Host
Write-Host "Your virtual machine is also available in the Hyper-V Manager."
Write-Host "Thank you for choosing my script. Bye!"