Bookmark and Share

 

(Note: These solutions were written for Beginner Event 5 of the 2010 Scripting Games.) 
 

Beginner Event 5 (Windows PowerShell)

Photo of Laerte Poltronieri Junior


Laerte Poltronieri Junior is a MCDBA—DBA SQL Server and can be found at the following:

·         www.laertejuniordba.spaces.live.com

·         www.simple-talk.com/author/laerte-junior

·         www.twitter.com/laertejuniordba

·         SQL Server Powershell Extension Team: http://sqlpsx.codeplex.com/

 

I wrote this script using the advanced functions and parameter ComputerName as ValueFromPipeline = $ true to enable the receiving values from the pipe if there is more than one computer to be displayed. I also used the Win32_Processor Class as a basis for displaying the description of the family.


I needed a simple and objective way to make an inventory of all computers of the company. The major requirements were the CPU data, family, and manufacturer. Additionally, it is used to display the CPU speed at the moment.


Here is the script.

function get-CpuInfo

{

      param (

                  [Parameter(Position=0, Mandatory=$false, ValueFromPipeline = $true)] [string] $ComputerName = $env:computername

                 

              )

             

      Begin

      {

     

            function ConvertTo-CPUCaption($Value)

            {

                  switch($value)

                  {

                        1   {"Other";break}

                        2   {"Unknow";break}

                        3   {"8086";break}

                        4   {"80286";break}

                        5   {"Intel386™ Processor";break}

                        6   {"Intel486™ Processor";break}

                        7   {"8087";break}

                        8   {"80287";break}

                        9   {"80387";break}

                        10  {"80487";break}

                        11  {"Pentium Brand";break}

                        12  {"Pentium Pro";break}

                        13  {"Pentium II";break}

                        14  {"Pentium Processor with MMX™ Technologyr";break}

                        15  {"Celeron™";break}

                        16  {"Pentium II Xeon™";break}

                        17  {"Pentium III";break}

                        18  {"M1 Family";break}

                        19  {"M2 Family";break}

                        24  {"AMD Duron™ Processor Family";break}

                        25  {"K5 Family";break}

                        26  {"K6 Family";break}

                        27  {"K6-2";break}

                        28  {"K6-3";break}

                        29  {"AMD Athlon™ Processor Family";break}

                        30  {"AMD2900 Family";break}

                        31  {"K6-2+";break}

                        32  {"Power PC Family";break}

                        33  {"Power PC 601";break}

                        34  {"Power PC 603";break}

                        35  {"Power PC 603+";break}

                        36  {"Power PC 604";break}

                        37  {"Power PC 620";break}

                        38  {"Power PC X704";break}

                        48  {"Alpha Family";break}

                        49  {"Alpha 21064";break}

                        50  {"Alpha 21066";break}

                        51  {"Alpha 21164";break}

                        52  {"Alpha 21164PC";break}

                        53  {"Alpha 21164a";break}

                        54  {"Alpha 21264";break}

                        55  {"Alpha 21364";break}

                        64  {"MIPS Family";break}

                        65  {"MIPS R4000";break}

                        66  {"MIPS R4200";break}

                        67  {"MIPS R4400";break}

                        68  {"MIPS R4600";break}

                        69  {"MIPS R10000";break}

                        80  {"SPARC Family";break}

                        81  {"SuperSPARC";break}

                        82  {"microSPARC II";break}

                        83  {"microSPARC IIep";break}

                        84  {"UltraSPARC";break}

                        85  {"UltraSPARC II";break}

                        86  {"UltraSPARC IIi";break}

                        87  {"UltraSPARC III";break}

                        88  {"UltraSPARC IIIi";break}

                        96  {"68040";break}

                        97  {"68xxx Family";break}

                        98  {"68000";break}

                        99  {"68010";break}

                        100 {"68020";break}

                        101  {"68030";break}

                        112  {"Hobbit Family";break}

                        120  {"Crusoe™ TM5000 Family";break}

                        121  {"Crusoe™ TM3000 Family";break}

                        122  {"Efficeon™ TM8000 Family";break}

                        128  {"Weitek";break}

                        130  {"Itanium™ Processor";break}

                        131  {"AMD Athlon™ 64 Processor Famiily";break}

                        133  {"AMD Opteron™ Processor Family";break}

                        144  {"PA-RISC Family";break}

                        145  {"PA-RISC 8500";break}

                        146  {"PA-RISC 8000";break}

                        147  {"PA-RISC 7300LC";break}

                        148  {"PA-RISC 7200";break}

                        149  {"PA-RISC 7100LC";break}

                        150  {"PA-RISC 7100";break}

                        160  {"V30 Family";break}

                        176  {"Pentium III Xeon™ Processor";break}

                        177  {"Pentium III Processor with Intel SpeedStep™ Technology";break}

                        178  {"Pentium 4"; break}       

                        179  {"Intel Xeon™"; break}       

                        180  {"AS400 Family"; break}  

                        181  {"Intel Xeon™ Processor MP";break}

                        182  {"AMD Athlon™ XP Family";break}

                        183  {"AMD Athlon™ MP Family";break}

                        184  {"Intel Itanium 2";break}

                        185  {"Intel Pentium M Processor";break}

                        190  {"K7";break}

                        200  {"IBM390 Family";break}

                        201  {"G4";break}

                        202  {"G5";break}

                        203  {"G6"; break}       

                        204  {"z/Architecture Base"; break}   

                        250  {"i860";break}

                        251  {"i960";break}

                        260  {"SH-3";break}

                        261  {"SH-4";break}

                        280  {"ARM";break}

                        281  {"StrongARM";break}

                        300  {"6x86";break}

                        301  {"MediaGX";break}

                        302  {"MII";break}

                        320  {"WinChip"; break}       

                        350  {"DSP"; break}

                        500  {"Video Processor"; break}

                  default {"Unknown";BREAK}

                  }

            }

 

      }

             

      Process

      {

            get-wmiobject Win32_Processor -ComputerName $ComputerName |

                  Select-Object     @{Expression={$ComputerName};Label="Computer Name"},

                                          @{Expression={$_.CurrentClockSpeed};Label="CPU Speed"},

                                          @{Expression={ConvertTo-CPUCaption -Value $_.family};Label="Family"},

                                          Manufacturer

           

      }

}

 

 

Here is the result from running the script:

Image of result of running script

  

Beginner Event 5 (VBScript)

Photo of Andrew Barnes

 

Andrew Barnes is a MCTS and a MCITP. He can be found at http://uk.linkedin.com/in/andrewdbarnes.

I am currently a senior IT systems administrator at the British Cycling Federation. I am also a Microsoft Certified IT Professional and a moderator on The Official Scripting Guys Forum where I share my experience and expertise. I have more than 20 years’ experience in the UK IT industry. Holding SC clearance in the past, I have worked for the Ministry of Defense, British Aerospace, and the Criminal Records Bureau.


My skills are broad I can do almost the entire spectrum of technical things from soldering components on circuit boards to scripting, operating system deployment, and configuration of server applications. My biggest sense of satisfaction comes from helping others to find solutions. I prefer to show people how to help themselves, rather than give them an answer. I also enjoy reducing workloads through automation.

------------

When I first saw this event I thought, how can I make a WMI lookup script appear so simple that it won’t leave a beginner crying to their mums? The answer is: I can’t. There is no escaping the fact that a WMI routine in VBScript looks complex the first time you see it. There’s also no escaping the fact that some people just want any excuse to go crying to their mums.


Now here in England we pride ourselves on our sense of fair play and stiff upper lip, chin-chin, tally-ho, old chap! So having said all that the best way I can help a beginner to solve this event is to teach them how to cheat! Yes! That’s right!


Here’s my script.

' ========================================================

'

'     Title:                  Scripting Games 2010, Beginner Event: 5 Solution

'     Author:                 Andrew D Barnes

'     Date created:           29 March 2010

'     Date Modified:          29 March 2010

'     File Name:        Solution5.vbs

'     Description:            Performs a simple WMI lookup and outputs to the screen

'                      

' ========================================================

 

Option Explicit

 

'----- Declare Variables -----

Dim objShell, strComputer, Title, Message

Dim objWMIService, colItems, objItem

 

'----- Initialize title text -----

Title = "Scripting Games 2010, Beginner Event 5 Solution"

 

'----- Get object -----

Set objShell = CreateObject("Wscript.Shell")

 

'----- Define Variables -----

strComputer = InputBox("Enter the Computername",Title,".")

 

'-------------------------

'----- Begin Routine -----

'-------------------------

 

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Processor")

 

      For Each objItem In colItems

            Message = "The CPU speed on " & objItem.SystemName

            Message = Message & " is currently " & objItem.CurrentClockSpeed & "mhz" & VbCrLf

            Message = Message & "The maker is " & objItem.Manufacturer & VbCrLf & VbCrLf

            Message = Message & "The CPU Family is " & objItem.Description & VbCrLf

      Next

 

'-------------------------

'-----  End Routine  -----

'-------------------------

 

'----- Display results -----

objShell.Popup  Message,, Title, vbInformation + vbOKOnly

 

'----- End Script -----

WScript.Quit

 

It works like this. You double-click the script and then this prompt appears:

Image of prompt that appears after double-clicking script

 

You can enter the remote computer name or just press OK for this computer. The following results then appear:

Image of results of entering computer name


Spiffing! I hear you say? Where’s that cheating you spoke of? Steady on, old bean! Here’s how I did it.


First, I downloaded the Scriptomatic 2.0 (some call it WMI Script Cheating) tool.


Now this wonderful little tool has the ability to write the routine that I can put into the heart of my script. It’s not exactly cricket but it’s a jolly good way to learn Window Management Instrumentation.

Image of Scriptomatic 2.0

 

The tool is simple:

1.     Connect to the default WMI namespace.

2.     Browse to the most relevant WMI class you’re looking for (for example, physical memory) to find out information about physical memory.

3.     Choose your scripting language.

4.     Output results to the Command Prompt window or Notepad.

5.     Click Run.


There you have it! The output is lengthy, but looking at the list, you can easily filter the results you’re interested in. Scriptomatic has done all the work by performing a WMI lookup and it has also generated the script code. Outstanding!


What happened to my sense of fair play? Well, I wasn’t in the 2010 Scripting Games myself; I am just showing how to solve the event in the easiest way possible. Besides; the only people who complain about Scriptomatic being a cheating tool are people who have already learned to perform WMI (the hard way no doubt).


Right, that’s quite enough of that! All we’re interested in now is CurrentClockSpeed, Description, Manufacturer, and SystemName(and Name for fun), so delete the other Wscript.Echo lines from Scriptomatic. The WMI part of your script should now look like this:

Image of how WMI part of script should now look

 

Now here’s an interesting piece of trivia. Did you know objItem.Name gives the same results as right-clicking Computer and then clicking Properties? Well, now you do!


Click Run and you should have output similar to what’s shown in the following image.

Image of output after clicking Run


Your script is basically done. All you need to decide is how you’re going to present it. The usual choices are output to text or to the screen. I’ve uploaded a couple templates to the Script Repository.


You could cut and paste the text into a template such as mine. Do this 2–3 times and you’ll be an expert.


You’ll notice that I trimmed away the text "WQL", _wbemFlagReturnImmediately + wbemFlagForwardOnly”). This part of the code is there to stop large WMI collections from hanging. You should learn and use it when you’re ready.


The only extra thing I did was to add the following code to prompt for a remote computer name. The default has been set to “.” which just means this is computer that is running the script.

Image of additional code added to prompt for remote computer name 

 

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys