From 1ac24c236e3b7136efedc557c08f61ed38db316c Mon Sep 17 00:00:00 2001 From: MyDrift Date: Tue, 10 Sep 2024 21:05:55 +0200 Subject: [PATCH] CTT Powershell Profile (#2707) * add main logic * finish logic --- config/feature.json | 8 +++ functions/private/Invoke-WinUtilpsProfile.ps1 | 50 +++++++++++++++++++ functions/public/Invoke-WPFButton.ps1 | 1 + 3 files changed, 59 insertions(+) create mode 100644 functions/private/Invoke-WinUtilpsProfile.ps1 diff --git a/config/feature.json b/config/feature.json index 0013b211..10835451 100644 --- a/config/feature.json +++ b/config/feature.json @@ -305,5 +305,13 @@ "Type": "Button", "ButtonWidth": "300", "link": "https://christitustech.github.io/winutil/dev/features/Legacy-Windows-Panels/user" + }, + "WPFWinUtilPSProfile": { + "Content": "Install CTT PowerShell Profile", + "category": "Powershell Profile", + "panel": "2", + "Order": "a083_", + "Type": "Button", + "ButtonWidth": "300" } } diff --git a/functions/private/Invoke-WinUtilpsProfile.ps1 b/functions/private/Invoke-WinUtilpsProfile.ps1 new file mode 100644 index 00000000..318e7921 --- /dev/null +++ b/functions/private/Invoke-WinUtilpsProfile.ps1 @@ -0,0 +1,50 @@ +function Invoke-WinUtilpsProfile { + <# + .SYNOPSIS + Installs & applies the CTT Powershell Profile + #> + Invoke-WPFRunspace -Argumentlist $PROFILE -DebugPreference $DebugPreference -ScriptBlock { + param ( $psprofile) + function Invoke-PSSetup { + $url = "https://raw.githubusercontent.com/ChrisTitusTech/powershell-profile/main/Microsoft.PowerShell_profile.ps1" + $oldhash = Get-FileHash $psprofile -ErrorAction SilentlyContinue + Invoke-RestMethod $url -OutFile "$env:temp/Microsoft.PowerShell_profile.ps1" + $newhash = Get-FileHash "$env:temp/Microsoft.PowerShell_profile.ps1" + if ($newhash.Hash -ne $oldhash.Hash) { + write-host "===> Installing Profile.. <===" -ForegroundColor Yellow + # Starting new hidden shell process bc setup does not work in a runspace + Start-Process -FilePath "pwsh" -ArgumentList "-ExecutionPolicy Bypass -NoProfile -Command `"Invoke-Expression (Invoke-WebRequest `'https://github.com/ChrisTitusTech/powershell-profile/raw/main/setup.ps1`')`"" -WindowStyle Hidden -Wait + Write-Host "Profile has been installed. Please restart your shell to reflect changes!" -ForegroundColor Magenta + write-host "===> Finished <===" -ForegroundColor Yellow + } else { + Write-Host "Profile is up to date" -ForegroundColor Green + } + } + + if (Get-Command "pwsh" -ErrorAction SilentlyContinue) { + if ($PSVersionTable.PSVersion.Major -ge 7) { + Invoke-PSSetup + } + else { + write-host "Profile requires Powershell 7, which is currently installed but not used!" -ForegroundColor Red + # Load the necessary assembly for Windows Forms + Add-Type -AssemblyName System.Windows.Forms + # Display the Yes/No message box + $question = [System.Windows.Forms.MessageBox]::Show("Profile requires Powershell 7, which is currently installed but not used! Do you want to install Profile for Powershell 7?", "Question", + [System.Windows.Forms.MessageBoxButtons]::YesNo, + [System.Windows.Forms.MessageBoxIcon]::Question) + + # Check the result + if ($question -eq [System.Windows.Forms.DialogResult]::Yes) { + Invoke-PSSetup + } + else { + Write-Host "Not proceeding with the profile setup!" + } + } + } + else { + write-host "Profile requires Powershell 7, which is not installed!" -ForegroundColor Red + } + } +} diff --git a/functions/public/Invoke-WPFButton.ps1 b/functions/public/Invoke-WPFButton.ps1 index 65d07a04..9f768fd8 100644 --- a/functions/public/Invoke-WPFButton.ps1 +++ b/functions/public/Invoke-WPFButton.ps1 @@ -58,5 +58,6 @@ function Invoke-WPFButton { "WPFMicrowin" {Invoke-WPFMicrowin} "WPFCloseButton" {Invoke-WPFCloseButton} "MicrowinScratchDirBT" {Invoke-ScratchDialog} + "WPFWinUtilPSProfile" {Invoke-WinUtilpsProfile} } }