You want to change environment variables for all users on a computer.
You can enumerate all instances of the
Win32_Environment
class and then modify any instances in which the environment variable
'envtmpchng.vbs 'changes the location of temporary file environment 'variables Tmp and Temp Dim objServices Dim objWMIObject, objWMIObjects 'get a reference to a WMI service Set objServices = _ GetObject("
winmgmts
:{impersonationLevel=impersonate}") Set objWMIObjects = objServices.InstancesOf("Win32_Environment") On Error Resume Next 'loop through each environment variable for temp For Each objWMIObject In objWMIObjects 'check if environment variable is TEMP or TMP If objWMIObject.Name = "TEMP" Or objWMIObject.Name = "TMP" Then objWMIObject.VariableValue = "d:\temp" 'update the settings Call objWMIObject.Put_ End If Next
Under Windows NT, environment variables can be modified for the current logged-on user. A profile is created for each user that uses the NT machine. The profile stores a copy of the
The Shell object's Environment object can change a variable for the current user, but not for any other profiles stored on the local system. The WMI object exposes environment variables through the Win32_Environment class.
If the user is logged on with administrative rights, he or she can access the environment variables of all profiles on the local machine.
The
Win32_Environment
class key is a combination of the environment variable name and user name. The user name and environment variable name are exposed through the
Win32_Environment
class as the
UserName
and
Name
properties, respectively. The user name can
To reference an instance of a WMI object with a
The following example gets an instance of the environment variable Temp for user Fred and sets the value to d:\temp :
Dim objServices, objWMIObject 'get a reference to a WMI service Set objServices = _ GetObject("winmgmts:{impersonationLevel=impersonate}") 'gets a reference to the environment variable TEMP for user Administrator Set objWMIObject = objServices.Get _ ("Win32_Environment.Name='TEMP',UserName='C3I\Administrator'") objWMIObject.VariableValue = "d:\temp" Call objWMIObject.Put_
Windows 9 x /ME machines can list environment variables but cannot modify the values.
Solution 2.3. For more information, read the MSDN Library article "Win32_Environment" ( http://msdn.microsoft.com/library/en-us/wmisdk/r_32os3_4dbo.asp ).
You want to terminate a system process.
You can get a reference to the system process you want to terminate by querying Win32_Process for the handle (process ID) for the process you want to terminate. You can invoke the Terminate method on the process object in this way:
Dim objServices, objWMIObject, nResult Set objServices = GetObject("
winmgmts
:{impersonationLevel=impersonate}") Set objWMIObject = objServices.Get("Win32_Process.Handle=326") nResult = objProcess.Terminate(0)
System processes are exposed through the WMI Win32_Process class. Access to process information is available to Windows NT/2000/XP/9 x /ME.
Each process is identified by a handle/process ID (PID). The PID is a numeric identifier assigned to the process when the process is created. The process handle is unique for each instance of the process. The following script lists all running processes with their handle and executable
Dim objWMIObjects, objWMIObject, objServices Set objServices = _ GetObject("winmgmts:{impersonationLevel=impersonate}!root\cimv2") 'create an instance of the Win32_Process class Set objWMIObjects = objServices.InstancesOf("Win32_Process") 'loop through each process and list the handle and executable For Each objWMIObject In objWMIObjects WScript.Echo objWMIObject.Handle & ""& objWMIObject.ExecutablePath
Next
The PID is exposed through the
Handle
property. The
To terminate an existing process, get a reference to the process you want to terminate and invoke the
Terminate
method. The syntax is as
nResult = objProcess.Terminate(nReason)
The nReason parameter is a process-specific reason code. Unless you know of any specific values to pass to a process, this value is 0. The Terminate method returns 0 if successful—any other value indicates that an error occurred.
The following script,
procmaint.wsf
, allows for processes to be listed and
<?xml version="1.0" ?> <job> <!--comment Script:procmaint.wsf Description: Performs process operations --> <script language="VBScript" src="wmiinc.vbs"> <![CDATA[ Dim avar, objDescriptor Dim objInstance, strMachine, strPermission Dim objService, nProcID, bList, objProcess, nResult strMachine = Null nProcID = Null On Error Resume Next 'check if script is being run from command prompt If Not IsCscript Then ExitScript _ "This script must be run from command line using cscript.exe",False End If If WScript.Arguments.Count = 0 Then ShowUsage End If GetArguments If strMachine<> "" Then Set objService = _ GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _ & strMachine) Else Set objService = _ GetObject("winmgmts:{impersonationLevel=impersonate}") End If 'check if list flag is set.. If bList Then 'get list of processes Set objInstance = _ objService.ExecQuery("Select Handle, Description From Win32_Process") For Each objProcess In objInstance WScript.Echo objProcess.Handle, objProcess.Description Next ElseIf Not IsNull(nProcID) Then 'get a reference to specified process specified by the PID Set objProcess = objService.Get("Win32_Process.Handle=" _ & chr(34) & nProcID & chr(34)) If Err Then ExitScript _ "Unable to get reference to process:" & nProcID,False 'terminate process nResult = objProcess.Terminate(0) If nResult Then WScript.Echo "Successfully terminated process# "& nProcID Else WScript.Echo "Unable to terminate process# "& nProcID End If End If 'Reads command line arguments Sub GetArguments Dim nF, strArg 'loop through command line parameters For nF = 0 to WScript.Arguments.Count - 1 Select Case Ucase(WScript.Arguments(nF)) Case "/KILL" 'stop specified process nProcID = GetParameter(nF) Case "/LIST" 'lists specified processes bList = True Case "/MACHINE" 'gets machine name strMachine = GetParameter(nF) End Select Next End Sub 'gets next command line argument 'Parameters nIndex command line argument number to process Function GetParameter(nIndex) If nIndex+1> WScript.Arguments.Count-1 Then ExitScript "Not enough arguments", True End If GetParameter = WScript.Arguments(nIndex+1) End Function Sub ShowUsage WScript.Echo "procmaint
performs
process operations" & vbCrLf & _ "Syntax:" & vbCrLf & _ "shrmaint.wsf [/LIST] [/MACHINE name] [/KILL procid]" & vbCrLf & _ "/LIST optional. Lists active process information "& vbCrLf & _ "/MACHINE optional. Name of machine to perform operations" & vbCrLf & _ "/KILL optional. Terminates process for specified procid" & vbCrLf & _ "Example: list processes on machine thor:" & vbCrLf & _ "procmaint.wsf /MACHINE thor /list" & vbCrLf & _ "Example: terminate process with id 100:" & vbCrLf & _ "procmaint.wsf /kill 100" End Sub ]]> </script> </job>
Execute the procmaint.wsf script from the command line without any parameters to retrieve the syntax.
For more information, read the MSDN Library article "Managing Windows with WMI" ( http://msdn.microsoft.com/library/techart/mngwmi.htm ).