Merge pull request #307 from lucdekens/Fingertips

PS Profile
This commit is contained in:
Kyle Ruddy
2019-09-05 10:31:15 -04:00
committed by GitHub
2 changed files with 177 additions and 0 deletions

View File

@@ -0,0 +1,117 @@
#
# Provide environment information in the PS Console
#
# History:
# 1.0 - August 4th 2019 - LucD
# Initial version (for session HBI1729BU VMworld US 2019)
#
# 1) PS prompt
# - current (local) time
# - execution time of the previous command
# - shortened PWD
# 2) Window title
# - User/Admin
# - PS-32/54-Edition-Version
# - PCLI version
# - git repo/branch
# - VC/ESXi:defaultServer-User [# connections]
function prompt
{
# Current time
$date = (Get-Date).ToString('HH:mm:ss')
Write-Host -Object '[' -NoNewline
Write-Host -Object $date -ForegroundColor Cyan -BackgroundColor DarkBlue -NoNewline
Write-Host -Object ']' -NoNewline
# Execution time previous command
$history = Get-History -ErrorAction Ignore -Count 1
if ($history)
{
$time = ([DateTime](New-TimeSpan -Start $history.StartExecutionTime -End $history.EndExecutionTime).Ticks).ToString('HH:mm:ss.ffff')
Write-host -Object '[' -NoNewLine
Write-Host -Object "$time" -ForegroundColor Yellow -BackgroundColor DarkBlue -NoNewline
Write-host -Object '] ' -NoNewLine
}
# Shorted PWD
$path = $pwd.Path.Split('\')
if ($path.Count -gt 3)
{
$path = $path[0], '..', $path[-2], $path[-1]
}
Write-Host -Object "$($path -join '\')" -NoNewline
# Prompt function needs to return something,
# otherwise the default 'PS>' will be added
"> "
# Refresh the window's title
Set-Title
}
function Set-Title
{
# Running as Administrator or a regular user
$userInfo = [Security.Principal.WindowsIdentity]::GetCurrent()
if ((New-Object Security.Principal.WindowsPrincipal $userInfo).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator))
{
$role = 'Admin'
}
else
{
$role = 'User'
}
# Usertype user@hostname
$user = "$role $($userInfo.Name)@$($env:computername)"
# PowerShell environment/PS version
$bits = 32
if ([Environment]::Is64BitProcess)
{
$bits = 64
}
$ps = " - PS-$($bits): $PSEdition/$($PSVersionTable.PSVersion.ToString())"
# PowerCLI version (derived from module VMware.PowerCLI)
$pcliModule = Get-Module -Name VMware.PowerCLI -ListAvailable |
Sort-Object -Property Version -Descending |
Select-Object -First 1
$pcli = " - PCLI: $($pcliModule.Version.ToString())"
# If git is present and if in a git controlled folder, display repositoryname/current_branch
$gitStr = ''
if ((Get-Command -Name 'git' -CommandType Application -ErrorAction SilentlyContinue).Count -gt 0)
{
$gitTopLevel = & { git rev-parse --show-toplevel 2> $null }
if ($gitTopLevel.Length -ne 0)
{
$gitRepo = Split-Path -Path $gitTopLevel -Leaf
$gitBranch = (git branch | Where-Object { $_ -match "\*" }).Trimstart('* ')
$gitStr = " - git: $gitRepo/$gitBranch"
}
}
# If there is an open vSphere Server connection
# display [VC|ESXi] last_connected_server-connected_user [number of open server connections]
if ($global:defaultviserver)
{
$vcObj = (Get-Variable -Scope global -Name 'DefaultVIServer').Value
if ($vcObj.ProductLine -eq 'vpx')
{
$vcSrv = 'VC'
}
else
{
$vcSrv = 'ESXi'
}
$vc = " - $($vcSrv): $($vcObj.Name)-$($vcObj.User) [$($global:DefaultVIServers.Count)]"
}
# Update the Window's title
$host.ui.RawUI.WindowTitle = "$user$ps$pcli$vc$gitStr"
}
# Set title after starting session
Set-Title

View File

@@ -0,0 +1,60 @@
[cmdletbinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
param(
# [Parameter(Mandatory = $true)]
[ValidateSet('CurrentUserCurrentHost', 'CurrentUserAllHosts',
'AllUsersCurrentHost', 'AllUsersAllHosts')]
[string]$Scope,
[switch]$NoClobber,
[switch]$Backup,
[string]$NewProfile = '.\NewProfile.ps1'
)
if ($PSCmdlet.ShouldProcess("$($Profile.$Scope)", "Create $Scope profile"))
{
$profilePath = $Profile."$Scope"
Write-Verbose -Message "Target is $profilePath"
$createProfile = $true
if (Test-Path -Path $profilePath)
{
Write-Verbose -Message "Target exists"
if ($NoClobber)
{
Write-Verbose -Message "Cannot overwrite target due to NoClobber"
$createProfile = $false
}
elseif ($Backup)
{
Write-Verbose -Message "Create a backup as $profilePath.bak"
Copy-Item -Path $profilePath -Destination "$profilePath.bak" -Confirm:$false -Force
}
elseif (-not $NoClobber)
{
Write-Verbose -Message "Target will be overwritten"
}
else
{
Write-Verbose -Message "Use -NoClobber:$false or -Backup"
}
}
if ($createProfile)
{
if (-not $NewProfile)
{
$script:MyInvocation.MyCommand | select *
$folder = Split-Path -Parent -Path $script:MyInvocation.MyCommand.Path
$folder = Get-Location
$NewProfile = "$folder\NewProfile.ps1"
}
Write-Verbose -Message "New profile expected at $NewProfile"
if (Test-Path -Path $NewProfile)
{
Write-Verbose -Message "Copy $NewProfile to $profilePath"
Copy-Item -Path $NewProfile -Destination $profilePath -Confirm:$false
}
else
{
Write-Warning -Message "Could not find the new profile file!"
Write-Warning -Message "Use the NewProfile parameter or store a NewProfile.ps1 file in folder $folder."
}
}
}