Skip to content

Commit

Permalink
Resiliency for Get-WindowsOptionalFeature (#3382)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jwikman authored Mar 4, 2024
1 parent e2c6bb2 commit 662b2f9
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions BcContainerHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,22 @@ if ($useVolumes -or $isInsideContainer) {
$hypervState = ""
function Get-HypervState {
if ($isAdministrator -and $hypervState -eq "") {
$feature = Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V -Online
if ($feature) {
$script:hypervState = $feature.State
try {
$feature = Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V -Online
if ($feature) {
$script:hypervState = $feature.State
}
else {
$script:hypervState = "Disabled"
}
}
else {
$script:hypervState = "Disabled"
catch {
if ($_.Exception.Message -match "Class not registered") {
Write-Host "Cannot check Hyper-V status."
}
else {
throw $_
}
}
}
return $script:hypervState
Expand Down

0 comments on commit 662b2f9

Please sign in to comment.