PowerShell Customization
Finally I was able to take some time to “pimp” my powershell prompt, I had took the time to customize my zsh prompt both in my mac and pc (using WSL) in order to get Powerline9k.
Anyways, I followed this guide which I stumbled upon on Twitter; the poster showed it running on Windows Terminal, I have to settle on my current config of ConEmu given that I have not installed Windows May Update (1903)
The guide is pretty straight forward, some variations I had to make:
- Make sure you install the latest version of PowerShellGet, or at least a version that supports the flag
-AllowPrerelease. This is needed to get the version required of the posh-git Module. - Install posh-git with
-AllowPrerelease, I forgot to do this an not all the properties on the$GitPromptSettingswere available causing a few issues on the script. (Null refs). - The scripts for the VS Prompt provided by the author rely on having the sed binary on your system, which I did not have and did not want to instal Cygwin either, therefore I had to make a small adjustment to the script in order to use
Get-Content, the change looks as follow:
if (Test-Path ~/.azure/clouds.config) {
#$currentSub = & sed -nr "/^\[AzureCloud\]/ { :l /^subscription[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" ~/.azure/clouds.config
$subscriptionLine = ((Get-Content -Path ~/.azure/clouds.config) -Match "^subscription(\ =)")[0]
if ($null -ne $subscriptionLine) {
$currentSub = $subscriptionLine.Substring($subscriptionLine.Length - 36)
$currentAccount = (Get-Content ~/.azure/azureProfile.json | ConvertFrom-Json).subscriptions | Where-Object { $_.id -eq $currentSub }
if ($null -ne $currentAccount) {
Write-Host " " -NoNewLine
Write-Host " $([char]0xf0c2)$([char]0xfd03)" -NoNewLine -BackgroundColor DarkCyan -ForegroundColor Yellow
Write-Host " $($currentAccount.name) " -NoNewLine -BackgroundColor DarkCyan -ForegroundColor White
}
}
}
The substitution is the commented line
#currentSub = ...
with
$subscriptionLine = ((Get-Content -Path ~/.azure/clouds.config) -Match "^subscription(\ =)")[0]
$subscriptionLine ends up being an array of matching lines, which in our case is just one, hence accessing it by index 0, thence it is further parsed to extract the Subscription GUID.
The rest of the code is already checked in to my PowerShell Profile Repository.