From a65b4d135e4b98066f1dbd0f0f459d41ebfb5fc5 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Mon, 21 Nov 2016 21:41:10 -0700 Subject: [PATCH] Fix error when testing for admin on PSCore Linux. This is somewhat experimental but the short-circuiting *if* statement should cause all existing users of Windows PowerShell (not core) to go through the old code path. Only PS Core users on LInux/OSX will go through the "id -u" path. I have tested this on Ubuntu but I don't have a Mac so can't test there. --- GitPrompt.ps1 | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/GitPrompt.ps1 b/GitPrompt.ps1 index a8ddfd24f..97b2843a0 100644 --- a/GitPrompt.ps1 +++ b/GitPrompt.ps1 @@ -95,8 +95,16 @@ $global:GitPromptSettings = New-Object PSObject -Property @{ TruncatedBranchSuffix = '...' } -$currentUser = [Security.Principal.WindowsPrincipal]([Security.Principal.WindowsIdentity]::GetCurrent()) -$isAdminProcess = $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) +# PowerShell 5.x only runs on Windows so use .NET types to determine isAdminProcess +# Or if we are on v6 or higher, check the $IsWindows pre-defined variable. +if (($PSVersionTable.PSVersion.Major -le 5) -or $IsWindows) { + $currentUser = [Security.Principal.WindowsPrincipal]([Security.Principal.WindowsIdentity]::GetCurrent()) + $isAdminProcess = $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) +} +else { + # Must be Linux or OSX, so use the id util. Root has userid of 0. + $isAdminProcess = 0 -eq (id -u) +} $adminHeader = if ($isAdminProcess) { 'Administrator: ' } else { '' } @@ -252,14 +260,14 @@ if(!(Test-Path Variable:Global:VcsPromptStatuses)) { $s = $global:GitPromptSettings # Override some of the normal colors if the background color is set to the default DarkMagenta. -if ($Host.UI.RawUI.BackgroundColor -eq [ConsoleColor]::DarkMagenta) { +if ($Host.UI.RawUI.BackgroundColor -eq [ConsoleColor]::DarkMagenta) { $s.LocalDefaultStatusForegroundColor = $s.LocalDefaultStatusForegroundBrightColor $s.LocalWorkingStatusForegroundColor = $s.LocalWorkingStatusForegroundBrightColor - $s.BeforeIndexForegroundColor = $s.BeforeIndexForegroundBrightColor - $s.IndexForegroundColor = $s.IndexForegroundBrightColor + $s.BeforeIndexForegroundColor = $s.BeforeIndexForegroundBrightColor + $s.IndexForegroundColor = $s.IndexForegroundBrightColor - $s.WorkingForegroundColor = $s.WorkingForegroundBrightColor + $s.WorkingForegroundColor = $s.WorkingForegroundBrightColor } function Global:Write-VcsStatus { $Global:VcsPromptStatuses | foreach { & $_ } }