Created
December 13, 2018 15:19
-
-
Save jdhitsolutions/d2875c800fdca62e59ee8d08326947bf to your computer and use it in GitHub Desktop.
A PowerShell prompt function to display free disk space from a list of remote servers.
This file contains hidden or 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( { | |
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()} | |
$results = $computers | ForEach-Object { | |
try { | |
$c = Get-Volume -DriveLetter c -CimSession $_ -ErrorAction Stop | | |
Add-Member -MemberType ScriptProperty -Name PctFree -Value {($($this.SizeRemaining) / $($this.size)) * 100 -as [int]} -Force -passthru | |
$val = $c.PctFree | |
} | |
Catch { | |
$val = '??' | |
} | |
[pscustomobject]@{ | |
Computername = $_.toupper() | |
PctFree = $val | |
} | |
} | |
$global:rsHash.results = $results | |
$global:rsHash.date = Get-Date | |
#set a sleep interval between tests | |
Start-Sleep -Seconds 60 | |
} while ($True) | |
}) | |
$pscmd.runspace = $newrunspace | |
[void]$psCmd.BeginInvoke() | |
} #catch | |
Write-Host "[" -NoNewline | |
if ($global:rshash.results) { | |
$global:rshash.results | Sort-Object -Property Computername | | |
ForEach-Object { | |
if ($_.pctfree -eq '??') { | |
$fg = "magenta" | |
} | |
elseif ($_.pctfree -le 20) { | |
$fg = "red" | |
} | |
elseif ($_.pctfree -le 50) { | |
$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/