Skip to content

Commit e63f223

Browse files
anjannathpraveenkumar
authored andcommitted
choco: handle error during enabling of hyper-v
catch error that might be thrown by cmdlet enabling hyper-v and fail the installation if error occurs
1 parent 0135fca commit e63f223

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

packaging/chocolatey/crc/tools/crcprerequisitesetup.ps1

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,29 @@ function Install-Hyperv {
2121
if ($enabled) {
2222
Write-Host "Hyper-V is already enabled"
2323
} else {
24-
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart | Out-Null
25-
Write-Host -ForegroundColor Red "Hyper-V has been enabled, please reboot to complete installation"
24+
try {
25+
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart | Out-Null
26+
Write-Host -ForegroundColor Red "Hyper-V has been enabled, please reboot to complete installation"
27+
}
28+
catch {
29+
Write-Host -ForegroundColor Red "Failed to enable Hyper-V, aborting installation"
30+
Set-PowershellExitCode 1
31+
}
2632
}
2733
}
2834
"Server" {
2935
$enabled = (Get-WindowsFeature -Name "Hyper-V" | Select-Object -ExpandProperty InstallState).ToString().Equals("Installed")
3036
if ($enabled) {
3137
Write-Host "Hyper-V is already enabled"
3238
} else {
33-
Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -Confirm:$false -Restart:$false | Out-Null
34-
Write-Host -ForegroundColor Red "Hyper-V has been enabled, please reboot to complete installation"
39+
try {
40+
Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -Confirm:$false -Restart:$false | Out-Null
41+
Write-Host -ForegroundColor Red "Hyper-V has been enabled, please reboot to complete installation"
42+
}
43+
catch {
44+
Write-Host -ForegroundColor Red "Failed to enable Hyper-V, aborting installation"
45+
Set-PowershellExitCode 1
46+
}
3547
}
3648
}
3749
}

0 commit comments

Comments
 (0)