Microsoft KB Archive/305702

= Examples of How to Use WMI Scripts to Make Changes to Windows Installer Packages =

Article ID: 305702

Article Last Modified on 2/28/2007

-

APPLIES TO


 * Microsoft Windows 2000 Server
 * Microsoft Windows 2000 Advanced Server
 * Microsoft Windows 2000 Professional Edition

-



This article was previously published under Q305702



SUMMARY
Window Management Instrumentation (WMI) supports a scripting interface that you can use to make changes to software packages that are installed by using the Windows Installer. This article contains examples that show you how to use Microsoft Visual Basic Script (VBScript) to create WMI scripts to install, remove, and retrieve information about Windows Installer packages.



MORE INFORMATION
The VBScript examples in this article use the Win32_Product WMI class. The Win32_Product class supports the following properties, as shown in Example 1: Enumerate Installed Windows Installer Packages:

Caption

Description

IdentifyingNumber

InstallDate

InstallLocation

InstallState

Name

PackageCache

SKUNumber

Vendor

Version

The Win32_Product class also supports the following methods (as shown Example 2: Install a Windows Installer Package and Example 3: Remove an Installed Windows Installer Package):

Install

Admin

Advertise

Reinstall

Upgrade

Configure

Uninstall

Example 1: Enumerate Installed Windows Installer Packages
This example generates a list of all Windows Installer packages that are installed on the computer. This example in this article uses the Name property, however, you can modify the script to use any property that is supported by Win32_Product. 'set computer name - replace variable with appropriate value Computer = &quot;&quot;

'obtain collection of Windows Installer packages Set MSIapps = GetObject(&quot;winmgmts:{impersonationLevel=impersonate}!\\&quot; & Computer &_    &quot;\root\cimv2&quot;).ExecQuery(&quot;select * from Win32_Product&quot;)

'obtain number of program in collection AppList = AppList & MSIapps.Count & &quot; MSI packages installed:&quot; & VBCRLF & &quot;--&quot; & VBCRLF

'enumerate the names of the packages in the collection For each App in MSIapps AppList = AppList & App.Name & VBCRLF Next

'display list of packages Wscript.Echo AppList

Example 2: Install a Windows Installer Package
This example installs a Windows Installer package on the computer. As the installation process is transparent to the user, it may not be appropriate in situations where the user must provide information or configure options during Setup. 'set variables Computer = &quot;&quot; MSIlocation = &quot;.msi&quot; 'must be complete path of package CmdLineOptions = NULL 'no command line options - change as appropriate AllUsers = FALSE 'install for current user only; set to TRUE to install for all users

'bind to winmgmnt Set WMI = GetObject(&quot;winmgmts:{impersonationLevel=impersonate}!\\&quot; & Computer & &quot;\root\cimv2&quot;)

'obtain an instance of the products object Set WMIapp = WMI.Get(&quot;Win32_Product&quot;)

'install the program WMIApp.Install MSIlocation, CmdLineOptions, AllUsers

Example 3: Remove an Installed Windows Installer Package
This example removes a Windows Installer package from the computer. The removal of the package from the computer is transparent to the user. 'set variables Computer = &quot;&quot; AppToRemove = &quot;&quot; 'case sensitive

'Obtain collection of Windows Installer packages with matching name Set MSIapp = GetObject(&quot;winmgmts:{impersonationLevel=impersonate}!\\&quot; & Computer & &quot;\root\cimv2&quot;).ExecQuery(&quot;select * from Win32_Product&quot;)

'enumerate the returned collection 'and remove the Windows Installer package For each App in MSIapp If App.Name = AppToRemove Then App.Uninstall End If   Next

How to Use the Examples in this Article
The following scripts can be run on the local computer or from a remote Windows 2000-based computer. To use any of the examples in this article, copy the code in the example that you want to use, paste it to the file, and then save the file with a .vbs extension. To do so:
 * 1) Start Notepad and open a blank, new document.
 * 2) Select the code from example that you want to use, and then click Copy.
 * 3) In the Notepad document, click Paste.
 * 4) Make any modifications that you want to use to customize the script, as appropriate to your situation.
 * 5) On the File menu, click Save.
 * 6) In the Save As dialog box, click All Files. In the Save as type box, specify a location where you want to save the file. In the File name box, type  .vbs, where   is the name of the file.
 * 7) Quit Notepad.
 * 8) To run the script, double-click the .vbs file.

Note: To run these scripts, you must have the appropriate permissions to perform the operations that are specified in the script. For example, to use Example 2 you must have the appropriate permissions that you need to log on by using a user account that has the appropriate credentials to install programs.

