From 662b2f940a0654fca9c90518dfdda6ec853a078b Mon Sep 17 00:00:00 2001 From: Johannes Wikman Date: Mon, 4 Mar 2024 05:41:07 +0100 Subject: [PATCH] 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. --- BcContainerHelper.psm1 | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/BcContainerHelper.psm1 b/BcContainerHelper.psm1 index eee99213e..ff925ccf8 100644 --- a/BcContainerHelper.psm1 +++ b/BcContainerHelper.psm1 @@ -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