Επεξεργασία

Κοινή χρήση μέσω


Win32_Environment class

The Win32_Environment WMI class represents an environment or system environment setting on a Windows computer system. Querying this class returns environment variables found in:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Sessionmanager\Environment

and

HKEY_USERS\<user>\Environment

The following syntax is simplified from Managed Object Format (MOF) code and includes all of the inherited properties. Properties are listed in alphabetic order, not MOF order.

Syntax

[Dynamic, Provider("CIMWin32"), Privileges("SeRestorePrivilege"), UUID("{8502C4D2-5FBB-11D2-AAC1-006008C78BC7}"), SupportsCreate, CreateBy("PutInstance"), SupportsDelete, DeleteBy("DeleteInstance"), SupportsUpdate, AMENDMENT]
class Win32_Environment : CIM_SystemResource
{
  string   Caption;
  string   Description;
  datetime InstallDate;
  string   Status;
  string   Name;
  boolean  SystemVariable;
  string   UserName;
  string   VariableValue;
};

Members

The Win32_Environment class has these types of members:

Properties

The Win32_Environment class has these properties.

Caption

Data type: string

Access type: Read-only

Qualifiers: MaxLen (64), DisplayName ("Caption")

A short textual description of the object.

This property is inherited from CIM_ManagedSystemElement.

Description

Data type: string

Access type: Read-only

Qualifiers: DisplayName ("Description")

A textual description of the object.

This property is inherited from CIM_ManagedSystemElement.

InstallDate

Data type: datetime

Access type: Read-only

Qualifiers: MappingStrings ("MIF.DMTF|ComponentID|001.5"), DisplayName ("Install Date")

Indicates when the object was installed. Lack of a value does not indicate that the object is not installed.

This property is inherited from CIM_ManagedSystemElement.

Name

Data type: string

Access type: Read/write

Qualifiers: Override ("Name"), key, MappingStrings ("Win32Registry|System\\CurrentControlSet\\Control\\Session Manager\\Environment")

Character string that specifies the name of a Windows-based environment variable. By specifying the name of a variable that does not yet exist, an application creates a new environment variable.

Example: "Path"

Status

Data type: string

Access type: Read-only

Qualifiers: MaxLen (10), DisplayName ("Status")

String that indicates the current status of the object. Operational and non-operational status can be defined. Operational status can include "OK", "Degraded", and "Pred Fail". "Pred Fail" indicates that an element is functioning properly, but is predicting a failure (for example, a SMART-enabled hard disk drive).

Non-operational status can include "Error", "Starting", "Stopping", and "Service". "Service" can apply during disk mirror-resilvering, reloading a user permissions list, or other administrative work. Not all such work is online, but the managed element is neither "OK" nor in one of the other states.

This property is inherited from CIM_ManagedSystemElement.

Values include the following:

OK ("OK")

Error ("Error")

Degraded ("Degraded")

Unknown ("Unknown")

Pred Fail ("Pred Fail")

Starting ("Starting")

Stopping ("Stopping")

Service ("Service")

Stressed ("Stressed")

NonRecover ("NonRecover")

No Contact ("No Contact")

Lost Comm ("Lost Comm")

SystemVariable

Data type: boolean

Access type: Read-only

Qualifiers: MappingStrings ("Win32Registry|System\\CurrentControlSet\\Control\\Session Manager\\Environment")

Indicates whether the variable is a system variable. A system variable is set by the operating system, and is independent from user environment settings.

UserName

Data type: string

Access type: Read-only

Qualifiers: key, MaxLen (260), MappingStrings ("Win32Registry|System\\CurrentControlSet\\Control\\Session Manager\\Environment")

Name of the owner of the environment setting. It is set to <SYSTEM> for settings that are specific to the Windows-based system (as opposed to a specific user) and <DEFAULT> for default user settings.

Example: "JSmith"

VariableValue

Data type: string

Access type: Read/write

Qualifiers: MappingStrings ("Win32Registry|System\\CurrentControlSet\\Control\\Session Manager\\Environment")

Placeholder variable of a Windows-based environment variable. Information like the file system directory can change from computer to computer. The operating system substitutes placeholders for these.

Example: "%SystemRoot%"

Remarks

The Win32_Environment class is derived from CIM_SystemResource. You can use this class to find the paths of special folders such as the System folder or Program files on a remote machine. Some examples are: windir, systemroot, programfiles, and userprofile. Win32_Environment basically returns what can be found in:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Sessionmanager\Environment

and

HKEY_USERS\<user>\Environment

The calling process that uses this class must have the SE_RESTORE_NAME privilege on the computer in which the registry resides. For example, if you enumerate this class on the local computer, the account under which your application runs must have this privilege. For more information, see Executing Privileged Operations.

Examples

The following VBScript code example enumerates the environment variables on the local computer.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colVar = objWMIService.ExecQuery("Select * from Win32_Environment")
For Each objVar in colVar
    Wscript.Echo "Description: " & objVar.Description & VBNewLine _
               & "Name: " & objVar.Name & VBNewLine _
               & "System Variable: " & objVar.SystemVariable & VBNewLine _
               & "User Name: " & objVar.UserName & VBNewLine _
               & "Variable Value: " & objVar.VariableValue 
Next

The following VBScript code example changes an environment variable named BUILD_TYPE to a value input by the user. The script assumes that the BUILD_TYPE variable already exists. If it does not exist then the script ends. The input value is checked: it must be either "Build1", "Build2", or "Build3", and no other value is accepted. The VBScript UCase function makes the input case-insensitive. If what is typed in is not one of the three acceptable values, the script ends.

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Environment")
Found = False
For Each objItem in colItems
    If objItem.Name = "BUILD_TYPE" Then
    Found = True
    Change = UCase(InputBox("BUILD_TYPE currently = " & objItem.VariableValue & VBNewLine _
                          & "Change options are Build1, Build2, Build3 "))
        If UCase(Change) = "BUILD1" OR Change = "BUILD2" OR Change = "BUILD3" Then
            objItem.VariableValue = Change
            objItem.Put_
        WScript.Echo "BUILD_TYPE changed to " & objItem.VariableValue
        Else 
        WScript.Echo "No input or unacceptable input." & " No change to BUILD_TYPE"
        End If
    End If
Next
If Found = False Then
    WScript.Echo "User-defined environment variable BUILD_TYPE not found."
End If

Requirements

Requirement Value
Minimum supported client
Windows Vista
Minimum supported server
Windows Server 2008
Namespace
Root\CIMV2
MOF
CIMWin32.mof
DLL
CIMWin32.dll

See also

CIM_SystemResource

Operating System Classes