Probably of use to others, I dunno, but one thing I always want to know is what all Graph, Exchange Online, or PnP PowerShell my current PowerShell session is connected to. Coz if it’s already connected I don’t need to fuss about and reconnect.
I guess there’s some way of adding it to fancy prompt engines like oh-my-posh, but all I wanted was a cmdlet I could run to view this. So I wrote up:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
function Get-MyConnectionInformation { $snippetsHash = @{ "Graph" = @{ "Status" = "" "Snippet" = "" "CombinedSnippet" = "" } "Exchange Online" = @{ "Status" = "" "Snippet" = "" "CombinedSnippet" = "" } "PnP PowerShell" = @{ "Status" = "" "Snippet" = "" "CombinedSnippet" = "" } } if (Get-MgContext) { $snippetsHash.Graph.Snippet = "Connected to $((Get-MgContext).AppName)" $snippetsHash.Graph.Status = $true } else { $snippetsHash.Graph.Snippet = "Disconnected" $snippetsHash.Graph.Status = $false } if (Get-ConnectionInformation) { $snippetsHash."Exchange Online".Snippet = "Connected to $((Get-ConnectionInformation).Organization)" $snippetsHash."Exchange Online".Status = $true } else { $snippetsHash."Exchange Online".Snippet = "Disconnected" $snippetsHash."Exchange Online".Status = $false } try { $pnpTemp = Get-PnpConnection -ErrorAction Stop } catch {} if ($pnpTemp) { $snippetsHash."PnP PowerShell".Snippet = "Connected to $($pnpTemp.Url)" $snippetsHash."PnP PowerShell".Status = $true } else { $snippetsHash."PnP PowerShell".Snippet = "Disconnected" $snippetsHash."PnP PowerShell".Status = $false } # An array holding all the snippets $allSnippets = @() # For each entity create the combined snippet (so I can find the one with the longest length) and also add it to the above array foreach ($key in $snippetsHash.Keys) { $snippetsHash.$key.CombinedSnippet = $key + "|" + $snippetsHash.$key."Snippet" $allSnippets += $snippetsHash.$key.CombinedSnippet } # Find the longest snippet $longestSnippet = $allSnippets | Sort-Object -Property Length -Descending | Select-Object -First 1 # For each entity now do the actual outputting, taking into account the longest one and adding dots in between foreach ($key in $snippetsHash.Keys) { $snippet = $snippetsHash.$key."CombinedSnippet" if ($snippet -eq $longestSnippet) { $numDots = 5 } else { $numDots = $longestSnippet.Length - $snippet.Length + 5 } # Parts of the final output $snippet1 = $key $snippet2 = $snippetsHash.$key.Snippet Write-Host -NoNewline $snippet1 $counter = 0 do { Write-Host -NoNewline "." $counter ++ } while ($counter -lt $numDots) if ($snippetsHash.$key.Status) { Write-Host -ForegroundColor Green $snippet2 } else { Write-Host -ForegroundColor Red $snippet2 } } } |
It’s more complicated than it needs to be, but that’s because I wanted to get output like this:
Or:
Neat, eh!