Read Environment Variables in Guest Operating System on VMware

My last post explained how to run, kill, and list programs in guest operating system on VMware. In that post, I mentioned that you can actually use the same API, GuestProgramDirector in particular, to read environment variables. I think the explanation is detailed enough for an implementation.

Still, a good sample provides more details. That is why I decided to write a quick sample just to show how to read environment variables. While trying the sample by myself, I did find more that I will discuss after the sample code.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

Now let’s get to the sample code. Note that I won’t repeat the pre-requisites and tips to run samples, but please check last post.

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


import guest.GuestProcessDirector;

import java.util.Calendar;

import com.vmware.vim25.GuestProcessInfo;
import com.vmware.vim25.GuestProgramSpec;
import com.vmware.vim25.NamePasswordAuthentication;

public class GuestReadEnvVars
  public static void main(String[] args) throws Exception
    ServiceInstance si = new ServiceInstance(new URL(""), "Administrator", "", true);
    Folder rootFolder = si.getRootFolder();

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

    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());

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

    String[] envVars = progDir.readEnvironmentVariables(new String[] {"PATH", "Cloud", "TEMP", "OS"});

    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);



If you put an invalid (non-existing) environment variable name, like Cloud in the sample, to the readEnvironmentVariables() method, it doesn’t complain but silently ignore it. The returned list contains only those that exist, therefore the number of returned environment variables could be less than what you passed in.

Although the order of the returned variables are the same as the order of the names you pass in, it’s not explicitly specified in API spec. To be safe, you’d better not code based on this assumption.

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


  1. santhosh
    Posted November 12, 2012 at 6:30 am | Permalink

    Hi Steve , this VI java really helpful to me. Can you explain what percentage resources using a process which is running on Guest Virtual machine

    Thank you,

  2. Posted November 12, 2012 at 11:53 am | Permalink

    Thanks Santhosh, I am glad you like it. Did you mean to find out the percentage of CPU used by a process running in a guest OS?

  3. santhosh
    Posted November 14, 2012 at 1:27 am | Permalink

    @Steve Jin

    Thanks Steve for replying to me , I need to find CPU and Memory utilization of a process which is running on Guest Virtual Machine.

    Thanks in advance

  4. Posted November 14, 2012 at 1:47 am | Permalink

    You have to run platform specific commands. On Windows, for example, you can use wmic command like this: (
    wmic path win32_perfFormattedData_perfProc_process get Name, PercentProcessorTIme
    You can also write a program to get whatever you want.
    On Linux, you can try ps -p -o %cpu,%mem (

  5. santhosh
    Posted November 14, 2012 at 2:48 am | Permalink

    @Steve Jin

    I need to find CPU and Memory utilization of a process which is running on Guest Virtual Machine from vCenter using VI java.

  6. Posted November 14, 2012 at 2:37 pm | Permalink

    There is no direct support for that in vSphere API. You want to run commands (see my previous reply) in guest operating systems for the information you want.

  7. Srinivasan V N
    Posted February 1, 2013 at 1:20 am | Permalink

    Plesae could you tell, where i have to download jar file for below class.

  8. Posted February 1, 2013 at 11:42 am | Permalink
  9. Mark Norman
    Posted August 22, 2013 at 9:17 am | Permalink

    I can use GuestProcessDirector successfully with Windows guests, but with Linux guests, I always get this exception: com.vmware.vim25.InvalidGuestLogin.
    The login info I am supplying is correct and the Linux guest has VMware tools installed.


  10. Posted August 21, 2015 at 2:18 am | Permalink

    That is very fascinating, You are an overly professional blogger.

    I’ve joined your feed and stay up for in quest of more of your fantastic post.

    Additionally, I have shared your website in my social networks

  11. Posted September 16, 2015 at 1:12 am | Permalink

    I blog quite often and I genuinely appreciate your content.

    The article has really peaked my interest. I am going to book mark your blog and keep
    checking for new details about once a week.
    I opted in for your Feed too.

  12. Posted September 18, 2016 at 9:40 am | Permalink

    Appreciating the hardd work you put into your blog and detailed information you
    provide. It’s great to come across a blog every once in a while that isn’t
    the same unwanted rehashed material. Wonderful read!

    I’ve saved your site and I’m adding your RSS feeds to my Google account.

2 Trackbacks

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=""> <s> <strike> <strong>


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__

    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.