| 個人檔案Musings of a PC相片部落格清單 | 說明 |
|
7月21日 My next self-tutorial on PowerShellAfter a big, long break from blogging on PowerShell, I finally found the time and the incentive to do some more learning on how to write in PowerShell. The incentive was a combination of three things:
So where did that leave me? I ended up wanting to write a prompt function that would warn me if the battery was running low. So, over to Script Centre first to see if I can find an existing script on getting battery information. Yep, there are a couple of scripts there, of which one (http://www.microsoft.com/technet/scriptcenter/scripts/hardware/power/hwpwvb01.mspx) deals with the Win32_Battery object. I tried running that script on my laptop and it didn't really give me much information that might have been useful. I checked MSDN about Win32_Battery but that didn't really get me advanced on what I was trying to achieve. What I did find, though, was an article on using WMI with PowerShell, written by Andrew Barr at Microsoft: http://www.proudlyserving.com/archives/2005/08/monad_and_wmi.html This helps a lot. If you type get-wmiobject Win32_Battery then you get all of the properties that WMI can return from this. Looking through the list, I spotted EstimatedChargeRemaining ... aha! I could write a prompt function that retrieves the estimated charge remaining and sets the prompt to be a different colour as the charge percentage goes down ... function prompt So if the battery charge drops below 50%, the prompt text should turn yellow. If it continues to fall and goes below 25%, it should go red. A couple of things that caught me out briefly ... 1. You must return something from the prompt function. In the example above, I'm returning a space. To begin with, I returned "" (i.e. an empty string) but PowerShell then issues the default prompt! 2. Being English, I accidentally started off trying to specify -foregroundcolour (i.e. with a u) ... and then wondered why -foregroundcolour White was being issued as part of my prompt! The PowerShell for Fun blog entry linked to above has a brief but useful reminder as to where PowerShell will gets its config files from when it starts up. If you want this function to be persistent, you need to add it to one of those files. The one complication here is that if you've only just started to use PowerShell, the script execution policy may be set to restricted. If that is the case then, when you start PowerShell having just added the prompt function to one of the startup files, you may see this error: The file profile.ps1 cannot be loaded. The execution of scripts is disabled on this system. Please see "get-help about_signing" for more details. If you type get-executionpolicy, you can find out what your machine is set to. Mine was set to restricted. I wanted to change it to RemoteSigned (so that local scripts can be run unsigned but downloaded ones must be signed) but my first attempt failed because I don't have admin rights so the write to the registry failed. Running PowerShell as an admin account and then typing set-executionpolicy RemoteSigned then allowed my format prompt to be set. 回應 (7)
引用通告引述這則內容的部落格
|
|
|