From 6fc948332a3c605e489eaa27918d9904cd3986fa Mon Sep 17 00:00:00 2001 From: he3als <65787561+he3als@users.noreply.github.com> Date: Sun, 11 Aug 2024 19:08:40 +0100 Subject: [PATCH] fix: cleanup previous commits --- src/playbook/Configuration/custom.yml | 9 ++-- src/playbook/Configuration/tweaks.yml | 2 +- .../tweaks/scripts/script-cleanup.yml | 9 ---- .../Modules/MicrosoftEdge/MicrosoftEdge.psm1 | 0 .../Scripts/Modules/Shortcuts/Shortcuts.psm1 | 6 ++- .../Scripts/Modules/UserPaths/UserPaths.psm1 | 42 ++++++------------- .../AtlasModules/Scripts/initPowerShell.ps1 | 9 ---- src/playbook/Executables/CLEANUP.ps1 | 36 +++++++++------- src/playbook/Executables/LIBREWOLF.ps1 | 32 +++++++------- src/playbook/Executables/SOFTWARE.ps1 | 14 ++++--- src/playbook/Executables/TASKBARPINS.ps1 | 18 ++------ 11 files changed, 72 insertions(+), 105 deletions(-) delete mode 100644 src/playbook/Configuration/tweaks/scripts/script-cleanup.yml delete mode 100644 src/playbook/Executables/AtlasModules/Scripts/Modules/MicrosoftEdge/MicrosoftEdge.psm1 delete mode 100644 src/playbook/Executables/AtlasModules/Scripts/initPowerShell.ps1 diff --git a/src/playbook/Configuration/custom.yml b/src/playbook/Configuration/custom.yml index ad9ea5c397..1cabb63b56 100644 --- a/src/playbook/Configuration/custom.yml +++ b/src/playbook/Configuration/custom.yml @@ -61,10 +61,13 @@ actions: - !task: {path: 'tweaks\scripts\script-ngen.yml'} - !task: {path: 'tweaks\qol\config-powershell.yml'} - # Cleanup script takes longer to finish so it has its own status - # It's ran here as you can't check when cleanmgr is finished + # Disk Cleanup is run first so it can run in the background - !writeStatus: {status: 'Cleaning up'} - - !task: {path: 'tweaks\scripts\script-cleanup.yml'} + - !powerShell: + command: '.\CLEANUP.ps1' + exeDir: true + wait: true + runas: currentUserElevated # Set hidden Settings pages # Done before everything else as scripts will overwrite it diff --git a/src/playbook/Configuration/tweaks.yml b/src/playbook/Configuration/tweaks.yml index 1fff681e21..bbf1f9a176 100644 --- a/src/playbook/Configuration/tweaks.yml +++ b/src/playbook/Configuration/tweaks.yml @@ -145,7 +145,7 @@ actions: # with modifying the graphical user interface (GUI). # ----------------------------------------------------- - !writeStatus: {status: 'Running QoL tweaks'} - - !task: {path: 'tweaks\performance\system\bcdedit-tweaks.yml'} + - !task: {path: 'tweaks\qol\bcdedit-tweaks.yml'} - !task: {path: 'tweaks\qol\best-wallpaper-quality.yml'} - !task: {path: 'tweaks\qol\config-start-menu.yml'} - !task: {path: 'tweaks\qol\config-windows-ink-workspace.yml'} diff --git a/src/playbook/Configuration/tweaks/scripts/script-cleanup.yml b/src/playbook/Configuration/tweaks/scripts/script-cleanup.yml deleted file mode 100644 index 5026883c35..0000000000 --- a/src/playbook/Configuration/tweaks/scripts/script-cleanup.yml +++ /dev/null @@ -1,9 +0,0 @@ ---- -title: Cleanup Temporary Files -description: Cleans up temporary files using Disk Cleanup (if no other installs of Windows are found) -actions: - - !powerShell: - command: '.\CLEANUP.ps1' - exeDir: true - wait: true - runas: currentUserElevated diff --git a/src/playbook/Executables/AtlasModules/Scripts/Modules/MicrosoftEdge/MicrosoftEdge.psm1 b/src/playbook/Executables/AtlasModules/Scripts/Modules/MicrosoftEdge/MicrosoftEdge.psm1 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/playbook/Executables/AtlasModules/Scripts/Modules/Shortcuts/Shortcuts.psm1 b/src/playbook/Executables/AtlasModules/Scripts/Modules/Shortcuts/Shortcuts.psm1 index 9c02e4ce05..6497ad8bd4 100644 --- a/src/playbook/Executables/AtlasModules/Scripts/Modules/Shortcuts/Shortcuts.psm1 +++ b/src/playbook/Executables/AtlasModules/Scripts/Modules/Shortcuts/Shortcuts.psm1 @@ -9,7 +9,7 @@ function New-Shortcut { [switch]$IfExist ) - if (!(Test-Path $Source -PathType Leaf)) { + if (!(Test-Path $Source)) { throw "Source '$source' not found." } @@ -17,6 +17,10 @@ function New-Shortcut { return } + if (!$WorkingDir) { + $WorkingDir = Split-Path $Source + } + $WshShell = New-Object -ComObject WScript.Shell $Shortcut = $WshShell.CreateShortcut($Destination) $Shortcut.TargetPath = $Source diff --git a/src/playbook/Executables/AtlasModules/Scripts/Modules/UserPaths/UserPaths.psm1 b/src/playbook/Executables/AtlasModules/Scripts/Modules/UserPaths/UserPaths.psm1 index 00a9481151..0689f622b0 100644 --- a/src/playbook/Executables/AtlasModules/Scripts/Modules/UserPaths/UserPaths.psm1 +++ b/src/playbook/Executables/AtlasModules/Scripts/Modules/UserPaths/UserPaths.psm1 @@ -43,42 +43,24 @@ public class KnownFolder } } -function Get-Accounts { - $accounts = Get-CimInstance Win32_UserAccount -Filter "Disabled=False" - $userProfiles = Get-CimInstance Win32_UserProfile - - $profileLookup = @{} - foreach ($profile in $userProfiles) { - $profileLookup[$profile.SID] = $profile.LocalPath - } - - $data = $accounts | ForEach-Object { - $account = $_ - $profilePath = $profileLookup[$account.SID] - - [PSCustomObject]@{ - Name = $account.Name - Caption = $account.Caption - Path = $profilePath - SID = $account.SID - } - } - - return $data -} - function Get-SystemDrive { - @( + $drive = $null + foreach ($letter in @( $env:SystemDrive, (Get-CimInstance -ClassName Win32_OperatingSystem).SystemDrive, "C:" - ) | ForEach-Object { - if (($_.Length -eq 2) -and (Test-Path $_ -ItemType Container)) { - return $_ + )) { + if ($letter -and ($letter.Length -eq 2) -and (Test-Path $letter -PathType Container)) { + $drive = $letter + break } } - throw "Failed to find the system drive!" + if ($drive) { + return $drive + } else { + throw "Failed to find the system drive!" + } } -Export-ModuleMember -Function Get-UserPath, Get-Accounts \ No newline at end of file +Export-ModuleMember -Function Get-UserPath, Get-SystemDrive \ No newline at end of file diff --git a/src/playbook/Executables/AtlasModules/Scripts/initPowerShell.ps1 b/src/playbook/Executables/AtlasModules/Scripts/initPowerShell.ps1 deleted file mode 100644 index aa60352f27..0000000000 --- a/src/playbook/Executables/AtlasModules/Scripts/initPowerShell.ps1 +++ /dev/null @@ -1,9 +0,0 @@ -$seperator = [IO.Path]::PathSeparator - -# Add modules path -$env:PSModulePath += "$seperator" + "$PWD\AtlasModules\Scripts\Modules" - -# Update PATH variable -$env:Path = [System.Environment]::GetEnvironmentVariable("Path",[System.EnvironmentVariableTarget]::Machine) + ` - $seperator + ` - [System.Environment]::GetEnvironmentVariable("Path",[System.EnvironmentVariableTarget]::User) \ No newline at end of file diff --git a/src/playbook/Executables/CLEANUP.ps1 b/src/playbook/Executables/CLEANUP.ps1 index 3eeca5613f..871dc27a8a 100644 --- a/src/playbook/Executables/CLEANUP.ps1 +++ b/src/playbook/Executables/CLEANUP.ps1 @@ -1,3 +1,4 @@ +$env:PSModulePath += ";$PWD\AtlasModules\Scripts\Modules" function Invoke-AtlasDiskCleanup { # Kill running cleanmgr instances, as they will prevent new cleanmgr from starting Get-Process -Name cleanmgr -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue @@ -48,40 +49,43 @@ function Invoke-AtlasDiskCleanup { # If so, don't cleanup as it will also cleanup other drives, which will be slow, and we don't want to touch other data $drives = (Get-PSDrive -PSProvider FileSystem).Root | Where-Object { $_ -notmatch $(Get-SystemDrive) } foreach ($drive in $drives) { - if (!(Test-Path -Path $(Join-Path -Path $drive.Root -ChildPath 'Windows') -PathType Container)) { - Invoke-AtlasDiskCleanup - } + if (!(Test-Path -Path $(Join-Path -Path $drive -ChildPath 'Windows') -PathType Container)) { + Write-Output "No other Windows drives found, running Disk Cleanup." + Invoke-AtlasDiskCleanup + } } # Clear the user temp folder -$env:temp, $env:tmp, "$env:localappdata\Temp" | ForEach-Object { - if (Test-Path $_ -PathType Container) { - $userTemp = $_ +foreach ($path in @($env:temp, $env:tmp, "$env:localappdata\Temp")) { + if (Test-Path $path -PathType Container) { + $userTemp = $path break } } -if (!$userTemp) { - Write-Error "User temp folder not found!" -} else { +if ($path) { + Write-Output "Cleaning user TEMP folder..." Get-ChildItem -Path $userTemp | Where-Object { $_.Name -ne 'AME' } | Remove-Item -Force -Recurse -EA 0 +} else { + Write-Error "User temp folder not found!" } # Clear the system temp folder $machine = [System.EnvironmentVariableTarget]::Machine -@( +foreach ($path in @( [System.Environment]::GetEnvironmentVariable("Temp", $machine), [System.Environment]::GetEnvironmentVariable("Tmp", $machine), "$([Environment]::GetFolderPath('Windows'))\Temp" -) | ForEach-Object { - if (Test-Path $_ -PathType Container) { - $sysTemp = $_ +)) { + if (Test-Path $path -PathType Container) { + $sysTemp = $path break } } -if (!$sysTemp) { - Write-Error "System temp folder not found!" -} else { +if ($sysTemp) { + Write-Output "Cleaning system TEMP folder..." Remove-Item -Path "$sysTemp\*" -Force -Recurse -EA 0 +} else { + Write-Error "System temp folder not found!" } # Delete all system restore points diff --git a/src/playbook/Executables/LIBREWOLF.ps1 b/src/playbook/Executables/LIBREWOLF.ps1 index 76e0755fd5..71031dfcd4 100644 --- a/src/playbook/Executables/LIBREWOLF.ps1 +++ b/src/playbook/Executables/LIBREWOLF.ps1 @@ -1,9 +1,9 @@ +$env:PSModulePath += ";$PWD\AtlasModules\Scripts\Modules" $ProgressPreference = "SilentlyContinue" $ErrorActionPreference = "Stop" # Initial variables $drive = Get-SystemDrive -$env:PSModulePath += ";$PWD\AtlasModules\Scripts\Modules" $desktop = [Environment]::GetFolderPath("Desktop") $startMenu = [Environment]::GetFolderPath("CommonPrograms") $programs = [Environment]::GetFolderPath("ProgramFiles") @@ -19,17 +19,8 @@ if ([string]::IsNullOrEmpty($librewolfVersion)) { $librewolfFileName = "librewolf-$librewolfVersion-windows-x86_64-setup.exe" $librewolfDownload = "https://gitlab.com/api/v4/projects/$gitLabId/packages/generic/librewolf/$librewolfVersion/$librewolfFileName" -Write-Output "Getting the latest LibreWolf-WinUpdater download link" -$librewolfUpdaterURI = "https://codeberg.org/api/v1/repos/ltguillaume/librewolf-winupdater/releases?draft=false&pre-release=false&page=1&limit=1" -$librewolfUpdaterDownload = (Invoke-RestMethod -Uri "$librewolfUpdaterURI").Assets | - Where-Object { $_.name -like "*.zip" } | - Select-Object -ExpandProperty browser_download_url - -# Output paths -$outputLibrewolf = "$drive\$librewolfFileName" -$outputLibrewolfUpdater = "$drive\librewolf-winupdater.zip" - Write-Output "Downloading the latest LibreWolf setup" +$outputLibrewolf = "$drive\$librewolfFileName" curl.exe -LSs "$librewolfDownload" -o "$outputLibrewolf" Write-Output "Installing LibreWolf silently" @@ -39,15 +30,23 @@ if (!(Test-Path $librewolfPath)) { } Write-Output "Creating LibreWolf Desktop shortcut" -New-Shortcut -Source "$librewolfPath\librewolf.exe" -ShortcutPath "$desktop\LibreWolf.lnk" -WorkingDir $librewolfPath +New-Shortcut -Source "$librewolfPath\librewolf.exe" -Destination "$desktop\LibreWolf.lnk" -WorkingDir $librewolfPath + + +Write-Title "Installing LibreWolf-WinUpdater..." +Write-Output "Getting the latest LibreWolf-WinUpdater download link" +$librewolfUpdaterURI = "https://codeberg.org/api/v1/repos/ltguillaume/librewolf-winupdater/releases?draft=false&pre-release=false&page=1&limit=1" +$librewolfUpdaterDownload = (Invoke-RestMethod -Uri "$librewolfUpdaterURI").Assets | + Where-Object { $_.name -like "*.zip" } | + Select-Object -ExpandProperty browser_download_url Write-Output "Downloading the latest LibreWolf WinUpdater ZIP" +$outputLibrewolfUpdater = "$drive\librewolf-winupdater.zip" curl.exe -LSs "$librewolfUpdaterDownload" -o "$outputLibrewolfUpdater" -Write-Output "Installing/extracting Librewolf-WinUpdater" +Write-Output "Extracting Librewolf-WinUpdater" Expand-Archive -Path $outputLibrewolfUpdater -DestinationPath "$programs\LibreWolf\librewolf-winupdater" -Force -# Automatic updater Write-Output "Adding automatic updater task" foreach ($User in (Get-CimInstance -ClassName Win32_UserAccount -Filter "Disabled=False").Name) { $Action = New-ScheduledTaskAction -Execute "$updaterPath\LibreWolf-WinUpdater.exe" -Argument "/Scheduled" @@ -59,8 +58,9 @@ foreach ($User in (Get-CimInstance -ClassName Win32_UserAccount -Filter "Disable } Write-Output "Adding LibreWolf WinUpdater shortcut" -New-Shortcut -Source "$updaterPath\Librewolf-WinUpdater.exe" -ShortcutPath "$startMenu\LibreWolf\LibreWolf WinUpdater.lnk" -WorkingDir $librewolfPath +New-Shortcut -Source "$updaterPath\Librewolf-WinUpdater.exe" -Destination "$startMenu\LibreWolf\LibreWolf WinUpdater.lnk" -WorkingDir $librewolfPath -Write-Output "Removing temp files" +# Finish +Write-Title "Removing temp files" Remove-Item "$outputLibrewolf" -Force Remove-Item "$outputLibrewolfUpdater" -Force \ No newline at end of file diff --git a/src/playbook/Executables/SOFTWARE.ps1 b/src/playbook/Executables/SOFTWARE.ps1 index 4e042f1731..747560fe4e 100644 --- a/src/playbook/Executables/SOFTWARE.ps1 +++ b/src/playbook/Executables/SOFTWARE.ps1 @@ -14,6 +14,7 @@ $msiArgs = "/qn /quiet /norestart ALLUSERS=1 REBOOT=ReallySuppress" $arm = ((Get-CimInstance -Class Win32_ComputerSystem).SystemType -match 'ARM64') -or ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64') # Create temporary directory +function Remove-TempDirectory { Pop-Location; Remove-Item -Path $tempDir -Force -Recurse -EA 0 } $tempDir = Join-Path -Path $(Get-SystemDrive) -ChildPath $([System.Guid]::NewGuid()) New-Item $tempDir -ItemType Directory -Force | Out-Null Push-Location $tempDir @@ -36,11 +37,11 @@ if ($Brave) { Write-Output "Still running BraveSetup." Start-Sleep -Seconds 2 } else { - Remove-Item "$tempDir" -ErrorAction SilentlyContinue -Force -Recurse + Remove-TempDirectory } } until (!$processesFound) - Stop-Process -Name "brave" -Force -ErrorAction SilentlyContinue + Stop-Process -Name "brave" -Force -EA 0 exit } @@ -52,6 +53,8 @@ if ($Firefox) { & curl.exe -LSs "https://download.mozilla.org/?product=firefox-latest-ssl&os=$firefoxArch&lang=en-US" -o "$tempDir\firefox.exe" Write-Output "Installing Firefox..." Start-Process -FilePath "$tempDir\firefox.exe" -WindowStyle Hidden -ArgumentList '/S /ALLUSERS=1' -Wait + + Remove-TempDirectory exit } @@ -62,6 +65,8 @@ if ($Chrome) { & curl.exe -LSs "https://dl.google.com/dl/chrome/install/googlechromestandaloneenterprise$chromeArch.msi" -o "$tempDir\chrome.msi" Write-Output "Installing Google Chrome..." Start-Process -FilePath "$tempDir\chrome.msi" -WindowStyle Hidden -ArgumentList '/qn' -Wait + + Remove-TempDirectory exit } @@ -136,7 +141,7 @@ $githubApi = Invoke-RestMethod "https://api.github.com/repos/M2Team/NanaZip/rele $assets = $githubApi.Assets.browser_download_url | Select-String ".xml", ".msixbundle" | Select-Object -Unique -First 2 function InstallNanaZip { Write-Output "Downloading NanaZip..." - $path = New-Item "$tempDir\nanazip-$(New-Guid)" -ItemType Directory + $path = New-Item "$tempDir\nanazip" -ItemType Directory $assets | ForEach-Object { $filename = $_ -split '/' | Select-Object -Last 1 Write-Output "Downloading '$filename'..." @@ -189,5 +194,4 @@ Write-Output "Installing legacy DirectX runtimes..." Start-Process -FilePath "$tempDir\directx\dxsetup.exe" -WindowStyle Hidden -ArgumentList '/silent' -Wait # Remove temporary directory -Pop-Location -Remove-Item -Path $tempDir -Force -Recurse -EA 0 \ No newline at end of file +Remove-TempDirectory \ No newline at end of file diff --git a/src/playbook/Executables/TASKBARPINS.ps1 b/src/playbook/Executables/TASKBARPINS.ps1 index b551d304db..a1250a5098 100644 --- a/src/playbook/Executables/TASKBARPINS.ps1 +++ b/src/playbook/Executables/TASKBARPINS.ps1 @@ -53,25 +53,13 @@ if ($Browser) { # Init shortcuts $tmp = New-Item (Join-Path -Path $([System.IO.Path]::GetTempPath()) -ChildPath $([System.Guid]::NewGuid())) -ItemType Directory -Force -function Add-Shortcut { - param ( - [string]$Temp = $tmp, - [string]$Path, - [string]$Destination - ) - - $Shortcut = (New-Object -ComObject WScript.Shell).CreateShortcut("$Temp\$Destination.lnk") - $Shortcut.TargetPath = $Path - $Shortcut.WorkingDirectory = Split-Path $Path - $Shortcut.Save() -} -Add-Shortcut -Path $($shortcuts.$explorer.Path) -Destination $explorer +New-Shortcut -Source $($shortcuts.$explorer.Path) -Destination "$tmp\$explorer.lnk" # Decide Registry Favorites if ([string]::IsNullOrEmpty($Browser)) { # If Edge exists, pin it, otherwise, pin only File Explorer $edgePath = $shortcuts.$edge.Path if (Test-Path $edgePath) { - Add-Shortcut -Path $edgePath -Destination $edge + New-Shortcut -Source $edgePath -Destination "$tmp\$edge.lnk" $Browser = $edge $regTaskbar = $shortcuts.$edge } else { @@ -80,7 +68,7 @@ if ([string]::IsNullOrEmpty($Browser)) { # If Edge exists, pin it, otherwise, pi $regTaskbar = $shortcuts.$explorer } } else { # Browser options - Add-Shortcut -Path $($shortcuts.$Browser.Path) -Destination $Browser + New-Shortcut -Source $($shortcuts.$Browser.Path) -Destination "$tmp\$Browser.lnk" $regTaskbar = $shortcuts.$Browser }