Last active
January 15, 2020 20:08
-
-
Save jdhitsolutions/d11e1598795eb892b1271b25792f8ff4 to your computer and use it in GitHub Desktop.
A PowerShell prompt function using a synchronized hashtable and runspace to display server status (up/down).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function prompt { | |
$up = 0x25b2 -as [char] | |
$down = 0x25bc -as [char] | |
Try { | |
Get-Variable -Name testHash -Scope global -ErrorAction Stop | Out-Null | |
} | |
catch { | |
#create the runspace and synchronized hashtable | |
$global:testHash = [hashtable]::Synchronized(@{Computername = $env:computername;results = ""; date= (Get-Date)}) | |
$newRunspace = [runspacefactory]::CreateRunspace() | |
#set apartment state if available | |
if ($newRunspace.ApartmentState) { | |
$newRunspace.ApartmentState = "STA" | |
} | |
$newRunspace.ThreadOptions = "ReuseThread" | |
$newRunspace.Open() | |
$newRunspace.SessionStateProxy.SetVariable("testHash", $testHash) | |
$pscmd = [PowerShell]::Create().AddScript( { | |
#define the list of computers to test | |
$computers = "dom1", "srv1", "srv2", "srv3" | |
do { | |
$results = $computers | ForEach-Object { | |
[pscustomobject]@{ | |
Computername = $_.toupper() | |
Responding = Test-WSMan -ComputerName $_ | |
} | |
} | |
$global:testHash.results = $results | |
$global:testHash.date = Get-Date | |
#set a sleep interval between tests | |
Start-Sleep -Seconds 5 | |
} while ($True) | |
}) | |
$pscmd.runspace = $newrunspace | |
[void]$psCmd.BeginInvoke() | |
} #catch | |
Write-Host "[" -NoNewline | |
$global:testHash.results.foreach( { | |
Write-Host $_.Computername -NoNewline | |
if ($_.responding) { | |
Write-Host $up -ForegroundColor green -NoNewline | |
} | |
else { | |
Write-Host $down -ForegroundColor red -NoNewline | |
} | |
}) | |
Write-Host "]" -NoNewline | |
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function is described at https://jdhitsolutions.com/blog/powershell/6275/powershell-updown-prompt/