Set Environment Variables in Guest Operating System on VMware

While reading my last post on reading environment variables from a guest operating system, you may wonder how to set environment variables. Don’t be disappointed if I tell you that there is NO direct support for setting an environment variable.

However, you have a workaround - use a command directly. Unlike reading variables, there is no standard ways to do it for different operating systems. You have to first figure out what type of operating system and then run different commands. For example, if you are targeting Windows family of operating systems, you simply run the following:

C:\Windows\System32\cmd.exe /c setx CLOUD Future -m

The command creates a new environment variable called “CLOUD” and sets its value as “Future.” Note that the setupx command is used instead of the setup command, which sets up the environment variable for a session but not persistent when the session is gone. It’s there by default in Windows 7. For Windows XP, it may require you to install The XP Support Tools which can be included in your XP CD. To play safe, you can just try the above command in the target server first.

The -m switch to the setx command is to specify it’s a system level environment variable; if not there, the command sets an environment variable for the currently logged in user.

Now time to take a look at a sample. To run it, first check out the simple prerequisites in a previous post.

/*=============================================================================
Copyright (c) 2012 Steve Jin, All Rights Reserved.

http://www.doublecloud.org

=============================================================================*/

package org.doublecloud.vi.vmware.guest.samples;

import java.net.URL;

import org.doublecloud.vi.vmware.guest.GuestProcessDirector;

import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;

public class GuestSetEnvVars
{
  public static void main(String[] args) throws Exception
  {
    ServiceInstance si = new ServiceInstance(new URL("https://8.8.8.8/sdk"), "Administrator", "doublecloud.org", true);
    Folder rootFolder = si.getRootFolder();

    ManagedEntity[] mes = new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine");
    if (mes == null || mes.length == 0)
    {
      return;
    }

    VirtualMachine vm = (VirtualMachine) mes[0];

    System.out.println("guest tool status:" + vm.getGuest().toolsRunningStatus);
    if (!"guestToolsRunning".equals(vm.getGuest().toolsRunningStatus))
    {
      System.out.println("The VMware Tools is not running in the Guest OS on VM: " + vm.getName());
      System.out.println("Exiting...");
      return;
    }

    GuestProcessDirector progDir = new GuestProcessDirector(vm, "Administrator", "vijava");

    progDir.run("C:\\Windows\\System32\\cmd.exe", "/c Setx CLOUD Future -m");

    String[] envVars = progDir.readEnvironmentVariables(new String[] { "CLOUD" });

    for (String envVar : envVars)
    {
      int pos = envVar.indexOf("=");
      String name = envVar.substring(0, pos);
      String value = envVar.substring(pos + 1);
      System.out.println("name: " + name);
      System.out.println("value: " + value);
    }

    si.getServerConnection().logout();
  }
}

After the command is executed, you should be able to check its value in a similar way as last post. The cmd.exe will also be gone, so you don’t need to kill it.

With above discussion, you may wonder if you can use commands to read environment variables as well. The answer is yes with a little trick to fetch back the standard output. This has to do with GuestFileDirector API to be discussed in future posts. Stay tuned.

This entry was posted in Software Development, vSphere API and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • NEED HELP?


    My consulting helps clients with virtualization and cloud computing, including VMware infrastructure automation and orchestration, vSphere management APIs, and deep product integration with hypervisors. Current training offerings include vSphere APIs training, vCenter Orchestrator training, and etc. Should you, or someone you know, need these consulting services or training, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.