I’ve been working on a new, more condensed version of the OpsMgr PoSh library – one of the coolest features I think is the ‘location awareness’ which automatically switches your management server and such based on where you’re actually connected from.
Anyway, to do that I was embedding things in function prompt {}. In case you aren’t aware, if you define the prompt function, whatever is in there will run everytime you return to the prompt in the console. You can use this to your advantage for many things, such as this nifty little battery gauge that lets you know how much juice is left. It also has a configurable global variable, BatteryDisplayAtPercent, which you can set so it will hide until there is that much charge or less remaining.
First a screenshot, then the script:

|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# ============================================================================================== # # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2007 # # NAME: Battery-Prompt.ps1 # # AUTHOR: Jeremy D. Pavleck , Pavleck.NET # DATE : 12/14/2008 # # COMMENT: Lists the percentage of battery remaining above your prompt inside the console. # Difficulty level: OVER 9000!!!!!!!!!!!!!!!! # # ============================================================================================== $GLOBAL:BatteryDisplayAtPercent = 101 # When you should start displaying status # Anything over 100 means to show it all Function GLOBAL:Get-BattLevel($minLevel) { $charge = (Get-WmiObject -Class Win32_Battery).EstimatedChargeRemaining If(!$charge) {break} # Not on a laptop, or no battery, so exit Switch($charge) { {($charge -ge 101) -and ($minLevel -ge 101)} {Write-Host "Batt: CHARGING" -ForeGroundColor Green} {($charge -ge 80) -and ($charge -le 100) -and ($charge -le $minLevel)} {Write-Host "Batt: $charge%" -ForeGroundColor Green} {($charge -ge 40) -and ($charge -le 79) -and ($charge -le $minLevel)} {Write-Host "Batt: $charge%" -ForeGroundColor Yellow} {($charge -ge 16) -and ($charge -le 39) -and ($charge -le $minLevel)} {Write-Host "Batt: $charge%" -ForeGroundColor Magenta} {($charge -ge 1) -and ($charge -le 15) -and ($charge -le $minLevel)} {Write-Host "Batt: $charge%" -ForeGroundColor Red} default {break} } } function prompt() { Get-BattLevel $GLOBAL:BatteryDisplayAtPercent } |
