March 18th, 2005

How Can I Create an Environment Variable Using a Script?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Can I create an environment variable using a script?

— OD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, OD. WMI is a great technology for scripting, but it has its eccentricities. For example, can you create a process using a script? Of course you can; after all, WMI’s Win32_Process class has a Create method. Can you create a service using a script? Of course you can; after all, WMI’s Win32_Service class has a Create method. Can you create an environment variable using a script? Of course you can, despite the fact that the Win32_Environment class doesn’t have a Create method. Strange but true!

In fact, here’s a script that creates a new environment variable named TestValue:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set objVariable = objWMIService.Get(“Win32_Environment”).SpawnInstance_

objVariable.Name = “TestValue” objVariable.UserName = “<System>” objVariable.VariableValue = “This is a test” objVariable.Put_

We know: this looks different from most WMI scripts. But don’t worry, we’re going to walk through the code, step-by-step.

Our environment variable script begins the same way most WMI scripts do: by connecting to the WMI service. After connecting to the WMI service you typically call ExecQuery to return a collection of items. However, we’re not going to do that with this script. Instead, we’re going to use the SpawnInstance_ method to create a “blank” environment variable, one that exists only in memory. (We use the term “blank” because SpawnInstance_ creates an environment variable that has all the properties of an environment variable; however, at the time of creation none of those properties are assigned a value. Hence a “blank” instance.)

This line of code creates an object reference (objVariable) that points to our new environment variable (which, again, exists only in memory when created):

Set objVariable = objWMIService.Get(“Win32_Environment”).SpawnInstance_

And, no, that’s not a misprint: the name really isSpawnInstance_, with an underscore coming at the end. You’ll see a lot of underscores before this column is finished.

What we need to do now is fill in the properties of our environment variable. In particular, we need to specify three values:

Name. This is simply the name of the environment variable. In this sample script we use the clever name TestValue.

UserName. This is the owner of the environment variable. To make this a system variable, set UserName to System; to make it a user-specific environment variable, set UserName to the user account name (for example, fabrikam\kenmyer).

VariableValue. The assigned value of the environment variable. Once again we’ve gone overboard with our cleverness, assigning TestValue the value This is a test.

And, yes, that’s why we Scripting Guys get paid the big bucks.

With the three property values assigned, we then call the Put_ method, which actually writes the new environment variable to the operating system. Mission accomplished.

What do you mean, “Are you sure?” If you don’t believe us, a quick way to verify creation is to use a script similar to this one. This script uses a WQL query to select all environment variables with the name TestValue, then echoes the property values of that variable to the screen:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colItems = objWMIService.ExecQuery _ (“Select * from Win32_Environment Where Name = ‘TestValue'”)

For Each objItem in colItems Wscript.Echo “Name: ” & objItem.Name Wscript.Echo “User Name: ” & objItem.UserName Wscript.Echo “Variable Value: ” & objItem.VariableValue Wscript.Echo Next

Of course, what’s the fun of having a brand-new, custom-made environment variable if you never get to play with it? If you want to change the value of your new environment variable, simply connect to that variable (using the same WQL query we just showed you) and assign a new value to the VariableValue property. Call the Put_ method to write the changes to the operating system, and your environment variable will take on the new value. For example, this sample script changes the value of TestValue to New value:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colItems = objWMIService.ExecQuery _ (“Select * from Win32_Environment Where Name = ‘TestValue'”)

For Each objItem in colItems objItem.VariableValue = “New value” objItem.Put_ Next

Child’s play.

But what if you want to delete your environment variable? (Yes, even after all the work you did to create it.) That’s easy enough: we just take the same basic approach we used when modifying the value of the environment variable. This time, however, we don’t assign a new value to the VariableValue property; instead we simply call the Delete_ method to delete the variable:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colItems = objWMIService.ExecQuery _ (“Select * from Win32_Environment Where Name = ‘TestValue'”)

For Each objItem in colItems objItem.Delete_ Next

Again, note the underscore on the end of the method name: Delete_.

Of course, there’s still one question left unanswered: how did we know that the Win32_Environment class supported the SpawnInstance_ and Delete_ methods? In this case, we used Wbemtest.exe. Call up Wbemtest and take a look at the Win32_Environment class:

Wbemtest


If you look at the very top of the screen (under the Qualifiers heading) you’ll see the CreateBy and DeleteBy qualifiers, followed by the methods used to create a new instance (PutInstance) and delete an existing instance (DeleteInstance). That’s how we could tell that Win32_Environment supported SpawnInstance_ and Delete_.

Author

0 comments

Discussion are closed.