Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active January 15, 2020 20:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdhitsolutions/d11e1598795eb892b1271b25792f8ff4 to your computer and use it in GitHub Desktop.
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).
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)) "
}
@jdhitsolutions
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment