Merge pull request #308 from virtualex-itv/master

PS Profile - Update
This commit is contained in:
Kyle Ruddy
2019-09-10 13:35:49 -04:00
committed by GitHub

View File

@@ -4,43 +4,50 @@
# History: # History:
# 1.0 - August 4th 2019 - LucD # 1.0 - August 4th 2019 - LucD
# Initial version (for session HBI1729BU VMworld US 2019) # Initial version (for session HBI1729BU VMworld US 2019)
# 2.0 - September 9th 2019 - virtualex
# Added PowerShell-Core compatibility
# #
# 1) PS prompt # 1) PS prompt
# - detect pwsh-core
# - current (local) time # - current (local) time
# - execution time of the previous command # - execution time of the previous command
# - shortened PWD # - shortened PWD
# 2) Window title # 2) Window title
# - User/Admin # - User/Admin
# - PS-32/54-Edition-Version # - PS-32/64-Edition-Version
# - PCLI version # - PCLI version
# - git repo/branch # - git repo/branch
# - VC/ESXi:defaultServer-User [# connections] # - VC/ESXi:defaultServer-User [# connections]
function prompt function prompt
{ {
# Detect PS-Core
If ($PSVersionTable.PSEdition -eq 'Core') {
Write-Host '(Core) ' -NoNewLine
}
# Current time # Current time
$date = (Get-Date).ToString('HH:mm:ss') $date = (Get-Date).ToString('HH:mm:ss')
Write-Host -Object '[' -NoNewline Write-Host -Object '[' -NoNewLine
Write-Host -Object $date -ForegroundColor Cyan -BackgroundColor DarkBlue -NoNewline Write-Host -Object $date -ForegroundColor Cyan -BackgroundColor DarkBlue -NoNewline
Write-Host -Object ']' -NoNewline Write-Host -Object ']' -NoNewLine
# Execution time previous command # Execution time previous command
$history = Get-History -ErrorAction Ignore -Count 1 $history = Get-History -ErrorAction Ignore -Count 1
if ($history) if ($history)
{ {
$time = ([DateTime](New-TimeSpan -Start $history.StartExecutionTime -End $history.EndExecutionTime).Ticks).ToString('HH:mm:ss.ffff') $time = ([DateTime](New-TimeSpan -Start $history.StartExecutionTime -End $history.EndExecutionTime).Ticks).ToString('HH:mm:ss.ffff')
Write-host -Object '[' -NoNewLine Write-Host -Object '[' -NoNewLine
Write-Host -Object "$time" -ForegroundColor Yellow -BackgroundColor DarkBlue -NoNewline Write-Host -Object "$time" -ForegroundColor Yellow -BackgroundColor DarkBlue -NoNewLine
Write-host -Object '] ' -NoNewLine Write-Host -Object '] ' -NoNewLine
} }
# Shorted PWD # Shortened PWD
$path = $pwd.Path.Split('\') $path = $pwd.Path.Split('\')
if ($path.Count -gt 3) if ($path.Count -gt 3)
{ {
$path = $path[0], '..', $path[-2], $path[-1] $path = $path[0], '..', $path[-2], $path[-1]
} }
Write-Host -Object "$($path -join '\')" -NoNewline Write-Host -Object "$($path -join '\')" -NoNewLine
# Prompt function needs to return something, # Prompt function needs to return something,
# otherwise the default 'PS>' will be added # otherwise the default 'PS>' will be added
@@ -53,18 +60,27 @@ function prompt
function Set-Title function Set-Title
{ {
# Running as Administrator or a regular user # Running as Administrator or a regular user
$userInfo = [Security.Principal.WindowsIdentity]::GetCurrent() If (($PSVersionTable.PSEdition -eq 'Core') -and ($IsWindows -eq 'True') -or ($PSVersionTable.PSEdition -ine 'Core'))
if ((New-Object Security.Principal.WindowsPrincipal $userInfo).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) {
{ $userInfo = [Security.Principal.WindowsIdentity]::GetCurrent()
$role = 'Admin' if ((New-Object Security.Principal.WindowsPrincipal $userInfo).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator))
} {
else $role = 'Admin'
{ }
$role = 'User' else
{
$role = 'User'
}
} }
# Usertype user@hostname # Usertype user@hostname
If (($PSVersionTable.PSEdition -eq 'Core') -and ($IsWindows -ine 'True')) {
$env:computername = hostname
$user = "$($env:user)@$($env:computername)"
}
Else {
$user = "$role $($userInfo.Name)@$($env:computername)" $user = "$role $($userInfo.Name)@$($env:computername)"
}
# PowerShell environment/PS version # PowerShell environment/PS version
$bits = 32 $bits = 32