個人檔案Musings of a PC相片部落格清單 工具 說明
7月21日

My next self-tutorial on PowerShell

After 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:
  • I've been trying to think of what to actually use PS for, and how to learn how to program in it. One of the ideas that has been going through my mind is taking some of the Script Centre scripts and re-writing them in PS.
  • A recent blog from Jeffrey Snover on setting the console title to be the current working directory. I'm glad he blogged about this 'cos I saw his demos at Tech-Ed and I'd been wondering how he had done that neat trick.
  • A link from Jeffrey's blog entry to a blog entry on the PowerShell for Fun site that goes into more detail on what you can do with setting the prompt. That entry ends with some suggestions on what the reader can do next ...

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
{
   $charge = get-wmiobject Win32_Battery -property EstimatedChargeRemaining
   $colour = "White"
   if ($charge.EstimatedChargeRemaining -lt 50)
   {
      $colour = "Yellow"
   }
   if ($charge.EstimatedChargeRemaining -lt 25)
   {
      $colour = "Red"
   }
   Write-Host ("PS " + $(get-location) + ">") -nonewline -foregroundcolor $colour
   return " "
}

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.
At line:1 char:2
+ .  <<<< profile.ps1

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)

請稍候...
很抱歉,您輸入的回應過長。請縮短您的回應。
您尚未輸入內容,請再試一次。
很抱歉,目前無法新增您的回應,請稍後再試。
若要新增回應,您的父母必須先給您權限。要求權限
您的家長已關閉回應功能。
很抱歉,目前無法刪除您的回應,請稍後再試。
您已超過每日回應上限次數,請於 24 小時後再試一次。
由於系統顯示您可能傳送垃圾郵件給其他使用者,因此您帳號中的回應功能已遭停用。 如果您認為自己帳號遭錯誤停用,請連絡 Windows Live 支援
請完成下列安全檢查,以完成回應。
您輸入的安全檢查字元必須與圖片或音訊中的字元相符。

若要新增回應,請以您的 Windows Live ID 登入 (若您使用 Hotmail、Messenger 或 Xbox LIVE,則您已擁有 Windows Live ID)。登入


沒有 Windows Live ID?註冊

沒有名稱撰寫:
http://www.goodlaptopbattery.co.uk/hp/2230s.htm hp 2230s battery
http://www.goodlaptopbattery.co.uk/mitac/bp-8050.htm Mitac bp-8050 Laptop Battery
http://www.goodlaptopbattery.co.uk/compaq/nc6000.htm Compaq nc6000 battery
http://www.goodlaptopbattery.co.uk/acer/as07b31.htm Acer as07b31 battery
http://www.goodlaptopbattery.co.uk/acer/as07b32.htm Acer as07b32 battery
http://www.goodlaptopbattery.co.uk/acer/as07b41.htm Acer as07b41 battery
http://www.goodlaptopbattery.co.uk/acer/as07b42.htm Acer as07b42 battery
http://www.goodlaptopbattery.co.uk/acer/aspire-5520.htm Acer aspire 5520 battery
http://www.goodlaptopbattery.co.uk/acer/aspire-5920.htm Acer aspire 5920 battery
http://www.goodlaptopbattery.co.uk/acer/aspire-9300.htm Acer aspire 9300 battery
http://www.goodlaptopbattery.co.uk/acer/batbl50l6.htm Acer batbl50l6 battery
http://www.goodlaptopbattery.co.uk/apple/a1185-black.htm Apple a1185 black battery
http://www.goodlaptopbattery.co.uk/apple/a1185-white.htm Apple a1185 white battery
http://www.goodlaptopbattery.co.uk/dell/inspiron-1520.htm Dell inspiron 1520 battery
http://www.goodlaptopbattery.co.uk/dell/inspiron-6400.htm Dell inspiron 6400 battery
http://www.goodlaptopbattery.co.uk/dell/latitude-d620.htm Dell latitude d620 battery
http://www.goodlaptopbattery.co.uk/dell/latitude-d630.htm Dell latitude d630 battery
http://www.goodlaptopbattery.co.uk/dell/latitude-d820.htm Dell latitude d820 battery
http://www.goodlaptopbattery.co.uk/dell/latitude-d830.htm Dell latitude d830 battery
http://www.goodlaptopbattery.co.uk/dell/inspiron-9400.htm Dell inspiron 9400 battery
http://www.goodlaptopbattery.co.uk/dell/vostro-1500.htm Dell vostro 1500 battery
http://www.goodlaptopbattery.co.uk/dell/vostro-1700.htm Dell vostro 1700 battery
http://www.goodlaptopbattery.co.uk/hp/530.htm Hp 530 battery
http://www.goodlaptopbattery.co.uk/hp/dv1000.htm Hp dv1000 battery
23 小時前
list29撰寫:
http://www.adapterlist.com/dell/inspiron-9300.htm dell inspiron 9300 battery
http://www.adapterlist.com/hp/dv8000.htm hp dv8000 battery
http://www.adapterlist.com/hp/dv1000.htm hp dv1000 battery
http://www.adapterlist.com/hp/dv4000.htm hp dv4000 battery
http://www.adapterlist.com/hp/r3000.htm hp r3000 battery
http://www.adapterlist.com/dell/inspiron-mini-9.htm dell inspiron mini 9 battery
http://www.adapterlist.com/dell/inspiron-9400.htm dell inspiron 9400 battery
http://www.adapterlist.com/acer/aspire-5520.htm acer aspire 5520 battery
http://www.adapterlist.com/acer/aspire-5920.htm acer aspire 5920 battery
http://www.adapterlist.com/dell/inspiron-6400.htm dell inspiron 6400 battery
http://www.adapterlist.com/ibm/thinkpad-x60.htm ibm thinkpad x60 battery
http://www.adapterlist.com/ibm/thinkpad-x60s.htm ibm thinkpad x60s battery
http://www.adapterlist.com/hp/zt3000.htm hp zt3000 battery
http://www.adapterlist.com/hp/pavilion-zv5000.htm hp pavilion zv5000 battery
http://www.adapterlist.com/hp/pavilion-zv6000.htm hp pavilion zv6000 battery
http://www.adapterlist.com/hp/pavilion-zd8000.htm hp pavilion zd8000 battery
http://www.adapterlist.com/hp/pavilion-zx5000.htm hp pavilion zx5000 battery
http://www.adapterlist.com/hp/presario-v4000.htm hp presario v4000 battery
http://www.adapterlist.com/hp/presario-v2000.htm hp presario v2000 battery
http://www.adapterlist.com/hp/presario-m2000.htm hp presario m2000 battery
http://www.adapterlist.com/compaq/presario-v4000.htm compaq presario v4000 battery
http://www.adapterlist.com/compaq/presario-v2000.htm compaq presario v2000 battery
http://www.adapterlist.com/compaq/presario-v3000.htm compaq presario v3000 battery
http://www.adapterlist.com/compaq/presario-v6000.htm compaq presario v6000 battery
http://www.adapterlist.com/compaq/presario-c700.htm compaq presario c700 battery
http://www.adapterlist.com/dell/fk890.htm dell fk890 battery
http://www.adapterlist.com/toshiba/pa3593u-1bas.htm toshiba pa3593u-1bas battery
http://www.adapterlist.com/hp/m2000.htm hp m2000 battery
http://www.adapterlist.com/hp/338794-001.htm hp 338794-001 battery
http://www.adapterlist.com/hp/395789-001.htm hp 395789-001 battery
http://www.adapterlist.com/compaq/116314-001.htm compaq 116314-001 battery
http://www.adapterlist.com/toshiba/satellite-a70.htm toshiba satellite a70 battery
http://www.adapterlist.com/compaq/319411-001.htm compaq 319411-001 battery
http://www.adapterlist.com/hp/346970-001.htm hp 346970-001 battery
http://www.adapterlist.com/hp/367759-001.htm hp 367759-001 battery
http://www.adapterlist.com/hp/411462-421.htm hp 411462-421 battery
http://www.adapterlist.com/hp/ev088aa.htm hp ev088aa battery
http://www.adapterlist.com/hp/ex941aa.htm hp ex941aa battery
http://www.adapterlist.com/laptop battery
http://www.adapterlist.com/laptop batteries
http://www.adapterlist.com/dell laptop batteries/
http://www.adapterlist.com/dell/wr050.htm dell wr050 battery
http://www.adapterlist.com/hp laptop batteries/
http://www.adapterlist.com/laptop ac adapter/
1 天前
沒有名稱撰寫:
Welcome to enter (wow gold) and (wow power leveling) trading site, (Rolex) are cheap, (World of Warcraft gold) credibility Very good! Quickly into the next single! Key words !
8 月 17 日
沒有名稱撰寫:
Welcome to enter (wow gold) and (wow power leveling) trading site, (wow gold) are cheap, (wow power leveling) credibility Very good! Quickly into the next single! Key words directly to the website click on transactions!
8 月 16 日
沒有名稱撰寫:
telefon tarife karşılaştırma en uygun tarife en iyi tarife havalandırma gizli kamera izolasyon çelik baca paslanmaz çelik baca doğalgaz bacası indir Sohbet iso 9001 iso 14001 Yangın Söndürme yangın söndürme cihazları yangın dolapları yangın tüpü izalasyon ısıtma soğutma isitma sogutma Aspirator Aspiratör Vantilatör sohbetim turizm işletme belgesi turizm belgesi turizm yatırım belgesi Chat sohbet odası sohbet sitesi türkiye sohbet tr sohbet tüm türkiye sohbet arkadaş sohbet türkiye sohpet kızlarla sohbet kızlarla sohpet muhabbet muhappet kızlarla çet çet Gizli Kamera türkiye çet çet sohpet mırç mirç türkiye mirc mirc muhabbet Sohbet Sitesi Chat Sohpet Yangın yangın güvenlik yangın söndürme sistemleri yangın tüpü dolum yangın merdiveni yangın çıkış kapısı  Hava Soğutma Hücreli Aspiratörler Fanlar Radyal Körükler Toz Toplama Soğutma Kulesi Klima Santraller Malzeme Nakil Vantilatörleri iso 14001 iso 14001 iso 22000 iso 22000 haccp belgesi haccp belgesi ikamet tezkeresi yabancı çalışma izni yabancı personel çalışma izni yabancı çalışma izni yabancı personel çalışma izni ohsas 18001 ohsas 18001 iso belgesi iso 9001 belgesi ohsas belgesi ISO 9001 Teşvik Belgesi Çocuk Bezi Hasta Bezi Makyaj Malzemeleri Makyaj Temizleme Mendili Kişisel Bakım kolonyalı mendil Islak mendil Dudak Koruyucu Temizlik Ürünleri Göz Kalemi Diyet Ürünleri Süper Site driver Güvenlik Kamerası Islak Mendil Kolonyalı Mendil Kolonyalı Mendil JoyTurk driver ara web tasarım Güvenlik Kamerası paketleme Kamera Kamera Kurulum Tatil Tatil Köyleri Turk tourizm Turkish tourizm Turk holiday Turkish holiday Turkish travels Turk Travels Tatil Yerleri Tatil Beldeleri Perde Perde Modelleri Kamera Epilasyon Emlak Tatil Video
5 月 12 日
匿名 的圖片
4 月 10 日
匿名 的圖片
DBMwS 撰寫:
Since I don't have any machines available that uses batteries,
I am not sure if the following line would cause too much time to execute or not
> get-wmiobject Win32_Battery -property EstimatedChargeRemaining
 
I wonder if people are willing to sacrifice to wait for their prompt to take long though.
But this seems likea  great idea for those laptop users ;)
7 月 21 日

引用通告

引述這則內容的部落格