En préambule, je vous invite à jetter un coup d’oeil à ConsoleZ qui permet, entre autre, de multiplier rapidement les shells.
Un prompt, pourquoi faire
Le prompt powershell est défini par la function prompt (ça tombe plutot bien). Par défaut, le prompt indique PS ainsi que le chemin actif, la fonction prompt est invoqué à chaque affichage de celui-ci..
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
Et donc ?
Le prompt étant customisable nous pouvons donc lui faire afficher ce que nous voulons :
La machine sur laquel je suis
Le compte que j’utilise
Le chemin courant
Le PID du processus Powershell
…
Commençons notre voyage !
un peu de couleur
Nous pouvons facilement décider de nous debarrasser du prompt gris clair et utiliser une autre couleur comme le bleu.
function prompt {
Write-Host "PS " -NoNewLine -foregroundcolor Blue
Write-Host (Get-location).Path -NoNewLine
Write-Host '>' -NoNewLine -foregroundcolor Blue
return " "
}
est ce que le shell est élévé ?
Comme dans le mode Unix, c’est pas mal de savoir si le shell actuel est simple ou mode élévé (root), du coup, suivant l’élévation, nous pouvons changer le > par un #
function prompt {
Write-Host "PS " -NoNewLine -foregroundcolor Blue
Write-Host (Get-location).Path -NoNewLine
$elevated = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator)
if($elevated){
Write-Host '#' -NoNewLine -foregroundcolor Blue
}else{
Write-Host '>' -NoNewLine -foregroundcolor Blue
}
return " "
}
En attendant de finir le post, voici, la fonction de prompt que j’utilise actuellement.
############################################################
# FUNCTION: Prompt #
############################################################
# Redefine Prompt setting new color for new sessions #
# and setting username, servername, PID and WS as title #
# TODO: Customisable prompt FIXED/ #
# TODO: Add Domain Information #D DONE/ #
############################################################
function Prompt {
<#
.Description
Customize your prompt s
Supported Format
#S
Shell, ie PS
#P
Current Path
#L
Leaf of the current path
#E
Powershell classique end '>'
#C
Custom entry :
Display content set after :
$Var => Standfor content of variable $Var
&Var => Standfor content of scriptblock in $Var
String string to display
example #C:[
#A
$ if user ou # if admin
#U
USerName
#M
MachineName
#I
Process ID
#V
Powershell Version
#W
WorkingSet Size
#G
Git Status
~Color
each TAG can be colorised by appending ~ColorNAme
#>
if (-not($Prompt)) {
$LocalPrompt = "#S #P#E"
} else {
$LocalPrompt = $Prompt
}
$items = $LocalPrompt.split('#')
$items | & {
process {
if (-not($_)) {
} elseif ($_ -match '([SPLECAUMIWDVG]):?([^~ ]*)~?([^ ]*)') {
$splat = @{}
if ($matches[3]) {
$splat['ForegroundColor'] = $matches[3]
}
if ($matches[1] -eq 'S') {
$splat['Object'] = $_.Replace($matches[0], "PS")
} elseif ($matches[1] -eq 'E') {
$splat['Object'] = $_.Replace($matches[0], ">")
} elseif ($matches[1] -eq 'P') {
$splat['Object'] = $_.Replace($matches[0], (Get-Location).Path)
} elseif ($matches[1] -eq 'C') {
if ($matches[2].StartsWith('$')) {
$var = Get-Variable -Name $matches[2].substring(1)
$splat['Object'] = $_.Replace($matches[0], $var )
} elseif ($matches[2].StartsWith('&') ) {
$var = & $(Get-Variable -Name $matches[2].substring(1)).Value
$splat['Object'] = $_.Replace($matches[0], $var )
} else {
$splat['Object'] = $_.Replace($matches[0], $matches[2])
}
} elseif ($matches[1] -eq 'A') {
$elevated = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ($elevated) {
$splat['Object'] = $_.Replace($matches[0], '#')
} else {
$splat['Object'] = $_.Replace($matches[0], '$')
}
} elseif ($matches[1] -eq 'U') {
$splat['Object'] = $_.Replace($matches[0], $env:username.trim())
} elseif ($matches[1] -eq 'M') {
$splat['Object'] = $_.Replace($matches[0], $env:COMPUTERNAME.trim())
} elseif ($matches[1] -eq 'I') {
$splat['Object'] = $_.Replace($matches[0], $PID)
} elseif ($matches[1] -eq 'L') {
$splat['Object'] = $_.Replace($matches[0], $(Split-Path -Path (Get-Location).Path -Leaf ))
} elseif ($matches[1] -eq 'W') {
$splat['Object'] = $_.Replace($matches[0], $((Get-Process -pid $PID).WS / 1Mb))
} elseif ($matches[1] -eq 'D') {
$splat['Object'] = $_.Replace($matches[0], $env:USERDOMAIN.Trim() )
} elseif ($matches[1] -eq 'V') {
$splat['Object'] = $_.Replace($matches[0], $psversiontable.PSVersion.Major )
} elseif ($matches[1] -eq 'G') {
$GitStatus = Get-GitStatus
$Branch = ""
if ($GitStatus) {
$Branch = " $($GitStatus.branch)"
if ($GitStatus.HasUntracked) {
$Branch += "*"
}
if ($GitStatus.AheadBy -gt 0 ) {
$Branch += ">$($GitStatus.AheadBy)"
} elseif ($GitStatus.BehindBy -gt 0) {
$Branch += "<$($GitStatus.BehindBy)"
}
}
$splat['Object'] = $_.Replace($matches[0], "$($Branch)" )
}
Write-Host -NoNewline @splat
}
}
}
if ($Title) {
$items = $Title.split('#')
$items = $items | & {
process {
if (-not($_)) {
} elseif ($_ -match '([SPLECAUMIWDV]):?([^~ ]*)~?([^ ]*)') {
if ($matches[1] -eq 'S') {
$_.Replace($matches[0], "PowerShell")
} elseif ($matches[1] -eq 'E') {
$_.Replace($matches[0], ">")
} elseif ($matches[1] -eq 'P') {
$_.Replace($matches[0], (Get-Location).Path)
} elseif ($matches[1] -eq 'C') {
if ($matches[2].StartsWith('$')) {
$var = Get-Variable -Name $matches[2].substring(1)
$_.Replace($matches[0], $var )
} elseif ($matches[2].StartsWith('&') ) {
$var = & $(Get-Variable -Name $matches[2].substring(1)).Value
$_.Replace($matches[0], $var )
} else {
$_.Replace($matches[0], $matches[2])
}
} elseif ($matches[1] -eq 'A') {
$elevated = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ($elevated) {
$_.Replace($matches[0], '#')
} else {
$_.Replace($matches[0], '$')
}
} elseif ($matches[1] -eq 'U') {
$_.Replace($matches[0], $env:username.trim())
} elseif ($matches[1] -eq 'M') {
$_.Replace($matches[0], $env:COMPUTERNAME.trim())
} elseif ($matches[1] -eq 'I') {
$_.Replace($matches[0], $PID)
} elseif ($matches[1] -eq 'L') {
$_.Replace($matches[0], $(Split-Path -Path (Get-Location).Path -Leaf ))
} elseif ($matches[1] -eq 'W') {
$_.Replace($matches[0], $((Get-Process -pid $PID).WS / 1Mb))
} elseif ($matches[1] -eq 'D') {
$_.replace($matches[1], $env:userdomain.trim())
} elseif ($matches[1] -eq 'V') {
$_.Replace($matches[0], $psversiontable.PSVersion.Major )
} elseif ($matches[1] -eq 'G') {
$GitStatus = Get-GitStatus
$Branch = ""
if ($GitStatus) {
$Branch = " $($GitStatus.branch)"
if ($GitStatus.HasUntracked) {
$Branch += "*"
}
if ($GitStatus.AheadBy -gt 0 ) {
$Branch += ">$($GitStatus.AheadBy)"
} elseif ($GitStatus.BehindBy -gt 0) {
$Branch += "<$($GitStatus.BehindBy)"
}
}
}
}
}
}
$host.ui.RawUI.WindowTitle = $items -join ''
}
Return " "
}
Did you like this article? Share it with your friends!
Leave a Reply