Skip to content

Commit 662b2f9

Browse files
authored
Resiliency for Get-WindowsOptionalFeature (#3382)
Solves #3378 by catching the specific error and continue. This is the output of `New-BcContainer` when running as administrator and having the mentioned issue with `Get-WindowsOptionalFeature -online`: ``` BcContainerHelper is running as administrator Cannot check Hyper-V status. HyperV is ``` At least we do not stop anymore with this.
1 parent e2c6bb2 commit 662b2f9

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

BcContainerHelper.psm1

+15-5
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,22 @@ if ($useVolumes -or $isInsideContainer) {
3131
$hypervState = ""
3232
function Get-HypervState {
3333
if ($isAdministrator -and $hypervState -eq "") {
34-
$feature = Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V -Online
35-
if ($feature) {
36-
$script:hypervState = $feature.State
34+
try {
35+
$feature = Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V -Online
36+
if ($feature) {
37+
$script:hypervState = $feature.State
38+
}
39+
else {
40+
$script:hypervState = "Disabled"
41+
}
3742
}
38-
else {
39-
$script:hypervState = "Disabled"
43+
catch {
44+
if ($_.Exception.Message -match "Class not registered") {
45+
Write-Host "Cannot check Hyper-V status."
46+
}
47+
else {
48+
throw $_
49+
}
4050
}
4151
}
4252
return $script:hypervState

0 commit comments

Comments
 (0)