Working with Software: Step-by-Step Exercises


In this exercise, we explore the use of WIN32_product and classes provided by the Windows installer provider.

  1. Open Notepad or your favorite script editor.

  2. At the top of your script, declare a variable called $strComputer. Assign the WMI shortcut dot character (.) to indicate you want to connect to WMI on your local machine. This line of code is shown here:

     $strComputer = "."

  3. On the next line, declare the variable $wmiNS, which will be used to hold the WMI namespace for your query. Assign the string "Root\cimv2" to the variable. This line of code is shown here:

     $wmiNS = "root\cimv2"

  4. On the next line, you will use the variable $wmiQuery to hold your WMI query. This query will select everything from the WIN32_Product WMI class. This code is shown here:

     $wmiQuery = "Select * from win32_product"

  5. Because this query can take a rather long time to complete (depending on the speed of your machine, CPU load, and number of installed applications), let’s use the Write-Host cmdlet to inform the user that the script could take a while to run. As long as we are using Write-Host, let’s have a little fun and specify the foregroundcolor argument of the Write-Host cmdlet, which will change the color of our font. I chose blue, but you can choose any color you wish. The foregroundcolor argument is mentioned in Chapter 4, “Using PowerShell Scripts,” and permitted values for this argument are in Table 4-5. Use the `n escape sequence to specify a new line at the end of your command. I used the grave accent character (`) to break the line of code for readability, but this certainly is not necessary for you. The completed code is shown here:

     Write-Host "Counting Installed Products. This" `    "may take a little while. " -foregroundColor blue `n

  6. On the next line, use the variable $objWMIServices to hold the object that is returned by the Get-WmiObject cmdlet. Supply the computer argument with the value contained in the $strComputer variable. Use the grave accent to continue to the next line. This code is shown here:

     $objWMIServices = Get-WmiObject -computer $strComputer `

  7. On the next line, use the namespace argument to specify the WMI namespace for the WMI query. Use the value contained in the $wmiNS variable. Use the query argument to supply the WMI query contained in the $wmiQuery variable to the Get-WmiObject cmdlet. This line of code is shown here:

     -namespace $wmiNS -query $wmiQuery

  8. Use the for statement to print out a progress indicator. Use the variable $i as the counter. Continue counting until the value of $i is less than or equal to the value of the count property of the IwbemObjectSet object contained in the $objWMIServices variable. (If you need to review the use of the for statement, refer to Chapter 4.) The for statement code is shown here:

     for ($i=1; $i -le $objWMIServices.count;$i++)

  9. The code that will be run as a result of the for statement uses the Write-Host cmdlet. We will write "/\" to the console. To keep the Write-Host cmdlet from writing everything on a new line, use the noNewLine argument. To make the progress bar different from the first prompt, use the foregroundcolor argument and specify an appropriate color. I chose red. This line of code is shown here:

     {Write-Host "/\" -noNewLine -foregroundColor red}

  10. Use the Write-Host cmdlet to print out the number of installed applications on the machine. To make the value a little easier to read, use two `n escape sequences to produce two blank lines from the progress indicator. This line of code is shown here:

     Write-Host `n`n "There are " $objWMIServices.count `    " products installed."

  11. Save and run your script. Call it yournameimage from book CountInstalledApplications.ps1. You should see an output similar to the one shown here. If you do not, compare it with image from book CountInstalledApplications.ps1.

     Counting Installed Products. This may take a little while. /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ /\/\/\/\/\/\/\ There are 87 products installed.

  12. Now we are going to add a timer to our script to see how long it takes to execute. On the fourth line of your script, under the $wmiQuery line, declare a variable called $dteStart and assign the date object that is returned by the Get-Date cmdlet to it. This line of code is shown here:

     $dteStart = Get-Date

  13. At the end of your script, under the last Write-Host command, declare a variable called $dteEnd and assign the date object that is returned by the Get-Date cmdlet to it. This line of code is shown here:

     $dteEnd = Get-Date

  14. Declare a variable called $dteDiff and assign the date object that is returned by the New-TimeSpan cmdlet to it. Use the New-TimeSpan cmdlet to subtract the two date objects contained in the $dteStart and $dteEnd variables. The $dteStart variable will go first. This command is shown here:

     $dteDiff = New-TimeSpan $dteStart $dteEnd

  15. Use the Write-Host cmdlet to print out the total number of seconds it took for the script to run. This value is contained in the totalSeconds property of the date object held in the $dteDiff variable. This command is shown here:

     Write-Host "It took " $dteDiff.totalSeconds " Seconds" `    " for this script to complete"

  16. Save your script as yournameimage from book CountInstalledApplicationsTimed.ps1. Run your script and compare your output with that shown here. If your results are not similar, then compare your script with the image from book CountInstalledApplicationsTimed.ps1 script.

     Counting Installed Products. This may take a little while. /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ /\/\/\/\/\/\/\ There are 87 products installed. It took 120.3125 Seconds for this script to complete

  17. This concludes this step-by-step exercise.




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net