diff --git a/src/GitPrompt.ps1 b/src/GitPrompt.ps1 index 3732831f3..010af199c 100644 --- a/src/GitPrompt.ps1 +++ b/src/GitPrompt.ps1 @@ -112,16 +112,7 @@ $global:GitPromptSettings = [pscustomobject]@{ TruncatedBranchSuffix = '...' } -# 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) -} +$isAdminProcess = Test-Administrator $adminHeader = if ($isAdminProcess) { 'Administrator: ' } else { '' } diff --git a/src/Utils.ps1 b/src/Utils.ps1 index 665680a28..8a96e982d 100644 --- a/src/Utils.ps1 +++ b/src/Utils.ps1 @@ -54,6 +54,18 @@ function Invoke-Utf8ConsoleCommand([ScriptBlock]$cmd) { } } +function Test-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()) + return $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) + } + + # Must be Linux or OSX, so use the id util. Root has userid of 0. + return 0 -eq (id -u) +} + <# .SYNOPSIS Configures your PowerShell profile (startup) script to import the posh-git @@ -65,7 +77,12 @@ function Invoke-Utf8ConsoleCommand([ScriptBlock]$cmd) { .PARAMETER AllHosts By default, this command modifies the CurrentUserCurrentHost profile script. By specifying the AllHosts switch, the command updates the - CurrentUserAllHosts profile. + CurrentUserAllHosts profile (or AllUsersAllHosts, given -AllUsers). +.PARAMETER AllUsers + By default, this command modifies the CurrentUserCurrentHost profile + script. By specifying the AllUsers switch, the command updates the + AllUsersCurrentHost profile (or AllUsersAllHosts, given -AllHosts). + Requires elevated permissions. .PARAMETER Force Do not check if the specified profile script is already importing posh-git. Just add Import-Module posh-git command. @@ -76,7 +93,7 @@ function Invoke-Utf8ConsoleCommand([ScriptBlock]$cmd) { Updates your profile script for the current PowerShell host to import the posh-git module when the current PowerShell host starts. .EXAMPLE - PS C:\> Add-PoshGitToProfile -AllHost + PS C:\> Add-PoshGitToProfile -AllHosts Updates your profile script for all PowerShell hosts to import the posh-git module whenever any PowerShell host starts. .INPUTS @@ -91,6 +108,10 @@ function Add-PoshGitToProfile { [switch] $AllHosts, + [Parameter()] + [switch] + $AllUsers, + [Parameter()] [switch] $Force, @@ -104,9 +125,18 @@ function Add-PoshGitToProfile { $TestParams ) + if ($AllUsers -and !(Test-Administrator)) { + throw 'Adding posh-git to an AllUsers profile requires an elevated host.' + } + $underTest = $false - $profilePath = if ($AllHosts) { $PROFILE.CurrentUserAllHosts } else { $PROFILE.CurrentUserCurrentHost } + $profileName = $(if ($AllUsers) { 'AllUsers' } else { 'CurrentUser' }) ` + + $(if ($AllHosts) { 'AllHosts' } else { 'CurrentHost' }) + Write-Verbose "`$profileName = '$profileName'" + + $profilePath = $PROFILE.$profileName + Write-Verbose "`$profilePath = '$profilePath'" # Under test, we override some variables using $args as a backdoor. if (($TestParams.Count -gt 0) -and ($TestParams[0] -is [string])) { @@ -149,7 +179,6 @@ function Add-PoshGitToProfile { if (!$profilePath) { Write-Warning "Skipping add of posh-git import to profile; no profile found." - Write-Verbose "`$profilePath = '$profilePath'" Write-Verbose "`$PROFILE = '$PROFILE'" Write-Verbose "CurrentUserCurrentHost = '$($PROFILE.CurrentUserCurrentHost)'" Write-Verbose "CurrentUserAllHosts = '$($PROFILE.CurrentUserAllHosts)'"