Düzenle

Aracılığıyla paylaş


WMI Tasks: Operating Systems

WMI tasks for operating systems obtain information about the operating system, such as version, whether it is activated, or which hotfixes are installed.

The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.

The following procedure describes how to run a script.

To run a script

  1. Copy the code and save it in a file with a .vbs extension, such as filename.vbs. Ensure that your text editor does not add a .txt extension to the file.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type CScript filename.vbs at the command prompt.
  4. If you cannot access an event log, check to see if you are running from an Elevated command prompt. Some Event Log, such as the Security Event Log, may be protected by User Access Controls (UAC).

Note

By default, CScript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript filename.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.

The following table lists script examples that can be used to obtain various types of data from the local computer.

How do I... WMI classes or methods
...determine if a service pack has been installed on a computer? Use the Win32_OperatingSystem class and check the value of the ServicePackMajorVersion and ServicePackMinorVersion properties.
VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    Wscript.Echo objOperatingSystem.ServicePackMajorVersion & "." & objOperatingSystem.ServicePackMinorVersion
Next
PowerShell
Get-WmiObject -Class Win32_OperatingSystem -Namespace "root\cimv2" | `
   format-list ServicePackMajorVersion, ServicePackMinorVersion
...determine when the operating system was installed on a computer?

Use the Win32_OperatingSystem class and the InstallDate property.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    Wscript.Echo "Install Date: " & objOperatingSystem.InstallDate 
Next
PowerShell
Get-WmiObject -Class Win32_OperatingSystem -Namespace "root\cimv2" | format-list InstallDate
...determine which version of the Windows operating system is installed on a computer?

Use the Win32_OperatingSystem class, and retrieve both the Name and Version properties.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    Wscript.Echo objOperatingSystem.Caption & "  " & objOperatingSystem.Version
Next
PowerShell
Get-WmiObject -Class Win32_OperatingSystem -Namespace "root\cimv2" | format-list Caption, Version
...determine which folder is the Windows folder (%Windir%) on a computer?

Use the Win32_OperatingSystem class, and check the value of the WindowsDirectory property.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    Wscript.Echo "Windows Folder: " & objOperatingSystem.WindowsDirectory
Next
PowerShell
Get-WmiObject -Class Win32_OperatingSystem -Namespace "root\cimv2" | format-list WindowsDirectory
...determine what hotfixes have been installed on a computer?

Use the Win32_QuickFixEngineering class.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colQuickFixes = objWMIService.ExecQuery ("Select * from Win32_QuickFixEngineering")
For Each objQuickFix in colQuickFixes
    Wscript.Echo "Description: " & objQuickFix.Description
    Wscript.Echo "Hotfix ID: " & objQuickFix.HotFixID
Next
PowerShell
Get-WmiObject -Class Win32_QuickFixEngineering -Namespace "root\cimv2" | format-list Description, HotFixIDs
...determine if I need to activate the operating system on a computer?

Use the Win32_WindowsProductActivation class and check the value of the ActivationRequired property.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colWPA = objWMIService.ExecQuery ("Select * from Win32_WindowsProductActivation")
For Each objWPA in colWPA
    Wscript.Echo "Activation Required: " & objWPA.ActivationRequired
    Wscript.Echo "Remaining Evaluation Period: " & objWPA.RemainingEvaluationPeriod
    Wscript.Echo "Remaining Grace Period: " & objWPA.RemainingGracePeriod
Next
PowerShell
Get-WmiObject -Class Win32_WindowsProductActivation -computer "." -Namespace "root\cimv2" | `
     format-list ActivationRequired, RemainingEvaluationPeriod, RemainingGracePeriod

WMI Tasks for Scripts and Applications

WMI C++ Application Examples

TechNet ScriptCenter