From affb3b5b12ddbc7fca5876d92cc5f4e60e780c15 Mon Sep 17 00:00:00 2001 From: he3als <65787561+he3als@users.noreply.github.com> Date: Sun, 8 Sep 2024 18:39:23 +0100 Subject: [PATCH] feat: Read-Pause function --- .../AtlasModules/Scripts/Modules/Utils/Utils.psm1 | 13 ++++++++++++- .../Scripts/ScriptWrappers/DefaultPowerSaving.ps1 | 5 ++++- .../Scripts/ScriptWrappers/DisablePowerSaving.ps1 | 7 +++++-- .../Scripts/ScriptWrappers/TelemetryComponents.ps1 | 13 ++++++++----- .../Scripts/ScriptWrappers/ToggleDefender.ps1 | 13 ++++++++----- .../Executables/AtlasModules/Scripts/newUsers.ps1 | 2 +- .../AtlasModules/Scripts/packageInstall.ps1 | 14 +++++--------- 7 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/playbook/Executables/AtlasModules/Scripts/Modules/Utils/Utils.psm1 b/src/playbook/Executables/AtlasModules/Scripts/Modules/Utils/Utils.psm1 index f99e1afac9..b4e1176832 100644 --- a/src/playbook/Executables/AtlasModules/Scripts/Modules/Utils/Utils.psm1 +++ b/src/playbook/Executables/AtlasModules/Scripts/Modules/Utils/Utils.psm1 @@ -8,6 +8,17 @@ function Write-Title { Write-Host "`n$dashes`n$text`n$dashes" -ForegroundColor Yellow } +function Read-Pause { + param ( + [Parameter(ValueFromPipeline = $true)] + [string]$Message = "Press Enter to exit", + [switch]$NewLine + ) + + if ($NewLine) { Write-Output "" } + $null = Read-Host $Message +} + enum MsgIcon { Stop Question @@ -57,4 +68,4 @@ function Read-MessageBox { } } -Export-ModuleMember -Function Write-Title, Read-MessageBox \ No newline at end of file +Export-ModuleMember -Function Write-Title, Read-Pause, Read-MessageBox \ No newline at end of file diff --git a/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/DefaultPowerSaving.ps1 b/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/DefaultPowerSaving.ps1 index d1816982e0..45a5821255 100644 --- a/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/DefaultPowerSaving.ps1 +++ b/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/DefaultPowerSaving.ps1 @@ -1,5 +1,8 @@ #Requires -RunAsAdministrator +$windir = [Environment]::GetFolderPath('Windows') +& "$windir\AtlasModules\initPowerShell.ps1" + Write-Host "`nRestoring default power schemes..." -ForegroundColor Yellow # This should set the power plan to 'Balanced' by default powercfg /restoredefaultschemes | Out-Null @@ -79,4 +82,4 @@ Remove-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Power\PowerThr bcdedit /deletevalue disabledynamictick *> $null # Finish -$null = Read-Host "`nCompleted.`nPress Enter to exit" \ No newline at end of file +Read-Pause "`nCompleted.`nPress Enter to exit" \ No newline at end of file diff --git a/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/DisablePowerSaving.ps1 b/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/DisablePowerSaving.ps1 index 3e5e76fc6c..31df8fee48 100644 --- a/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/DisablePowerSaving.ps1 +++ b/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/DisablePowerSaving.ps1 @@ -4,6 +4,9 @@ param ( [switch]$Silent ) +$windir = [Environment]::GetFolderPath('Windows') +& "$windir\AtlasModules\initPowerShell.ps1" + if (!$Silent) { $isLaptop = (Get-CimInstance -Class Win32_ComputerSystem -Property PCSystemType).PCSystemType -eq 2 if ($isLaptop) { @@ -54,7 +57,7 @@ powercfg /setacvalueindex scheme_current 54533251-82be-4824-96c1-47b60b740d00 4d powercfg /setactive scheme_current Write-Host "Disabling power-saving ACPI devices..." -ForegroundColor Yellow -& "$([Environment]::GetFolderPath('Windows'))\AtlasModules\Scripts\toggleDev.cmd" -Disable '@("ACPI Processor Aggregator", "Microsoft Windows Management Interface for ACPI")' | Out-Null +& "$windir\AtlasModules\Scripts\toggleDev.cmd" -Disable '@("ACPI Processor Aggregator", "Microsoft Windows Management Interface for ACPI")' | Out-Null Write-Host "Disabling network adapter power-saving..." -ForegroundColor Yellow $properties = Get-NetAdapter -Physical | Get-NetAdapterAdvancedProperty @@ -130,4 +133,4 @@ New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Power\PowerThrott bcdedit /set disabledynamictick yes | Out-Null if ($Silent) { exit } -$null = Read-Host "`nCompleted.`nPress Enter to exit" +Read-Pause "`nCompleted.`nPress Enter to exit" diff --git a/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/TelemetryComponents.ps1 b/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/TelemetryComponents.ps1 index e97e91757c..3acf82a95d 100644 --- a/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/TelemetryComponents.ps1 +++ b/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/TelemetryComponents.ps1 @@ -1,5 +1,8 @@ $ProgressPreference = 'SilentlyContinue' -function Pause ($message = "Press Enter to exit") { $null = Read-Host $message } + +$windir = [Environment]::GetFolderPath('Windows') +& "$windir\AtlasModules\initPowerShell.ps1" + function Write-BulletPoint($message) { Write-Host " - " -ForegroundColor Green -NoNewline Write-Host $message @@ -11,14 +14,14 @@ function Restart { Restart-Computer } else { Write-Host "`nChanges will apply after next restart." -ForegroundColor Yellow - Pause + Read-Pause } } -$packageInstall = "$([Environment]::GetFolderPath('Windows'))\AtlasModules\Scripts\packageInstall.ps1" +$packageInstall = "$windir\AtlasModules\Scripts\packageInstall.ps1" if (!(Test-Path $packageInstall)) { Write-Host "Missing package install script, can't continue." - Pause + Read-Pause exit 1 } @@ -29,7 +32,7 @@ try { } catch { if (!$?) { Write-Host "Failed to get packages! $_" -ForegroundColor Red - Pause + Read-Pause exit 1 } } diff --git a/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/ToggleDefender.ps1 b/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/ToggleDefender.ps1 index b2859ad53e..81b593b35f 100644 --- a/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/ToggleDefender.ps1 +++ b/src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/ToggleDefender.ps1 @@ -1,5 +1,8 @@ $ProgressPreference = 'SilentlyContinue' -function Pause ($message = "Press Enter to exit") { $null = Read-Host $message } + +$windir = [Environment]::GetFolderPath('Windows') +& "$windir\AtlasModules\initPowerShell.ps1" + function Restart { Write-Host "`nCompleted!" -ForegroundColor Green choice /c yn /n /m "Would you like to restart now to apply the changes? [Y/N] " @@ -7,14 +10,14 @@ function Restart { Restart-Computer } else { Write-Host "`nChanges will apply after next restart." -ForegroundColor Yellow - Pause + Read-Pause } } -$packageInstall = "$([Environment]::GetFolderPath('Windows'))\AtlasModules\Scripts\packageInstall.ps1" +$packageInstall = "$windir\AtlasModules\Scripts\packageInstall.ps1" if (!(Test-Path $packageInstall)) { Write-Host "Missing package install script, can't continue." - if (!$args) { Pause } + if (!$args) { Read-Pause } exit 1 } @@ -25,7 +28,7 @@ try { } catch { if (!$?) { Write-Host "Failed to get packages! $_" -ForegroundColor Red - Pause + Read-Pause exit 1 } } diff --git a/src/playbook/Executables/AtlasModules/Scripts/newUsers.ps1 b/src/playbook/Executables/AtlasModules/Scripts/newUsers.ps1 index 0d1b123f76..d63cbeac79 100644 --- a/src/playbook/Executables/AtlasModules/Scripts/newUsers.ps1 +++ b/src/playbook/Executables/AtlasModules/Scripts/newUsers.ps1 @@ -7,7 +7,7 @@ $title = 'Preparing Atlas user settings...' if (!(Test-Path $atlasDesktop) -or !(Test-Path $atlasModules)) { Write-Host "Atlas was about to configure user settings, but its files weren't found. :(" -ForegroundColor Red - $null = Read-Host "Press Enter to exit" + Read-Pause exit 1 } diff --git a/src/playbook/Executables/AtlasModules/Scripts/packageInstall.ps1 b/src/playbook/Executables/AtlasModules/Scripts/packageInstall.ps1 index eaa7441a11..54eb0794b8 100644 --- a/src/playbook/Executables/AtlasModules/Scripts/packageInstall.ps1 +++ b/src/playbook/Executables/AtlasModules/Scripts/packageInstall.ps1 @@ -34,10 +34,6 @@ $safeModeStatus = (Get-CimInstance -Class Win32_ComputerSystem).BootupState -ne # ======================================================================================================================= # # FUNCTIONS # # ======================================================================================================================= # -function Pause ($message = "Press Enter to exit") { - $null = Read-Host $message -} - function Write-BulletPoint($message) { $message | Foreach-Object { Write-Host " - " -ForegroundColor Green -NoNewline @@ -83,7 +79,7 @@ function Restart { Restart-Computer Start-Sleep 2 Write-Host "Something seems to have went wrong restarting automatically, restart manually." -ForegroundColor Red - if (!$NoInteraction) { Pause } + if (!$NoInteraction) { Read-Pause } exit 9000 } @@ -127,7 +123,7 @@ $seperator function NoRestart { Write-Host "`nIf any packages installed successfully, they will apply next restart." -ForegroundColor Yellow - Pause + Read-Pause } if ($safeModeStatus) { @@ -157,7 +153,7 @@ $seperator Restart } else { Write-Host "`nChanges will apply next restart." -ForegroundColor Yellow - Pause + Read-Pause exit $script:errorLevel } } @@ -221,7 +217,7 @@ if ($InstallPackages) { if ($matchedPackages.Count -eq 0) { Write-Host "[ERROR] The specified CABs ($InstallPackages) to install weren't found." -ForegroundColor Red - if (!$NoInteraction) { Pause } + if (!$NoInteraction) { Read-Pause } exit 1 } if ($notMatchedPackages.Count -gt 0) { @@ -277,7 +273,7 @@ Please note that if you chose to disable Windows Defender, it may still remain e # ======================================================================================================================= # if (!$matchedPackages) { Write-Host "This will install specified CBS packages online, meaning live on your current install of Windows." -ForegroundColor Yellow - Pause "Press Enter to continue" + Read-Pause "Press Enter to continue" Write-Host "`n[INFO] Opening file dialog to select CBS package CAB..." Add-Type -AssemblyName System.Windows.Forms