Wow. Do you all love PowerShell prompts or what? My prompt to display up/down information was very popular. How about a few more? As I mentioned in my previous post, performance is super critical when it comes to a PowerShell prompt function. I've experimented with a number of different techniques and I think using a runspace and synchronized hashtable is the best way to go. With that in mind here are a few more PowerShell prompt functions.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Show Free Space
First up is a prompt that displays the percent of free disk space for the C: drive on one or more remote computers. I'm using Get-Volume to retrieve this information. In my previous function, I had the computer names hard-coded. In this version, I put them in a text file in my Scripts folder. In the Do loop, I get the list, filtering out blank lines and trimming up spaces.
$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 = '??' }
Now I can dynamically change the prompt by modifying the text file. When the prompt is first loaded, the hashtable doesn't have values so I display 'working'. Once there is data the prompt reflects the percent free space.
Because I'm assuming disk space doesn't change rapidly, the loop in the runspace only runs once a minute. As with my other prompt functions, this code is on GitHub.
Show Free Memory
But, wait there's more! How about a variation to display percent free memory? This version also uses my list of computers. But to make it more efficient, I create PSSessions for each computer. This allows me to run Invoke-Command to get information from the Win32_OperatingSystem class via Get-CimInstance in a more efficient manner. The problem I ran into was accounting for servers going down or becoming unavailable. In my function, I remove broken PSSessions and create new sessions for computers on the list that don't already have an open session. What I haven't accounted for is removing a computer from the list and having the session be removed in the prompt. You can add that code if you want. Alternatively, simply update the list, and delete the $rshash variable. Or restart your PowerShell session.
The function can be found online here.
I have one more comprehensive telemetry prompt I'm working on. I love pushing the boundaries and this is definitely extreme. Thanks for your enthusiasm. Enjoy.
2 thoughts on “More PowerShell Monitoring Prompts”
Comments are closed.