Skip to content

Commit 3df4971

Browse files
authored
Add files via upload
1 parent 31ab5a2 commit 3df4971

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

Scripts/Convert-VHDX-To-Qcow2.ps1

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Add-Type -AssemblyName System.Windows.Forms
2+
$FileDialog = New-Object System.Windows.Forms.OpenFileDialog -Property @{
3+
InitialDirectory = [Environment]::GetFolderPath('Desktop')
4+
Filter = 'VHDX Files (*.vhdx)|*.vhdx'
5+
}
6+
7+
Write-Host "Downloading necessary utilities..." -ForegroundColor Yellow
8+
Write-Host " "
9+
10+
$QemuPath = ".\qemu-img"
11+
if (Test-Path -Path $QemuPath) {
12+
Write-Host "Utilities already downloaded, continuing with the process..." -ForegroundColor Yellow
13+
Write-Host " "
14+
Set-Location -Path $QemuPath
15+
Write-Host "Select the .VHDX file to convert..." -ForegroundColor Yellow
16+
Write-Host " "
17+
Start-Sleep -Seconds 2
18+
$FileDialogResult = $FileDialog.ShowDialog()
19+
if ($FileDialogResult -eq [System.Windows.Forms.DialogResult]::OK) {
20+
$VhdxFile = $FileDialog.FileName
21+
$Extension = ".qcow2"
22+
# Get the file name without extension
23+
$FileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($VhdxFile)
24+
$FileDirectory = [System.IO.Path]::GetDirectoryName($VhdxFile)
25+
# Create the new path with the new extension
26+
$Qcow2File = [System.IO.Path]::Combine($FileDirectory, "$FileNameWithoutExtension$Extension")
27+
$FinalCommand = "& .\qemu-img.exe convert '$VhdxFile' -O qcow2 '$Qcow2File'"
28+
Write-Host "Converting file..." -ForegroundColor Yellow
29+
Write-Host " "
30+
Invoke-Expression $FinalCommand
31+
Write-Host "File successfully converted to: $Qcow2File" -ForegroundColor Green
32+
} else {
33+
Write-Host "No file selected, ending the process." -ForegroundColor Red
34+
}
35+
}
36+
else {
37+
try {
38+
Write-Host "Downloading utilities..." -ForegroundColor Yellow
39+
Invoke-WebRequest -Uri "https://cloudbase.it/downloads/qemu-img-win-x64-2_3_0.zip" -OutFile "qemu-img.zip"
40+
Expand-Archive -LiteralPath ".\qemu-img.zip" -DestinationPath ".\qemu-img"
41+
Remove-Item -Path ".\qemu-img.zip"
42+
Write-Host "Utilities downloaded successfully!" -ForegroundColor Green
43+
Write-Host " "
44+
Set-Location -Path $QemuPath
45+
Write-Host "Select the .VHDX file to convert..." -ForegroundColor Yellow
46+
Write-Host " "
47+
Start-Sleep -Seconds 2
48+
$FileDialogResult = $FileDialog.ShowDialog()
49+
if ($FileDialogResult -eq [System.Windows.Forms.DialogResult]::OK) {
50+
$VhdxFile = $FileDialog.FileName
51+
$Extension = ".qcow2"
52+
$FileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($VhdxFile)
53+
$FileDirectory = [System.IO.Path]::GetDirectoryName($VhdxFile)
54+
$Qcow2File = [System.IO.Path]::Combine($FileDirectory, "$FileNameWithoutExtension$Extension")
55+
$FinalCommand = "& .\qemu-img.exe convert '$VhdxFile' -O qcow2 '$Qcow2File'"
56+
Write-Host "Converting file..." -ForegroundColor Yellow
57+
Write-Host " "
58+
Invoke-Expression $FinalCommand
59+
Write-Host "File successfully converted to: $Qcow2File" -ForegroundColor Green
60+
Write-Host " "
61+
} else {
62+
Write-Host "No file selected, ending the process." -ForegroundColor Red
63+
Write-Host " "
64+
}
65+
}
66+
catch {
67+
Write-Host "ERROR: An error occurred during the utility download process..." -ForegroundColor Red
68+
Write-Host " "
69+
}
70+
}
71+
72+
cd ..
73+
Write-Host "Cleaning up downloaded resources..." -ForegroundColor Yellow
74+
Write-Host " "
75+
try {
76+
Remove-Item -LiteralPath ".\qemu-img" -Force -Recurse
77+
Write-Host "Resources cleaned up successfully!" -ForegroundColor Green
78+
}
79+
catch {
80+
Write-Host "ERROR: Unable to clean up resources..." -ForegroundColor Red
81+
}

0 commit comments

Comments
 (0)