From c7054521a73b4073225c5f48c73238c14d5f6180 Mon Sep 17 00:00:00 2001 From: undergroundwires Date: Fri, 4 Aug 2023 17:48:15 +0100 Subject: [PATCH] win: improve output/user feedback capabilities - Improve error handling. - Provide more detailed user feedback with meaningful messages. - Check if capability exists before performing operations. - Inform system restart is needed. --- src/application/collections/windows.yaml | 40 ++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/application/collections/windows.yaml b/src/application/collections/windows.yaml index 296a39bb..a455e29f 100644 --- a/src/application/collections/windows.yaml +++ b/src/application/collections/windows.yaml @@ -7359,10 +7359,44 @@ functions: call: function: RunPowerShell parameters: - code: Get-WindowsCapability -Online -Name '{{ $capabilityName }}*' | Remove-WindowsCapability -Online + code: |- + $capabilityName = '{{ $capabilityName }}' + try { + # Using wildcard for version number handling + $capability = Get-WindowsCapability -Online -Name "$capabilityName*" + if (!$capability) { + Write-Host "Skipping. Capability `"$capabilityName`" is missing." + exit 0 + } + if ($capability.State -eq 'NotPresent') { + Write-Host "Skipping. Capability `"$capabilityName`" is missing." + exit 0 + } + Write-Host "Removing capability `"$capabilityName`"" + Remove-WindowsCapability -Online -Name "$($capability.Name)" -ErrorAction Stop + Write-Host "Successfully removed `"$CapabilityName`"" + } + catch { + Write-Error "Failed to remove `"$capabilityName`": $_" + } revertCode: |- - $capability = Get-WindowsCapability -Online -Name '{{ $capabilityName }}*' - Add-WindowsCapability -Name "$capability.Name" -Online + $capabilityName = '{{ $capabilityName }}' + try { + # Using wildcard for version number handling + $capability = Get-WindowsCapability -Online -Name "$capabilityName*" + if (!$capability) { + Write-Error "Failed to find `"$capabilityName`"." + exit 1 + } + $result = Add-WindowsCapability -Name $capability.Name -Online -ErrorAction Stop + Write-Host "Successfully added `"$capabilityName`"." + if ($result.RestartNeeded -eq 'Yes') { + Write-Warning "A restart is needed to finish installing `"$capabilityName`"." + } + } + catch { + Write-Error "Failed to add `"$capabilityName`": $_" + } - name: RenameSystemFile parameters: