/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" PowerShell Adding -verbose switch to functions "," PowerShell and Active Directory Part 9 (nested gro... "," Powershell and hey Scripting guy ! "," PowerShell Orphan share remover Tool (Update) "," PowerShell and Active Directory Part 8 (ACL's) "," Signing PowerShell Scripts "," PowerShell and Active Directory Part 7 "," PowerShell and Active Directory Part 6 "," Windows PowerShell Video: Next Generation Command ... "," PowerShell and Active Directory Part 5 "," ")

Wednesday, August 30, 2006

 


PowerShell, Has my Dell a dangerous battery ?



Some Dell Batteries can become to hot  see : Dell Laptop Explodes in Flames - Gizmodo

Dell has started a Recall of Notebook Computer Batteries,CPSC, Dell Announce Recall of Notebook Computer Batteries.

Not all Dell Batteries are Affected, so  I made a PowerShell script to get the Battery Model and check it against the list of batteries so that you can check your battery easy. For more information and pointers to instructions on how to inspect your battery, and how to begin the battery exchange process, see Dell's Battery Return Program page.

I also will use this example, to show again how to start different kinds of examples in PowerShell as there are a couple of different ways to make and start the examples, making it very flexible, but for beginning users in PowerShell it is not always that clear how to start the examples on my blog, I noticed from some comments and questions, I mentioned this before a couple of times before on my blog e.g. in PowerShell and Active Directory Part 8 (ACL's), but I will use this example to show some of the different forms again and how to use them as I think it is an important issue and of course want everyone to be able to start and use the examples I provide on my blog ;-)

*Edit* in the next post Powershell, Has my Dell a dangerous battery Part 2 ?  you find an example that gets te serials from the Dell site directly, also I have a new list of serials with some changes ! so check out the next post also !

The first Example you can just paste onto your Console

 

# Lines of code that can just be pasted onto the PowerShell Console

$AffectedSerials = "3K590","59474","6P922"
  "C2603","C5339","C5340",  "C5446","C6269"
  "C6270",  "D2961","D5555","D6024","D6025"
  "F2100","F5132","GD785","H3191","JD616"
  "JD617","KD494","M3006","RD857","TD349"
  "U5867","U5882","W5915","X5308","X5329"
  "X5332","X5333","X5875","X5877","Y1333"
  "Y4500","Y5466" 

get-WmiObject win32_battery | Foreach { 
  $Name = $_.name 
  write-host "Battery Model : $name"
  $AffectedSerials | Where {$name -match $_} | Foreach { 
      Write-Warning "Affected : Battery $Name matches : *$_*" 
  } 
}
 
 
Ok  Lets see how that does look,Let's past in the first line
 

PoSH>$AffectedSerials = "3K590","59474","6P922",
>> "C2603","C5339","C5340", "C5446","C6269",
>> "C6270", "D2961","D5555","D6024","D6025",
>> "F2100","F5132","GD785","H3191","JD616",
>> "JD617","KD494","M3006","RD857","TD349",
>> "U5867","U5882","W5915","X5308","X5329",
>> "X5332","X5333","X5875","X5877","Y1333",
>> "Y4500","Y5466"
>>

this is also the way I created the list from the Dell website and pasted it in from notepad , next to that it started with this oneliner I created interactively :

Posh>gwmi win32_battery |% {$AffectedSerials |% {$name -match $_}}

 

As you could see from the first line, filling the Affected serials variable, PowerShell detects that a line is not ready when you past it in and will continue the  line (showing >>)

So I copied that line into notepad did format it more lines, after that I cleaned out the code a bit, while pasting to code back into the console to check until I liked the result

I really love to work on the console by pasting in and out of the console like this while testing as its really quick as you use quickedit !! the console works great see also : Commandline editing with MSH.

 (as my VM has not battery and my base OS on my laptop runs still MSH you see the prompt change ;-)),

 

Posh>gwmi win32_battery |% {$AffectedSerials |% {$name -match $_}}

MSH>$AffectedSerials = "3K590","59474","6P922",
>> "C2603","C5339","C5340", "C5446","C6269",
>> "C6270", "D2961","D5555","D6024","D6025",
>> "F2100","F5132","GD785","H3191","JD616",
>> "JD617","KD494","M3006","RD857","TD349",
>> "U5867","U5882","W5915","X5308","X5329",
>> "X5332","X5333","X5875","X5877","Y1333",
>> "Y4500","Y5466"
>>
MSH>get-WmiObject win32_battery | Foreach {
>> $Name = $_.name
>> write-host "Battery Model : $name"
>> $AffectedSerials | Where {$name -match $_} | Foreach {
>> Write-Warning "Affected : Battery $Name matches : *$_*"
>> }
>>
>> }
>>
Battery Model : DELL 4M0105

Note that with this method all is loaded in the $global (interactive) scope, so that I can also interact with the variables in between, in this case I use this to add part of my found serial to the $Affected serials HashTable to test the Warning  :

 

# I add part of my model to the array with serials to check, to show the output with a affected battery

MSH>$affectedSerials += "4M010"
MSH>get-WmiObject win32_battery | Foreach {
>> $Name = $_.name
>> write-host "Battery Model : $name"
>> $AffectedSerials | Where {$name -match $_} | Foreach {
>> Write-Warning "Affected : Battery $Name matches : *$_*"
>> }
>> }
>>
Battery Model : DELL 4M0105
WARNING: Affected : Battery DELL 4M0105 matches : *4M010*

You can see that I do not have to paste the HashTable back in as I have it still loaded in my $global Scope, and I can just change it and past in the second line again, and now it will show the error as it will match against my battery.

This method is very handy while working interactively developing or testing as you can test parts of the script just by pasting them in, as I work like this almost always you can do this with almost every example on my blog.

Another version you will find often on my blog is a function,

# check-Battery function

function Check-Battery ($computer = '.') {

  $AffectedSerials = "3K590","59474","6P922"
    "C2603","C5339","C5340",  "C5446","C6269"
    "C6270",  "D2961","D5555","D6024","D6025"
    "F2100","F5132","GD785","H3191","JD616"
    "JD617","KD494","M3006","RD857","TD349"
    "U5867","U5882","W5915","X5308","X5329"
    "X5332","X5333","X5875","X5877","Y1333"
    "Y4500","Y5466" 

  get-WmiObject win32_battery -computer $computer | Foreach { 
    $Name = $_.name 
    write-host "Battery Model : $name"
    $AffectedSerials | Where {$name -match $_} | Foreach { 
        Write-Warning "Affected : Battery $Name matches : *$_*" 
    } 
  }
}

you can also use those examples just by pasting them onto the Console, but after that you need to start them

MSH># check-Battery function
MSH>
MSH>function Check-Battery ($computer = '.') {
>>
>> $AffectedSerials = "3K590","59474","6P922",
>> "C2603","C5339","C5340", "C5446","C6269",
>> "C6270", "D2961","D5555","D6024","D6025",
>> "F2100","F5132","GD785","H3191","JD616",
>> "JD617","KD494","M3006","RD857","TD349",
>> "U5867","U5882","W5915","X5308","X5329",
>> "X5332","X5333","X5875","X5877","Y1333",
>> "Y4500","Y5466"
>>
>> get-WmiObject win32_battery -computer $computer | Foreach {
>> $Name = $_.name
>> write-host "Battery Model : $name"
>> $AffectedSerials | Where {$name -match $_} | Foreach {
>> Write-Warning "Affected : Battery $Name matches : *$_*"
>> }
>> }
>> }
>>
MSH>Check-Battery
Battery Model : DELL 4M0105
MSH>Check-Battery foo
get-WMIObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:10 char:16
+ get-WmiObject <<<< win32_battery -computer $computer | Foreach {

MSH>$AffectedSerials -contains '4M010'
True

Note that as I declare the $AffectedSerials in the function, my modified $AffectedSerials is not used so my computer will not show up as affected, but my $global scope is not changed it still contains the added value as the function runs in his own scope.

A common misunderstanding is that an example like this is copied from my blog into notepad and then saved in a file.

as this is the declaration of a function this function gets loaded not executed, also after you run the script you can not use the function if you want to load it in the interactive ($global) scope to keep it handy you need to start the script with a dot (called Dot Sourcing to be able to use it after running the script) as with the function

some examples of this:

 PowerShell out-DataGrid update and more Dataset utilities 

working with CSV files in MSH (part one)

this way of loading functions is great for use in a profile see also :

Tuning MSH in your Profile

 

I hope this shows a bit how easy it is to test by pasting code into the console, how to work with the different examples and how the different ways of starting code differ. in later posts more about how to work with scripts.

If you still have problems starting one of my examples please let me know.

Enjoy,

Greetings, /\/\o\/\/
Tags : Monad msh PowerShell


Comments:
Anonymous Anonymous
Another great blog MOW.
Thanks!

Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
 
Anonymous Anonymous
You can also generate the list of bad batteries with the following:

$bat = $wc.downloadstring("https://www.dellbatteryprogram.com/")
$r = [regex]" ([A-Z0-9][A-Z0-9][0-9][0-9][0-9])"
$matches = $r.matches($bat)
$badbatteries = $matches |%{ $_.groups[1].captures[0].value }|sort

This relies, of course, on Dell never changing the format of the html.
 
Blogger /\/\o\/\/
Jim,

Thanks again and I made a new Post with you made appended to the script, the HTML-code for a nonbreaking space did stay in that version :

http://mow001.blogspot.com/2006/08/powershell-has-my-dell-dan_115705284548014997.html

Greetings /\/\o\/\/
 
Blogger Rouge Delimiter
here's a Powershell script to find files of certain type, say *.mp3 in all the shared folders on all the machines in current domain. Do let me know it can be implemented in any way better.

http://rougedelimiter.blogspot.com/2006/12/powershell-network-scan-script.html
 
Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?