From 3965bbc37d5119bbb443136820d1efdc3ac42876 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 16 Nov 2025 00:25:11 +0000 Subject: [PATCH 1/3] Initial plan From b6bac37a1e6c7dcebe056d15469c597a212fc9d8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 16 Nov 2025 00:31:07 +0000 Subject: [PATCH 2/3] Fix PSScriptAnalyzer warnings and remove unused DestructiveMode parameter Co-authored-by: AprilDeFeu <36605389+AprilDeFeu@users.noreply.github.com> --- .../maintenance/system-maintenance.ps1 | 43 ++++++++----------- .../PowerShell/system-maintenance.Tests.ps1 | 6 --- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/PowerShell/system-administration/maintenance/system-maintenance.ps1 b/PowerShell/system-administration/maintenance/system-maintenance.ps1 index 4267002..474909c 100644 --- a/PowerShell/system-administration/maintenance/system-maintenance.ps1 +++ b/PowerShell/system-administration/maintenance/system-maintenance.ps1 @@ -22,17 +22,10 @@ Maximum age (in days) files must be older than to be removed from temp locations. Default: 7. Set to 0 to remove everything (use with caution). -.PARAMETER DestructiveMode - When specified, enables destructive operations (disk cleanup, network reset, - CHKDSK repair) without interactive prompts. Use with caution in automated - scenarios. Without this flag, destructive operations require confirmation. -.EXAMPLE - .\system-maintenance.ps1 -RunWindowsUpdate -MaxTempFileAgeDays 14 .EXAMPLE - .\system-maintenance.ps1 -DestructiveMode -WhatIf - Preview destructive operations in non-interactive mode. + .\system-maintenance.ps1 -RunWindowsUpdate -MaxTempFileAgeDays 14 .EXAMPLE .\system-maintenance.ps1 -WhatIf @@ -47,8 +40,7 @@ [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] param( [switch] $RunWindowsUpdate, - [ValidateRange(0, 3650)][int] $MaxTempFileAgeDays = 7, - [switch] $DestructiveMode + [ValidateRange(0, 3650)][int] $MaxTempFileAgeDays = 7 ) Set-StrictMode -Version Latest @@ -71,9 +63,8 @@ $script:LogFile = Get-LogFilePath # Store the script-level PSCmdlet for use in nested scriptblocks $script:ScriptPSCmdlet = $PSCmdlet -# Helper to perform a confirmation check that works even when invoked inside -# nested scriptblocks. Uses the script-scoped PSCmdlet reference. -function Write-Log { +# Custom logging function (named Write-MaintenanceLog to avoid conflict with built-in Write-MaintenanceLog in PS Core 6.1+) +function Write-MaintenanceLog { [CmdletBinding()] param( [Parameter(Mandatory = $true)][string] $Message, @@ -90,22 +81,23 @@ function Write-Log { } function Invoke-Step { + [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory = $true)][scriptblock] $ScriptBlock, [Parameter(Mandatory = $true)][string] $Title, [string] $ConfirmTarget, [switch] $Destructive ) - Write-Log "BEGIN: $Title" + Write-MaintenanceLog "BEGIN: $Title" try { if ($Destructive.IsPresent -and $ConfirmTarget) { # Use script-scoped PSCmdlet with null check if ($null -eq $script:ScriptPSCmdlet) { - Write-Log -Message "SKIP: $Title (PSCmdlet not available)" -Level 'WARN' + Write-MaintenanceLog -Message "SKIP: $Title (PSCmdlet not available)" -Level 'WARN' return } if (-not ($script:ScriptPSCmdlet.ShouldProcess($ConfirmTarget, $Title))) { - Write-Log -Message "SKIP: $Title (not confirmed)" -Level 'WARN' + Write-MaintenanceLog -Message "SKIP: $Title (not confirmed)" -Level 'WARN' return } } @@ -124,27 +116,28 @@ function Invoke-Step { # Log standard output if ($output.Count -gt 0) { $outputString = ($output | Out-String).Trim() - if ($outputString -ne '') { Write-Log $outputString } + if ($outputString -ne '') { Write-MaintenanceLog $outputString } } # Log errors separately if ($errors.Count -gt 0) { foreach ($err in $errors) { - Write-Log -Message "ERROR: $($err.Exception.Message)" -Level 'ERROR' + Write-MaintenanceLog -Message "ERROR: $($err.Exception.Message)" -Level 'ERROR' } } - Write-Log "END: $Title" + Write-MaintenanceLog "END: $Title" } catch { - Write-Log -Message "ERROR in ${Title}: $($_.Exception.Message)" -Level 'ERROR' + Write-MaintenanceLog -Message "ERROR in ${Title}: $($_.Exception.Message)" -Level 'ERROR' } } -Write-Log "Starting system maintenance and health checks. Params: RunWindowsUpdate=$RunWindowsUpdate, MaxTempFileAgeDays=$MaxTempFileAgeDays" +Write-MaintenanceLog "Starting system maintenance and health checks. Params: RunWindowsUpdate=$RunWindowsUpdate, MaxTempFileAgeDays=$MaxTempFileAgeDays" -# --- Destructive Mode Selection --- -# Note: Now controlled via -DestructiveMode parameter (no longer prompts interactively) +# --- Destructive Operations --- +# Destructive operations (disk cleanup, network reset, CHKDSK) use ShouldProcess +# for confirmation and can be controlled with -WhatIf and -Confirm parameters. # ---------------------- Windows Update (optional) ---------------------- if ($RunWindowsUpdate) { @@ -454,5 +447,5 @@ Invoke-Step -Title 'Event Log: Critical/System errors (24h)' -ScriptBlock { try { $since = (Get-Date).AddDays(-1); Get-WinEvent -FilterHashtable @{LogName = 'System'; Level = 1; StartTime = $since } -ErrorAction SilentlyContinue | Select-Object TimeCreated, Id, ProviderName, LevelDisplayName, Message | Format-Table -AutoSize | Out-String } catch { "EventLog scan error: $($_.Exception.Message)" } } -Write-Log "Maintenance completed. Review the log for details: $script:LogFile" -Write-Log 'If CHKDSK or network resets were scheduled, please reboot to complete repairs.' +Write-MaintenanceLog "Maintenance completed. Review the log for details: $script:LogFile" +Write-MaintenanceLog 'If CHKDSK or network resets were scheduled, please reboot to complete repairs.' diff --git a/tests/unit/PowerShell/system-maintenance.Tests.ps1 b/tests/unit/PowerShell/system-maintenance.Tests.ps1 index 7e36085..c0923cc 100644 --- a/tests/unit/PowerShell/system-maintenance.Tests.ps1 +++ b/tests/unit/PowerShell/system-maintenance.Tests.ps1 @@ -111,12 +111,6 @@ Describe "system-maintenance.ps1" { # WhatIf prevents actual Windows Update operations { & $localPath -RunWindowsUpdate -WhatIf } | Should -Not -Throw } - - It "should handle DestructiveMode switch with WhatIf" { - $localPath = $scriptPath - # WhatIf prevents actual destructive operations - { & $localPath -DestructiveMode -WhatIf } | Should -Not -Throw - } } Context "Permissions and Prerequisites" { From d35cf4f03d452d567266b83843fc174c26a55e78 Mon Sep 17 00:00:00 2001 From: April <36605389+AprilDeFeu@users.noreply.github.com> Date: Sat, 15 Nov 2025 19:39:11 -0500 Subject: [PATCH 3/3] Update PowerShell/system-administration/maintenance/system-maintenance.ps1 Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../system-administration/maintenance/system-maintenance.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PowerShell/system-administration/maintenance/system-maintenance.ps1 b/PowerShell/system-administration/maintenance/system-maintenance.ps1 index 474909c..712a9a6 100644 --- a/PowerShell/system-administration/maintenance/system-maintenance.ps1 +++ b/PowerShell/system-administration/maintenance/system-maintenance.ps1 @@ -63,7 +63,7 @@ $script:LogFile = Get-LogFilePath # Store the script-level PSCmdlet for use in nested scriptblocks $script:ScriptPSCmdlet = $PSCmdlet -# Custom logging function (named Write-MaintenanceLog to avoid conflict with built-in Write-MaintenanceLog in PS Core 6.1+) +# Custom logging function (named Write-MaintenanceLog to avoid conflict with built-in Write-Log in PS Core 6.1+) function Write-MaintenanceLog { [CmdletBinding()] param(