Skip to content

Commit 36ee554

Browse files
authored
Merge pull request #312 from rkeithhill/rkeithhill/fix-admin-test-on-non-Windows
Fix error when testing for admin on PSCore Linux.
2 parents fba883f + a65b4d1 commit 36ee554

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

GitPrompt.ps1

+14-6
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,16 @@ $global:GitPromptSettings = New-Object PSObject -Property @{
9595
TruncatedBranchSuffix = '...'
9696
}
9797

98-
$currentUser = [Security.Principal.WindowsPrincipal]([Security.Principal.WindowsIdentity]::GetCurrent())
99-
$isAdminProcess = $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
98+
# PowerShell 5.x only runs on Windows so use .NET types to determine isAdminProcess
99+
# Or if we are on v6 or higher, check the $IsWindows pre-defined variable.
100+
if (($PSVersionTable.PSVersion.Major -le 5) -or $IsWindows) {
101+
$currentUser = [Security.Principal.WindowsPrincipal]([Security.Principal.WindowsIdentity]::GetCurrent())
102+
$isAdminProcess = $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
103+
}
104+
else {
105+
# Must be Linux or OSX, so use the id util. Root has userid of 0.
106+
$isAdminProcess = 0 -eq (id -u)
107+
}
100108

101109
$adminHeader = if ($isAdminProcess) { 'Administrator: ' } else { '' }
102110

@@ -252,14 +260,14 @@ if(!(Test-Path Variable:Global:VcsPromptStatuses)) {
252260
$s = $global:GitPromptSettings
253261

254262
# Override some of the normal colors if the background color is set to the default DarkMagenta.
255-
if ($Host.UI.RawUI.BackgroundColor -eq [ConsoleColor]::DarkMagenta) {
263+
if ($Host.UI.RawUI.BackgroundColor -eq [ConsoleColor]::DarkMagenta) {
256264
$s.LocalDefaultStatusForegroundColor = $s.LocalDefaultStatusForegroundBrightColor
257265
$s.LocalWorkingStatusForegroundColor = $s.LocalWorkingStatusForegroundBrightColor
258266

259-
$s.BeforeIndexForegroundColor = $s.BeforeIndexForegroundBrightColor
260-
$s.IndexForegroundColor = $s.IndexForegroundBrightColor
267+
$s.BeforeIndexForegroundColor = $s.BeforeIndexForegroundBrightColor
268+
$s.IndexForegroundColor = $s.IndexForegroundBrightColor
261269

262-
$s.WorkingForegroundColor = $s.WorkingForegroundBrightColor
270+
$s.WorkingForegroundColor = $s.WorkingForegroundBrightColor
263271
}
264272

265273
function Global:Write-VcsStatus { $Global:VcsPromptStatuses | foreach { & $_ } }

0 commit comments

Comments
 (0)