ps/Modules/Cole.PowerShell.Developer/Public/Get-CurrentUsername.ps1
2023-05-30 22:51:22 -07:00

32 lines
963 B
PowerShell

function Get-CurrentUsername {
<#
.SYNOPSIS
This function is just a shim to make it easier to figure out who you are
#>
[CmdletBinding()]
[OutputType([string])]
param()
$logLead = (Get-LogLeadName)
$currentUsername = $env:USERNAME
if ([string]::IsNullOrWhiteSpace($currentUsername)) {
try {
$currentUsername = (whoami)
} catch {
Write-Verbose "$logLead : Is `whoami` supported on this system?"
}
}
if ([string]::IsNullOrWhiteSpace($currentUsername)) {
# How is this empty? Maybe someone jacked with the environment variables?
$homePath = (Resolve-Path -Path ~\ -ErrorAction Ignore)
if ([string]::IsNullOrWhiteSpace($homePath)) {
throw "$logLead : I have no clue what the current username is. Fix this sometime when you can repro."
}
$currentUsername = (Split-Path -Path $homePath -Leaf)
}
return $currentUsername
}