Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

More PowerShell Monitoring Prompts

Posted on December 13, 2018December 13, 2018

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.

server free space PowerShell prompt

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.

%free memory PowerShell prompt

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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “More PowerShell Monitoring Prompts”

  1. Pingback: PowerShell Schnipseljagd 50/18 | | PowerShell Usergroup Austria
  2. Pingback: My Telemetry PowerShell Prompt Function • The Lonely Administrator

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d