Skip to content

Commit 8d39b5b

Browse files
authored
gracefully handle if servermanager not present (#436)
# Description This pull request updates the `Get-EnvironmentRole` function in `src/modules/SdnDiag.Utilities.psm1` to handle scenarios where the `Get-WindowsFeature` cmdlet may not be available. The change ensures that the function gracefully handles environments lacking this cmdlet by returning a default value. ### Key changes: * **Improved error handling for `Get-WindowsFeature`:** - Added a check using `Get-Command` to verify the existence of the `Get-WindowsFeature` cmdlet before attempting to use it. If the cmdlet does not exist, the function now returns the default array `@('Common')` without attempting further operations. # Change type - [x] Bug fix (non-breaking change) - [ ] Code style update (formatting, local variables) - [ ] New Feature (non-breaking change that adds new functionality without impacting existing) - [ ] Breaking change (fix or feature that may cause functionality impact) - [ ] Other # Checklist: - [x] My code follows the style and contribution guidelines of this project. - [x] I have tested and validated my code changes.
1 parent 8546802 commit 8d39b5b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/modules/SdnDiag.Utilities.psm1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,8 +2883,15 @@ function Get-EnvironmentRole {
28832883
$array = @('Common')
28842884

28852885
try {
2886-
$featuresInstalled = Get-WindowsFeature | Where-Object {$_.Installed -ieq $true}
2887-
if ($null -eq $featuresInstalled) {
2886+
# due to some weird functionality in the Get-WindowsFeature cmdlet, we need to check to see if the cmdlet exists
2887+
# and if it does, we will check to see if the features are installed
2888+
if (Get-Command -Name Get-WindowsFeature -ErrorAction Ignore) {
2889+
$featuresInstalled = Get-WindowsFeature -ErrorAction Ignore | Where-Object {$_.Installed -ieq $true}
2890+
if ($null -eq $featuresInstalled) {
2891+
return $array
2892+
}
2893+
}
2894+
else {
28882895
return $array
28892896
}
28902897

0 commit comments

Comments
 (0)