Skip to content

Commit c5bd5cb

Browse files
(DSC) Handle when a scope has no installed resources
Prior to this change, the `PSResourceList` resource would fail during `export` operations when a scope had no installed resources because it would emit an empty/invalid item. The non-terminating error for no resources in a scope was also erroneously emitted. This change: 1. Defines the `GetAllPsResourcesInScope` function as a wrapper around `Get-PsResource` to retrieve all resources in a given scope, returning an empty array if no resources are found and erroring if the command fails for any other reason. 1. Updates `PopulatePSResourceListObject` to handle empty resource arrays gracefully, emitting an info message instead of invalid data.
1 parent 928b50d commit c5bd5cb

1 file changed

Lines changed: 46 additions & 19 deletions

File tree

‎src/dsc/psresourceget.ps1‎

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -624,9 +624,12 @@ function ExportOperation {
624624
exit [ExitCode]::ExportNotImplemented
625625
}
626626
'psresourcelist' {
627-
$currentUserPSResources = Get-PSResource
628-
$allUsersPSResources = Get-PSResource -Scope AllUsers
629-
PopulatePSResourceListObject -allUsersPSResources $allUsersPSResources -currentUserPSResources $currentUserPSResources
627+
$populatingParams = @{
628+
currentUserPSResources = GetAllPsResourcesInScope -Scope CurrentUser
629+
allUsersPSResources = GetAllPsResourcesInScope -Scope AllUsers
630+
}
631+
632+
PopulatePSResourceListObject @populatingParams
630633
}
631634
default {
632635
Write-Trace -level error -message "Unknown ResourceType: $ResourceType"
@@ -844,6 +847,22 @@ function DeleteOperation {
844847
}
845848
}
846849

850+
function GetAllPsResourcesInScope {
851+
param(
852+
[Scope]$Scope
853+
)
854+
855+
try {
856+
Get-PSResource -Scope $Scope -ErrorAction Stop
857+
} catch [Microsoft.PowerShell.PSResourceGet.UtilClasses.ResourceNotFoundException] {
858+
# Everything is fine, there's just no installed resources
859+
@()
860+
} catch {
861+
Write-Trace -level error -message "Failed to get PSResources for '$Scope' scope: $_"
862+
@()
863+
}
864+
}
865+
847866
function PopulatePSResourceListObjectByRepository {
848867
param (
849868
$resourcesExist,
@@ -891,24 +910,32 @@ function PopulatePSResourceListObject {
891910

892911
$allPSResources = @()
893912

894-
$allPSResources += $allUsersPSResources | ForEach-Object {
895-
return [PSResource]::new(
896-
$_.Name,
897-
$_.Version,
898-
[Scope]"AllUsers",
899-
$_.Repository,
900-
$_.PreRelease ? $true : $false
901-
)
913+
if ($allUsersPSResources.count -gt 1) {
914+
$allPSResources += $allUsersPSResources | ForEach-Object {
915+
return [PSResource]::new(
916+
$_.Name,
917+
$_.Version,
918+
[Scope]"AllUsers",
919+
$_.Repository,
920+
$_.PreRelease ? $true : $false
921+
)
922+
}
923+
} else {
924+
Write-Trace -level info "No PSResources found for AllUsers scope."
902925
}
903926

904-
$allPSResources += $currentUserPSResources | ForEach-Object {
905-
return [PSResource]::new(
906-
$_.Name,
907-
$_.Version,
908-
[Scope]"CurrentUser",
909-
$_.Repository,
910-
$_.PreRelease ? $true : $false
911-
)
927+
if ($currentUserPSResources.count -gt 1) {
928+
$allPSResources += $currentUserPSResources | ForEach-Object {
929+
return [PSResource]::new(
930+
$_.Name,
931+
$_.Version,
932+
[Scope]"CurrentUser",
933+
$_.Repository,
934+
$_.PreRelease ? $true : $false
935+
)
936+
}
937+
} else {
938+
Write-Trace -level info "No PSResources found for CurrentUser scope."
912939
}
913940

914941
$repoGrps = $allPSResources | Group-Object -Property repositoryName

0 commit comments

Comments
 (0)