Created
December 13, 2018 15:37
-
-
Save jdhitsolutions/e25b2748d9e2ca7ec0c4314bc82a04ff to your computer and use it in GitHub Desktop.
A PowerShell Prompt function to display free memory percentage from a list of remote servers.
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 { | |
Try { | |
Get-Variable -Name rsHash -Scope global -ErrorAction Stop | Out-Null | |
} | |
Catch { | |
#create the runspace and synchronized hashtable | |
$global:rsHash = [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("rsHash", $rsHash) | |
$pscmd = [PowerShell]::Create().AddScript( { | |
#define a pssession option to speed up connections | |
$opt = New-PSSessionOption -OpenTimeout 1000 -MaxConnectionRetryCount 1 | |
do { | |
#define the list of computers to test | |
#filter out blank lines and trim any spaces to clean up names. | |
$computers = Get-Content -Path c:\scripts\watch.txt | | |
Where-Object {$_ -match $_} | | |
Foreach-Object {$_.trim()} | |
#make sure there are sessions for all computers in the list | |
$computers | Where-Object {(get-pssession).where( {$_.state -eq 'opened'}).computername -notcontains $_} | | |
ForEach-Object { | |
New-PSSession -ComputerName $_ -SessionOption $opt | |
} | |
#remove broken sessions | |
Get-PSSession | Where-Object {$_.state -eq 'broken'} | Remove-PSSession | |
$data = Invoke-command -ScriptBlock { | |
Get-CimInstance -ClassName win32_operatingsystem -Property TotalVisibleMemorySize, FreePhysicalMemory | |
} -Session (Get-PSSession) | |
$results = $data | Sort-Object -Property PSComputername | | |
ForEach-Object { | |
[int]$val = ($_.FreePhysicalMemory / $_.TotalVisibleMemorySize) * 100 | |
[pscustomobject]@{ | |
Computername = $_.PSComputername.toUpper() | |
PctFree = $val | |
} | |
} | |
$global:rsHash.results = $results | |
$global:rsHash.date = Get-Date | |
#set a sleep interval between tests | |
Start-Sleep -Seconds 10 | |
} while ($True) | |
}) | |
$pscmd.runspace = $newrunspace | |
[void]$psCmd.BeginInvoke() | |
} #catch | |
Write-Host "FreeMem" | |
Write-Host "[" -NoNewline | |
if ($global:rshash.results) { | |
$global:rshash.results | Sort-Object -Property Computername | | |
ForEach-Object { | |
if ($_.pctfree -le 30) { | |
$fg = "red" | |
} | |
elseif ($_.pctfree -le 60) { | |
$fg = "yellow" | |
} | |
else { | |
$fg = "green" | |
} | |
Write-Host " $($_.computername) " -NoNewline | |
Write-Host "$($_.pctfree)%" -ForegroundColor $fg -NoNewline | |
} #foreach data item | |
} #if results | |
else { | |
Write-Host "working..." -NoNewline -ForegroundColor Cyan | |
} | |
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
Described at https://jdhitsolutions.com/blog/powershell/6285/more-powershell-monitoring-prompts/