Inside the foreach loop, the main function declares the $err variable and sets its value to $NULL. After this, the function uses a trap scriptblock to trap WMI errors, as callout A in Listing 2 shows. If an error occurs, the trap scriptblock updates the error variable to the current error record (i.e., $ERROR\[0]) and continues to the statement following the error.
|
Listing 2: Code That Traps Errors
|
 |
Next, the main function connects to the StdRegProv class on the current computer using the [WMIClass] type accelerator for the Windows .NET Framework's System.Management.ManagementClass. (If you're unfamiliar with PowerShell's type accelerators, see "Type Accelerators: A Useful But Undocumented Feature in PowerShell 1.0".) If an error occurs, it gets trapped by the trap scriptblock shown in callout B in Listing 2. If the $err variable is not $NULL, this means the trap scriptblock was triggered. In this case, the main function uses the Write-Error cmdlet to output the error record, then it uses the continue statement to skip the remainder of the foreach loop and continue to the next computer name in the array.
After this, the main function uses the StdRegProv class's EnumKey method to enumerate the subkeys in the Uninstall key. The EnumKey method's result—an array of subkey names—is stored in the sNames property. The function then iterates through the array of subkey names using another foreach loop.
For each subkey name in the array, the main function uses the GetStringValue method of the StdRegProv class to retrieve the registry key's DisplayName property. The function uses the Join-Path cmdlet to append the subkey's name to the Uninstall key. The resulting registry path is used as an argument for the GetStringValue method, which returns a registry entry's value. (It must be a string value.) The method takes three arguments: a registry hive, the subkey path, and the name of the entry. GetStringValue returns an object that contains two properties: the ReturnValue property, which contains a value indicating the success or the failure of the method call, and the sValue property, which contains the string value. The main function assigns the sValue property's value to the $name variable.
If the $name variable is not $NULL, the main function creates a custom output object that contains four properties: ComputerName, AppID, AppName, Publisher, and Version. It updates the object's ComputerName property with the current computer name, the object's AppID property with the current subkey name, and the object's AppName property with the current application's display name. The function then uses the GetStringValue method twice more to retrieve the current application's Publisher and DisplayVersion entry values and updates the corresponding properties in the custom object with these values.
At this point, the main function has completed reading the information about the current application, so it checks if the $propertyList hashtable contains any keys by checking its Keys collection's Count property. If the hashtable doesn't contain any keys (i.e., if none of the -appID, -appname, -publisher, and -version parameters exist on the command line), then the function simply outputs the custom object.
If the hashtable contains one or more keys, the main function needs to determine which of the command-line parameters' arguments match the property values in the custom object. Listing 3 shows how the main function accomplishes this.
|
Listing 3: Code That Matches All Named Properties
|
 |
First, it sets the $matches variable to 0, then it iterates through the $propertyList hashtable's Keys collection. For each key in the $propertyList hashtable, the function uses PowerShell's -like operator to see if the custom object's property matches the corresponding value from the $propertyList hashtable. If they match, the function increments the $matches variable. If the number of matches is the same as the number of keys in the hashtable, the main function outputs the custom object. Lastly, if the -matchall parameter doesn't exist on the command line, the function uses the break statement to break out of the foreach loop used to iterate through the registry subkeys.
Know What's Installed on Your Computers
The Get-InstalledApp.ps1 PowerShell script, which you can obtain by clicking the Download the Code Here button near the top of the page, provides an easy way to list applications installed on one or more computers. And it can even search for specific applications. This easy-to-use script might be all you need to audit the software that's installed on the computers in your network.