Examples of How to Use WMI Scripts to Make Changes to Windows Installer Packages (305702)



The information in this article applies to:

  • Microsoft Windows 2000 Server
  • Microsoft Windows 2000 Advanced Server
  • Microsoft Windows 2000 Professional

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 = "<ComputerName>"

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

	'obtain number of program in collection
	AppList = AppList & MSIapps.Count & " MSI packages installed:" & VBCRLF & "------" & 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 = "<ComputerName>"
	MSIlocation = "<MSIFile>.msi" '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("winmgmts:{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2")

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

	'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 = "<ComputerName>"
	AppToRemove = "<MSIApp>" 'case sensitive

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

	'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 FileName.vbs, where FileName 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.

REFERENCES

For more information about the Win32_Product class, visit the following Microsoft Developer Network (MSDN) Web site:For more information about Windows Installer technology, visit the following Microsoft Developer Network (MSDN) Web site: For more information about WMI, visit the following MSDN Web site:

Modification Type:MajorLast Reviewed:6/6/2003
Keywords:kbinfo KB305702