Add title setting logic to windev.ps1

This commit is contained in:
Cryostrixx 2024-10-10 15:12:01 -07:00
parent 974ac3ae40
commit 8c8e3a5509
No known key found for this signature in database
GPG Key ID: 2FC11420AFB82801
2 changed files with 36 additions and 8 deletions

View File

@ -78,16 +78,14 @@ if ($processCmd -ne $powershellCmd) {
$launchArguments = "$powershellCmd $launchArguments"
}
# Store the script's directory in $CurrentDirectory
# Note: This is not used with -WorkingDirectory but
# is used in the PowerShell instance's window title.
# Store the script's directory; used in the fallback window title
$CurrentDirectory = if ($MyInvocation.MyCommand.Path) {
"$($MyInvocation.MyCommand.Path)"
} elseif ($PSScriptRoot) {
"$($PSScriptRoot)"
} elseif ($PWD) { "$PWD" }
# Create the base titles used for naming the instance
# Create the fallback and base window titles used in the window title
$FallbackWindowTitle = "$CurrentDirectory\winutil.ps1"
$BaseWindowTitle = if ($MyInvocation.MyCommand.Path) {
$MyInvocation.MyCommand.Path
@ -95,7 +93,7 @@ $BaseWindowTitle = if ($MyInvocation.MyCommand.Path) {
$MyInvocation.MyCommand.Definition
}
# Prepend (User) or (Admin) prefix to the window title
# Add elevation status prefix to the beginning of the window title
try {
$Host.UI.RawUI.WindowTitle = if (!$isElevated) {
"(User) " + $BaseWindowTitle
@ -104,9 +102,9 @@ try {
}
} catch {
$Host.UI.RawUI.WindowTitle = if (!$isElevated) {
"(User) $FallbackWindowTitle"
"(User) " + $FallbackWindowTitle
} else {
"(Admin) $FallbackWindowTitle"
"(Admin) " + $FallbackWindowTitle
}
}

View File

@ -137,6 +137,36 @@ function Start-LatestWinUtil {
$WinUtilLaunchArguments += " " + $($WinUtilArgumentsList -join " ")
}
# Store the script's current directory; this is used as part of the instance's fallback window title.
$CurrentDirectory = if ($MyInvocation.MyCommand.Path) {
"$($MyInvocation.MyCommand.Path)"
} elseif ($PSScriptRoot) {
"$($PSScriptRoot)"
} elseif ($PWD) { "$PWD" }
# Create the base and fallback window titles used for setting the window title of the running instance.
$FallbackWindowTitle = "$CurrentDirectory\winutil.ps1"
$BaseWindowTitle = if ($MyInvocation.MyCommand.Path) {
$MyInvocation.MyCommand.Path
} else {
$MyInvocation.MyCommand.Definition
}
# Add the processes' elevation status prefix to the beginning of the running instance's window title.
try {
$Host.UI.RawUI.WindowTitle = if (!$isElevated) {
"(User) " + $BaseWindowTitle
} else {
"(Admin) " + $BaseWindowTitle
}
} catch {
$Host.UI.RawUI.WindowTitle = if (!$isElevated) {
"(User) " + $FallbackWindowTitle
} else {
"(Admin) " + $FallbackWindowTitle
}
}
# If the WinUtil script is not running as administrator, relaunch the script with administrator permissions.
if (!$ProcessIsElevated) {
Write-Host "WinUtil is not running as administrator. Relaunching..." -ForegroundColor DarkCyan
@ -173,4 +203,4 @@ try {
}
} catch {
Write-Host "Error launching WinUtil '$($WinUtilReleaseTag)': $_" -ForegroundColor Red
}
}