header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Subject: Get the BIOS version
Prev Next
You are not authorized to post a reply.

Author Messages
santonsenUser is Offline
New Member
New Member
Posts:2

02/18/2008 2:56 AM  

This scipt will get the list of computers you want to check, ping for availability, then produce a text file with your results.  It also produces a file of computers that where not available.  Disclosure:  This is my 1st script. 


function PING {
 PROCESS {
  $results = gwmi -query "SELECT StatusCode FROM Win32_PingStatus WHERE Address = '$_'"
  foreach ($result in $results) {
   if ($result.StatusCode -eq 0) {
    write $_ >> c:\Ping.txt
    }
   else
    {
     write $_ >> c:\NoPing.txt
    }
    break
   }
  }
 }

New-Item -type file c:\Ping.txt -Force
New-Item -type file c:\NoPing.txt -Force

$Ping = gc c:\computers.txt | PING
 
 
function BIOS {
 
 Process {
  foreach ($Asset in $Assets) {
   $name = gwmi win32_computersystem -computerName $_
   $Bios = gwmi win32_Bios -computerName $_
   }
   $a = $name.name
   $b = $name.model
   $c = $Bios.SMBIOSBIOSVersion
   $computer = ("$a,$b,$c") >> c:\BiosVersion.txt
    }
  }
New-Item -type file c:\BiosVersion.txt -Force
$Result = gc C:\Ping.txt | BIOS

halr9000User is Offline
CLI Addict
CLI Addict
Posts:245


03/14/2008 5:31 PM  
Some recommendations:

1. Don't hard code log files to go to C:\.
2. Pull your log file path out to a variable, that way you or anyone else can change it in one place in the file instead of 10. This concept applies to almost anything, not just file paths of course.
3. Just for form's sake, if you are gonna fully spell out the parameter names like ComputerName, might as well expand the aliases e.g. gwmi -> get-wmiobject
4. The way you did the output stream to the biosversion file works, but you are not taking advantage of a lot of cool stuff powershell has to offer. Some things you might want to research:
a. format-* cmdlets
b. write-* cmdlets
c. select-object, new-object for use in creating custom objects. Check out good ole post #25 http://www.bsonposh.com/modules/wordpress/?p=25

Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast (http://powerscripting.net)
Author, TechProsaic (http://halr9000.com)
santonsenUser is Offline
New Member
New Member
Posts:2

04/06/2008 12:18 PM  

OK - went to Sapien/Don Jones class.  Let's see if the re-write is better 

 function TestPing([string]$address) {
 PROCESS {
  $address = $_
  foreach ($computer in $computers) {
   $ping = Get-WmiObject -query "SELECT * FROM Win32_PingStatus WHERE Address = '$address'"
   
   if ($Ping.StatusCode -eq 0) {
   
    $Currentuser = Get-WmiObject Win32_ComputerSystem -computerName $_
    $Bios = Get-WmiObject win32_Bios -computerName $_
    $User = Get-WmiObject win32_computersystem -computerName $_
    $name = Get-WmiObject win32_computersystem -computerName $_
    
    $obj = New-Object PSObject
    $obj | Add-Member NoteProperty PingStatus $ping.statuscode
    $obj | Add-Member NoteProperty ComputerName $name.name
    $obj | Add-Member NoteProperty ComputerModel $name.model
    $obj | Add-Member NoteProperty CurrentUser $Currentuser.Username
    $obj | Add-Member NoteProperty BiosVersion $Bios.SMBIOSBIOSVersion
    $obj | Add-Member NoteProperty BiosSerialNumber $Bios.SerialNumber    
    Write-Output $obj
   }
   Else {
    $obj = New-Object PSObject
    $obj | Add-Member NoteProperty PingStatus $ping.statuscode
    $obj | Add-Member NoteProperty ComputerName $name.name
    Write-Output $obj
   }
  }
 }
}
$results = Get-Content c:\logs\computers.txt | TestPing
$results | Export-Csv c:\logs\BIOSInfo.csv

smurawskiUser is Offline
Shell User
Shell User
Posts:40


04/06/2008 1:14 PM  
Just my 2 cents..


#To make the script more flexible, I would add parameters for the input file and output file
 param
 (
     $ComputersToTest = 'c:\users\(your username here)\documents\computers.txt',
    $output = 'c:\users\(your username here)\documents\BIOSInfo.csv'
 )
 
 function TestPing([string]$address) {
 PROCESS {
  $address = $_
  
  #The foreach ($computer in $computers is not needed).  $computer was never assigned anywhere.
  
   $ping = Get-WmiObject -query "SELECT * FROM Win32_PingStatus WHERE Address = '$address'"
   
   if ($Ping.StatusCode -eq 0) {
   
    $Currentuser = Get-WmiObject Win32_ComputerSystem -computerName $_
    $Bios = Get-WmiObject win32_Bios -computerName $_
    $User = Get-WmiObject win32_computersystem -computerName $_
    $name = Get-WmiObject win32_computersystem -computerName $_
    
    $obj = New-Object PSObject
    $obj | Add-Member NoteProperty PingStatus $ping.statuscode
    $obj | Add-Member NoteProperty ComputerName $name.name
    $obj | Add-Member NoteProperty ComputerModel $name.model
    $obj | Add-Member NoteProperty CurrentUser $Currentuser.Username
    $obj | Add-Member NoteProperty BiosVersion $Bios.SMBIOSBIOSVersion
    $obj | Add-Member NoteProperty BiosSerialNumber $Bios.SerialNumber    
    Write-Output $obj
   }
   else {
    $obj = New-Object PSObject
    $obj | Add-Member NoteProperty PingStatus $ping.statuscode
    $obj | Add-Member NoteProperty ComputerName $name.name
    Write-Output $obj
   }
  }
 }

#The last two lines could be consolidated into 
Get-Content -ReadCount 1 $ComputersToTest | TestPing | Export-Csv $output
#and specifying a ReadCount of 1 means it will pass each line down the pipeline rather than waiting
#to read the whole file before passing each line down the pipeline.  Could be faster if you have a long
#list of computers


Steven Murawski
Co-Host - Mind of Root (www.mindofroot.com)
Host - PowerShell Basics (powershell-basics.com)
glnsizeUser is Offline
Shell Enthusiast
Shell Enthusiast
Posts:60

06/22/2008 11:59 AM  

Great to see the use of custom objects however one thing... Your executing the same query four times.  The beauty of Custom objects is you can go out get all your data, and then return exactly what you need.  Having said that try changing...

$Currentuser = Get-WmiObject Win32_ComputerSystem -computerName $_
    $Bios = Get-WmiObject win32_Bios -computerName $_
    $User = Get-WmiObject win32_computersystem -computerName $_
    $name = Get-WmiObject win32_computersystem -computerName $_
    
    $obj = New-Object PSObject
    $obj | Add-Member NoteProperty PingStatus $ping.statuscode
    $obj | Add-Member NoteProperty ComputerName $name.name
    $obj | Add-Member NoteProperty ComputerModel $name.model
    $obj | Add-Member NoteProperty CurrentUser $Currentuser.Username
    $obj | Add-Member NoteProperty BiosVersion $Bios.SMBIOSBIOSVersion
    $obj | Add-Member NoteProperty BiosSerialNumber $Bios.SerialNumber    
    Write-Output $obj

TO:

$ComputerSystem = Get-WmiObject Win32_ComputerSystem -computerName $_
    $Bios = Get-WmiObject win32_Bios -computerName $_
$obj = New-Object PSObject
$obj | Add-Member NoteProperty PingStatus $ping.statuscode

$obj | Add-Member NoteProperty ComputerName $ComputerSystem.name $obj | Add-Member NoteProperty ComputerModel $ComputerSystem.model $obj | Add-Member NoteProperty CurrentUser $ComputerSystem.Username $obj | Add-Member NoteProperty BiosVersion $Bios.SMBIOSBIOSVersion $obj | Add-Member NoteProperty BiosSerialNumber $Bios.SerialNumber Write-Output $obj

Not trying to nit-pick but, you will see a significant performance increase as you scale up!

You are not authorized to post a reply.
Forums > Using PowerShell > Peer Review > Get the BIOS version



ActiveForums 3.7
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer