From 6c466f49f7caf143f6c3c678ac91b5a09fbf7d28 Mon Sep 17 00:00:00 2001 From: Joeri Date: Sat, 7 Feb 2026 10:22:50 +0100 Subject: [PATCH] Updated script --- cleanup.ps1 | 180 +++++++++++++++++++++++++++++++++++++++++++++---- cleanup.ps1.bk | 49 ++++++++++++++ 2 files changed, 216 insertions(+), 13 deletions(-) create mode 100644 cleanup.ps1.bk diff --git a/cleanup.ps1 b/cleanup.ps1 index 63dab24..189a2cc 100644 --- a/cleanup.ps1 +++ b/cleanup.ps1 @@ -1,25 +1,179 @@ +# -------------------------------------------------------------------------------- +# Logging setup +# -------------------------------------------------------------------------------- +$LogDirectory = "C:\Quest" +$LogDate = Get-Date -Format "yyyy-MM-dd" +$RandomSuffix = Get-Random -Minimum 1000 -Maximum 9999 +$LogFile = Join-Path $LogDirectory "cleanup-${LogDate}-${RandomSuffix}.log" + +# Create log directory if it doesn't exist +if (-not (Test-Path $LogDirectory)) { + New-Item -Path $LogDirectory -ItemType Directory -Force | Out-Null +} + +# Function for timestamped logging +function Write-Log { + param( + [Parameter(Mandatory=$true)] + [string]$Message, + [string]$Level = "INFO" + ) + + $Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + $LogEntry = "[$Timestamp] [$Level] $Message" + + # Write to log file + $LogEntry | Out-File -FilePath $LogFile -Append -Encoding UTF8 + + # Write to console + Write-Host $LogEntry +} + +# Initialize log file +"================================================================================" | Out-File -FilePath $LogFile -Append -Encoding UTF8 +"Script started at $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" | Out-File -FilePath $LogFile -Append -Encoding UTF8 +"Log file: $LogFile" | Out-File -FilePath $LogFile -Append -Encoding UTF8 +"================================================================================" | Out-File -FilePath $LogFile -Append -Encoding UTF8 +"" | Out-File -FilePath $LogFile -Append -Encoding UTF8 + +# -------------------------------------------------------------------------------- +# Quest Help download and installation +# -------------------------------------------------------------------------------- +Write-Log "Checking if Quest Help exists..." + +if (Test-Path "C:\Program Files\Quest Help\Quest Help.exe") { + Write-Log "Quest Help found! Skipping installation..." -Level "INFO" +} else { + Write-Log "Quest Help not found! Downloading installer..." -Level "WARNING" + Invoke-WebRequest -Uri "https://hulpvanopafstand.be/assets/downloads/quest_help.exe" -OutFile "C:\Quest\quest_help.exe" + Write-Log "Download completed." -Level "INFO" + Write-Log "Starting installer..." -Level "INFO" + Start-Process "C:\Quest\quest_help.exe" + Write-Log "Installer started." -Level "INFO" +} + +Write-Log "Quest Help check completed." -Level "INFO" +"" | Out-File -FilePath $LogFile -Append -Encoding UTF8 + +# -------------------------------------------------------------------------------- # Add defender exclusions -Add-MpPreference -ExclusionPath "C:\Quest\*" -Add-MpPreference -ExclusionPath "C:\Program Files\Mesh Agent\*" -Add-MpPreference -ExclusionPath "C:\Program Files\TacticalAgent\*" -Add-MpPreference -ExclusionPath "C:\ProgramData\TacticalRMM\*" -Add-MpPreference -ExclusionPath "C:\Windows\Temp\is-*.tmp\tacticalagent*" -Add-MpPreference -ExclusionProcess "C:\Program Files\TacticalAgent\tacticalrmm.exe" -Add-MpPreference -ExclusionProcess "C:\ProgramData\TacticalRMM\tacticalagent*" -Add-MpPreference -ExclusionProcess "C:\Windows\Temp\is-*.tmp\tacticalagent*" +# -------------------------------------------------------------------------------- +Write-Log "Adding Windows Defender exclusions..." + +$ExclusionPaths = @( + "C:\Quest\*", + "C:\Program Files\Mesh Agent\*", + "C:\Program Files\TacticalAgent\*", + "C:\ProgramData\TacticalRMM\*", + "C:\Windows\Temp\is-*.tmp\tacticalagent*" +) + +$ExclusionProcesses = @( + "C:\Program Files\TacticalAgent\tacticalrmm.exe", + "C:\ProgramData\TacticalRMM\tacticalagent*", + "C:\Windows\Temp\is-*.tmp\tacticalagent*" +) + +foreach ($Path in $ExclusionPaths) { + try { + Add-MpPreference -ExclusionPath $Path -ErrorAction Stop | Out-Null + Write-Log "Added exclusion path: $Path" -Level "INFO" + } catch { + Write-Log "Failed to add exclusion path: $Path - $($_.Exception.Message)" -Level "ERROR" + } +} + +foreach ($Process in $ExclusionProcesses) { + try { + Add-MpPreference -ExclusionProcess $Process -ErrorAction Stop | Out-Null + Write-Log "Added exclusion process: $Process" -Level "INFO" + } catch { + Write-Log "Failed to add exclusion process: $Process - $($_.Exception.Message)" -Level "ERROR" + } +} + +Write-Log "Defender exclusions completed." -Level "INFO" +"" | Out-File -FilePath $LogFile -Append -Encoding UTF8 # -------------------------------------------------------------------------------- # Enable daily restore points +# -------------------------------------------------------------------------------- +Write-Log "Setting up daily restore points..." -Enable-ComputerRestore -Drive 'C:' +try { + Enable-ComputerRestore -Drive 'C:' -ErrorAction Stop | Out-Null + Write-Log "System Restore enabled for C: drive." -Level "INFO" +} catch { + Write-Log "Failed to enable System Restore: $($_.Exception.Message)" -Level "ERROR" +} -Register-ScheduledTask -TaskName "Daily System Restore" -Action (New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-ExecutionPolicy Bypass -Command `"Checkpoint-Computer -Description \"\"AUTOMATIC-$(Get-Date -Format 'yyyyMMddHHmmss')\"\" -RestorePointType \"\"MODIFY_SETTINGS\"\"`"") -Trigger (New-ScheduledTaskTrigger -Daily -At 9am) -Settings (New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd -RunOnlyIfNetworkAvailable) -Principal (New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest) +Register-ScheduledTask -TaskName "Daily System Restore" -Action (New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-ExecutionPolicy Bypass -Command `"Checkpoint-Computer -Description \"\"AUTOMATIC-$(Get-Date -Format 'yyyyMMddHHmmss')\"\" -RestorePointType \"\"MODIFY_SETTINGS\"\"`"") -Trigger (New-ScheduledTaskTrigger -Daily -At 9am) -Settings (New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd -RunOnlyIfNetworkAvailable) -Principal (New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest) -ErrorAction Stop | Out-Null +Write-Log "Scheduled task 'Daily System Restore' created." -Level "INFO" -Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore" -Name "SystemRestorePointFrequency" -Value 0 -Type DWORD -Force +try { + Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore" -Name "SystemRestorePointFrequency" -Value 0 -Type DWORD -Force -ErrorAction Stop + Write-Log "Restore point frequency set to 0 (allows frequent restore points)." -Level "INFO" +} catch { + Write-Log "Failed to set restore point frequency: $($_.Exception.Message)" -Level "ERROR" +} + +Write-Log "Restore points setup completed." -Level "INFO" +"" | Out-File -FilePath $LogFile -Append -Encoding UTF8 # -------------------------------------------------------------------------------- # Updating all apps +# -------------------------------------------------------------------------------- +Write-Log "Upgrading all applications with winget..." -winget upgrade --all +try { + winget upgrade --all 2>&1 | Out-File -FilePath $LogFile -Append -Encoding UTF8 + Write-Log "Winget upgrade completed." -Level "INFO" +} catch { + Write-Log "Winget upgrade failed: $($_.Exception.Message)" -Level "ERROR" +} -# -------------------------------------------------------------------------------- \ No newline at end of file +Write-Log "Application updates completed." -Level "INFO" +"" | Out-File -FilePath $LogFile -Append -Encoding UTF8 + +# -------------------------------------------------------------------------------- +# Updating Windows +# -------------------------------------------------------------------------------- +Write-Log "Updating Windows..." + +try { + Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -ErrorAction Stop | Out-Null + Write-Log "NuGet package provider installed." -Level "INFO" +} catch { + Write-Log "Failed to install NuGet: $($_.Exception.Message)" -Level "ERROR" +} + +try { + Install-Module PSWindowsUpdate -Force -AllowClobber -ErrorAction Stop | Out-Null + Write-Log "PSWindowsUpdate module installed." -Level "INFO" +} catch { + Write-Log "Failed to install PSWindowsUpdate: $($_.Exception.Message)" -Level "ERROR" +} + +try { + Import-Module PSWindowsUpdate -ErrorAction Stop + Write-Log "PSWindowsUpdate module imported." -Level "INFO" +} catch { + Write-Log "Failed to import PSWindowsUpdate: $($_.Exception.Message)" -Level "ERROR" +} + +try { + Get-WindowsUpdate -AcceptAll -Install -ErrorAction Stop 2>&1 | Out-File -FilePath $LogFile -Append -Encoding UTF8 + Write-Log "Windows updates installed." -Level "INFO" +} catch { + Write-Log "Failed to install Windows updates: $($_.Exception.Message)" -Level "ERROR" +} + +Write-Log "Windows update completed." -LEVEL "INFO" +"" | Out-File -FilePath $LogFile -Append -Encoding UTF8 + +# -------------------------------------------------------------------------------- +# Script completion +# -------------------------------------------------------------------------------- +Write-Log "Script completed at $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" -Level "INFO" +Write-Log "Log file saved to: $LogFile" -Level "INFO" +"================================================================================" | Out-File -FilePath $LogFile -Append -Encoding UTF8 \ No newline at end of file diff --git a/cleanup.ps1.bk b/cleanup.ps1.bk new file mode 100644 index 0000000..bdb3d58 --- /dev/null +++ b/cleanup.ps1.bk @@ -0,0 +1,49 @@ +if (Test-Path -Path "C:\Quest" -PathType Container) { + Write-Host "Directory exists! Skipping..." +} else { + Write-Host "Creating C:\Quest..." + New-Item -Path C:\Quest -ItemType Directory + Write-Host "Done." +} + +if (Test-Path "C:\Program Files\Quest Help\Quest Help.exe") { + Write-Host "Quest Help found! Skipping installation..." +} else { + Write-Host "Quest Help not found! Downloading installer..." + Invoke-WebRequest -Uri https://hulpvanopafstand.be/assets/downloads/quest_help.exe -OutFile "C:\Quest\quest_help.exe" + Start-Process "C:\Quest\quest_help.exe" + Write-Host "Done." +} + +# -------------------------------------------------------------------------------- +# Add defender exclusions +Add-MpPreference -ExclusionPath "C:\Quest\*" +Add-MpPreference -ExclusionPath "C:\Program Files\Mesh Agent\*" +Add-MpPreference -ExclusionPath "C:\Program Files\TacticalAgent\*" +Add-MpPreference -ExclusionPath "C:\ProgramData\TacticalRMM\*" +Add-MpPreference -ExclusionPath "C:\Windows\Temp\is-*.tmp\tacticalagent*" +Add-MpPreference -ExclusionProcess "C:\Program Files\TacticalAgent\tacticalrmm.exe" +Add-MpPreference -ExclusionProcess "C:\ProgramData\TacticalRMM\tacticalagent*" +Add-MpPreference -ExclusionProcess "C:\Windows\Temp\is-*.tmp\tacticalagent*" + +# -------------------------------------------------------------------------------- +# Enable daily restore points + +Enable-ComputerRestore -Drive 'C:' + +Register-ScheduledTask -TaskName "Daily System Restore" -Action (New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-ExecutionPolicy Bypass -Command `"Checkpoint-Computer -Description \"\"AUTOMATIC-$(Get-Date -Format 'yyyyMMddHHmmss')\"\" -RestorePointType \"\"MODIFY_SETTINGS\"\"`"") -Trigger (New-ScheduledTaskTrigger -Daily -At 9am) -Settings (New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd -RunOnlyIfNetworkAvailable) -Principal (New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest) + +Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore" -Name "SystemRestorePointFrequency" -Value 0 -Type DWORD -Force + +# -------------------------------------------------------------------------------- +# Updating all apps + +winget upgrade --all + +# -------------------------------------------------------------------------------- +# Updating Windows +Write-Output "Updating Windows" +Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force +Install-Module PSWindowsUpdate -Force -AllowClobber +Import-Module PSWindowsUpdate +Get-WindowsUpdate -AcceptAll -Install \ No newline at end of file