Test-Admin utility

This commit is contained in:
talon 2023-06-17 15:29:09 -06:00
parent 85582483ed
commit 153185ba84
3 changed files with 16 additions and 25 deletions

View file

@ -31,26 +31,24 @@ New-Item -Path $PROFILE.CurrentUserAllHosts -ItemType "file" -Force
notepad $PROFILE.CurrentUserAllHosts notepad $PROFILE.CurrentUserAllHosts
``` ```
A very simple first customization one can do is update the prompt. This will indicate whether or not you're running as an Adminsitrator. A very simple first customization one can do is update the prompt. This will indicate whether or not you're running as an Administrator.
```powershell ```powershell
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
function prompt { function prompt {
$prompt = "> " $prompt = "> "
$current_user = New-Object Security.Principal.WindowsPrincipal( If (Test-Admin -eq $true) {
[Security.Principal.WindowsIdentity]::GetCurrent()
)
If ($current_user.IsInRole(
[Security.Principal.WindowsBuiltinRole]::Administrator
)) {
$prompt = "Admin$prompt" $prompt = "Admin$prompt"
} }
$prompt $prompt
} }
``` ```
I also added `install_scoop` and `install_bleachbit` utility functions. They are totally optional but, I think, useful. Note the `Test-Admin` function, which is now available globally, will be used again in the `full_update.ps1` script and is generally a nice utility to have. I also added `Install-Scoop` and `Install-BleachBit` utility functions. They are totally optional but, I think, useful.
# `$env:PATH` # `$env:PATH`

View file

@ -1,10 +1,5 @@
param([switch]$Elevated) param([switch]$Elevated)
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) { if ((Test-Admin) -eq $false) {
if ($elevated) { if ($elevated) {
# tried to elevate, did not work, aborting # tried to elevate, did not work, aborting

View file

@ -1,29 +1,27 @@
# Scripts in this $PROFILE folder are added to the PATH # Scripts in this $PROFILE folder are added to the PATH
$env:path += ";$(Split-Path -Parent $MyInvocation.MyCommand.Path)" $env:path += ";$(Split-Path -Parent $MyInvocation.MyCommand.Path)"
function Test-Admin {
$current_user = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$current_user.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
function prompt { function prompt {
$prompt = "> " $prompt = "> "
$current_user = New-Object Security.Principal.WindowsPrincipal( If (Test-Admin -eq $true) {
[Security.Principal.WindowsIdentity]::GetCurrent()
)
If ($current_user.IsInRole(
[Security.Principal.WindowsBuiltinRole]::Administrator
)) {
$prompt = "Admin$prompt" $prompt = "Admin$prompt"
} }
$prompt $prompt
} }
function install_scoop { function Install-Scoop {
# You can read the script before executing it here: # You can read the script before executing it here:
# https://raw.githubusercontent.com/scoopinstaller/install/master/install.ps1 # https://raw.githubusercontent.com/scoopinstaller/install/master/install.ps1
Invoke-RestMethod get.scoop.sh | Invoke-Expression Invoke-RestMethod get.scoop.sh | Invoke-Expression
# (un)install docs: https://github.com/ScoopInstaller/Install # (un)install docs: https://github.com/ScoopInstaller/Install
} }
function install_bleachbit { function Install-BleachBit {
scoop bucket add extras scoop bucket add extras
scoop install bleachbit scoop install bleachbit
} }