-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinBCD.ps1
365 lines (309 loc) · 15.2 KB
/
WinBCD.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
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
# Check administrator privileges
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Start-Process powershell "-File `"$PSCommandPath`"" -Verb RunAs
exit
}
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
function Console {
param (
[Switch]$Show,
[Switch]$Hide,
[string]$Text
)
if (-not ("Console.Window" -as [type])) {
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
}
if ($Show) {
$consolePtr = [Console.Window]::GetConsoleWindow()
$null = [Console.Window]::ShowWindow($consolePtr, 5)
}
if ($Hide) {
$consolePtr = [Console.Window]::GetConsoleWindow()
$null = [Console.Window]::ShowWindow($consolePtr, 0)
}
}
# Funcion para mostrar un dialogo desplegable
function Show-SelectionDialog {
param (
[string]$title,
[string]$prompt,
[array]$options
)
$form = New-Object System.Windows.Forms.Form
$form.Text = $title
$form.Size = New-Object System.Drawing.Size(410, 290)
$form.StartPosition = 'CenterScreen'
$form.MaximizeBox = $false
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$label = New-Object System.Windows.Forms.Label
$label.Text = $prompt
$label.AutoSize = $true
$label.Location = New-Object System.Drawing.Point(10, 20)
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Size = New-Object System.Drawing.Size(360, 150)
$listBox.Location = New-Object System.Drawing.Point(10, 50)
$listBox.Items.AddRange($options)
$form.Controls.Add($listBox)
$okButton = New-Object System.Windows.Forms.Button
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$okButton.Location = New-Object System.Drawing.Point(310, 220)
$form.Controls.Add($okButton)
$form.AcceptButton = $okButton
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$cancelButton.Location = New-Object System.Drawing.Point(220, 220)
$form.Controls.Add($cancelButton)
$form.CancelButton = $cancelButton
$form.Add_Shown({$form.Activate()})
[void]$form.ShowDialog()
if ($form.DialogResult -eq [System.Windows.Forms.DialogResult]::OK -and $listBox.SelectedItem -ne $null) {
return $listBox.SelectedItem
}
return $null
}
# Funcion para refrescar los nombres de las particiones
function Refresh-Partitions {
$comboBox.Items.Clear()
$drives = Get-WmiObject -Class Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 -or $_.DriveType -eq 5 -and $_.DeviceID -ne "$env:SystemDrive" }
foreach ($drive in $drives) {
$label = $drive.VolumeName
if ([string]::IsNullOrWhiteSpace($label)) {
$label = "Drive " + $drive.DeviceID
}
$comboBox.Items.Add("$($drive.DeviceID) - $label")
}
if ($comboBox.Items.Count -gt 0) {
$comboBox.SelectedIndex = 0
}
}
# Funcion para crear la particion a partir del nombre la iso
function Create-Partition {
param (
[string]$filePath
)
# Verificar si existe la particion
$volumeName = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
$checkpartition = Get-WMIObject Win32_Volume | Where-Object { $_.Label -eq $volumeName}
if ($checkpartition) {
[System.Windows.Forms.MessageBox]::Show("The partition already exists", "Info", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
# calcular el valor en GB, redondear para estar más seguro, verificar en que disco se encuentra
$fileSizeGB = [math]::Ceiling((Get-Item $filePath).Length / 1GB)
$systemDrive = $env:SystemDrive.TrimEnd(':')
$disk = Get-Partition -DriveLetter $systemDrive | Get-Disk
if ($disk -ne $null) {
# reducir el tamaño de la particion principal,
$systemPartition = Get-Partition -DriveLetter $systemDrive
Resize-Partition -InputObject $systemPartition -Size ($systemPartition.Size - ($fileSizeGB * 3GB))
# Crear la nueva particion
$partition = New-Partition -DiskNumber $disk.Number -Size ($partitionSizeGB * 3GB) -AssignDriveLetter
if ($partition -ne $null) {
$driveLetter = $partition.DriveLetter
# Determinar el sistema de archivos, obtener el nombre de la iso
$fileSystem = if ($fileSizeGB -lt 4) { "FAT32" } else { "NTFS" }
# formatear
Format-Volume -DriveLetter $driveLetter -FileSystem $fileSystem -NewFileSystemLabel $volumeName
[System.Windows.Forms.MessageBox]::Show("Partition created and formatted successfully", "Info", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
# refrescar
Refresh-Partitions
} else {
[System.Windows.Forms.MessageBox]::Show("Failed to create partition", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
}
} else {
[System.Windows.Forms.MessageBox]::Show("Failed to find the system disk", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
}
}
# Crear form, ocultar consola
Console -Hide
[System.Windows.Forms.Application]::EnableVisualStyles();
$form = New-Object System.Windows.Forms.Form
$form.Text = "WinBCD"
$form.ClientSize = New-Object System.Drawing.Size(300, 100)
$form.StartPosition = "CenterScreen"
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$form.AllowDrop = $true
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$form.KeyPreview = $true
$form.Add_KeyDown({
param($sender, $e)
if ($e.KeyCode -eq [System.Windows.Forms.Keys]::F5) {
Refresh-Partitions
}
})
$form.Add_Paint({
param (
[object]$sender,
[System.Windows.Forms.PaintEventArgs]$e
)
$rect = New-Object System.Drawing.Rectangle(0, 0, $sender.Width, $sender.Height)
$brush = New-Object System.Drawing.Drawing2D.LinearGradientBrush(
$rect,
[System.Drawing.Color]::FromArgb(44, 44, 44), # Color negro
[System.Drawing.Color]::FromArgb(99, 99, 99),# Color gris oscuro
[System.Drawing.Drawing2D.LinearGradientMode]::Vertical
)
$e.Graphics.FillRectangle($brush, $rect)
})
# Enumerar particiones
$comboBox = New-Object System.Windows.Forms.ComboBox
$comboBox.Location = New-Object System.Drawing.Point(65, 10)
$comboBox.Size = New-Object System.Drawing.Size(180, 20)
$comboBox.BackColor = [System.Drawing.Color]::FromArgb(44, 44, 44)
$comboBox.ForeColor = [System.Drawing.Color]::White
$comboBox.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10, 40)
$textBox.Size = New-Object System.Drawing.Size(250, 20)
$textBox.Enabled = $false
$textBox.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
$textBox.BackColor = [System.Drawing.Color]::FromArgb(44, 44, 44)
$textBox.ForeColor = [System.Drawing.Color]::White
# OpenFileDialog para seleccionar la imagen
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog.Filter = "ISO Files (*.iso)|*.iso|WIM Files (*.wim)|*.wim|ESD Files (*.esd)|*.esd"
$buttonSelectFile = New-Object System.Windows.Forms.Button
$buttonSelectFile.Location = New-Object System.Drawing.Point(270, 40)
$buttonSelectFile.Size = New-Object System.Drawing.Size(25, 20)
$buttonSelectFile.Text = "..."
$buttonSelectFile.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$buttonSelectFile.BackColor = [System.Drawing.Color]::FromArgb(44, 44, 44)
$buttonSelectFile.ForeColor = [System.Drawing.Color]::White
$buttonSelectFile.FlatAppearance.BorderSize = 0
$buttonSelectFile.Add_Click({
if ($openFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
$textBox.Text = $openFileDialog.FileName
$buttonCreatePartition.Enabled = $true
}
})
# Instalar
$buttonInstall = New-Object System.Windows.Forms.Button
$buttonInstall.Location = New-Object System.Drawing.Point(50, 70)
$buttonInstall.Size = New-Object System.Drawing.Size(75, 23)
$buttonInstall.Text = "Install"
$buttonInstall.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$buttonInstall.BackColor = [System.Drawing.Color]::FromArgb(44, 44, 44)
$buttonInstall.ForeColor = [System.Drawing.Color]::White
$buttonInstall.FlatAppearance.BorderSize = 0
$buttonInstall.Add_Click({
# Obtener la ruta de la imagen
$filePath = $textBox.Text
if ([string]::IsNullOrEmpty($filePath)) {
[System.Windows.Forms.MessageBox]::Show("Specify an ISO image", "Info", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
return
}
# Step 1: Confirmar montaje de la imagen
$driveLetter = ($comboBox.SelectedItem.ToString() -split ' ')[0]
$warningMessage = "Mounting the image ($filePath) will erase all files on drive $driveLetter. Do you want to continue?"
$confirmation = [System.Windows.Forms.MessageBox]::Show($warningMessage, "Warning", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Warning)
if ($confirmation -eq [System.Windows.Forms.DialogResult]::Yes) {
# Verificar si la imagen es .iso
if ([System.IO.Path]::GetExtension($filePath) -eq ".iso") {
try {
Mount-DiskImage -ImagePath "$filePath" | Out-Null
} catch {
[System.Windows.Forms.MessageBox]::Show("An error occurred while trying to mount the ISO", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
$mountedDrive = (Get-DiskImage -ImagePath "$filePath" | Get-Volume).DriveLetter
$esdPath = "$mountedDrive`:\sources\install.esd"
$wimPath = "$mountedDrive`:\sources\install.wim"
if (Test-Path $esdPath) {
$imgPath = $esdPath
} elseif (Test-Path $wimPath) {
$imgPath = $wimPath
} else {
[System.Windows.Forms.MessageBox]::Show("The selected ISO does not appear to be a Windows ISO. The ISO must contain either install.esd or install.wim in the 'sources' folder", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
Dismount-DiskImage -ImagePath "$filePath"
return
}
} else {
$imgPath = $filePath
}
# Step 2: Seleccionar la edicion
$windowsEditions = Get-WindowsImage -ImagePath $imgPath
$editionsList = $windowsEditions | ForEach-Object { $_.ImageName }
$selectedEdition = Show-SelectionDialog -title "Select a Windows edition" -prompt "Available editions:" -options $editionsList
if (-not $selectedEdition -or $selectedEdition -eq "Cancel") {
Write-Host "Edition selection canceled." -ForegroundColor Yellow
Dismount-DiskImage -ImagePath "$filePath"
return
}
$selectedIndex = $windowsEditions | Where-Object { $_.ImageName -eq $selectedEdition } | Select-Object -ExpandProperty ImageIndex
# Step 3: Aplicar la imagen
# Formatear, extraer imagen en la particion
Write-Host "Applying image (installing/extracting Windows)..." -ForegroundColor Yellow
Expand-WindowsImage -ImagePath "$imgPath" -ApplyPath "$driveLetter\" -Index $selectedIndex -LogLevel 2
# Step 7: Aplicar autounattend por si existe
if (Test-Path "$driveLetter\autounattend.xml") {
Write-Host "Applying unattend answer files..." -ForegroundColor Yellow
Use-WindowsUnattend -Path "$driveLetter\" -UnattendPath "$driveLetter\autounattend.xml"
if (Test-Path "$mountedDrive\sources\$OEM$\$$\Setup\Scripts") {
Copy-Item "$mountedDrive\sources\$OEM$\$$\Setup\Scripts\*" -Destination "$driveLetter\Windows\Setup\Scripts" -Recurse
}
Copy-Item "$mountedDrive\autounattend.xml*" -Destination "$driveLetter\Windows\Setup\Scripts" -Recurse
}
# Step 8: añadir la instalacion de windows al bootmgr
Write-Host "Adding new Windows installation to boot loader..." -ForegroundColor Yellow
bcdboot "$driveLetter\Windows" | Out-Null
Write-Host "Installation completed!" -ForegroundColor Green
$confirm = [System.Windows.Forms.MessageBox]::Show("Installation completed!`ndo you wish to reboot now?", "Info", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Information)
if ($confirm -eq [System.Windows.Forms.DialogResult]::Yes) {
Restart-Computer -Force
}
Dismount-DiskImage -ImagePath "$filePath"
} else {
Write-Host "Mounting canceled" -ForegroundColor Yellow
return
}
})
# Crear Particion
$buttonCreatePartition = New-Object System.Windows.Forms.Button
$buttonCreatePartition.Location = New-Object System.Drawing.Point(140, 70)
$buttonCreatePartition.Size = New-Object System.Drawing.Size(120, 23)
$buttonCreatePartition.Text = "Create Partition"
$buttonCreatePartition.Enabled = $false
$buttonCreatePartition.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$buttonCreatePartition.BackColor = [System.Drawing.Color]::FromArgb(44, 44, 44)
$buttonCreatePartition.ForeColor = [System.Drawing.Color]::White
$buttonCreatePartition.FlatAppearance.BorderSize = 0
$buttonCreatePartition.Add_Click({
Create-Partition -filePath $textBox.Text
})
# añadir Controles
$form.Controls.Add($comboBox)
$form.Controls.Add($textBox)
$form.Controls.Add($buttonSelectFile)
$form.Controls.Add($buttonInstall)
$form.Controls.Add($buttonCreatePartition)
# Drag & Drop
$form.Add_DragEnter({
if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop)) {
$_.Effect = [Windows.Forms.DragDropEffects]::Copy
}
})
$form.Add_DragDrop({
$files = $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)
if ($files.Count -eq 1) {
$filePath = $files[0]
$extension = [System.IO.Path]::GetExtension($filePath).ToLower()
if ($extension -eq ".iso" -or $extension -eq ".wim" -or $extension -eq ".esd") {
$textBox.Text = $filePath
$buttonCreatePartition.Enabled = $true
} else {
[System.Windows.Forms.MessageBox]::Show("Unsupported file format.`nPlease drag and drop an ISO, WIM, or ESD file.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
}
}
})
Refresh-Partitions
[void]$form.ShowDialog()