Home > Blogs > VIX API Blog > Monthly Archives: December 2008

Monthly Archives: December 2008

Call VIX more easily from .NET (plus PowerShell goodness)

A lot of great new VIX stuff is coming up from all over, this time it's Daniel Doubrovkine on the VIX community telling us about a library he's created that makes calling VIX from .NET really easy. If you're a .NET minded individual this stuff will make your life a lot easier, and it supports everything VIX supports, Workstation, Server as well as ESX.


Below I offer just a small bit of proof about just how easy it really is by using it in a PowerShell script I whipped together in about 20 minutes. This script is designed to work with ESX and requires the VI Toolkit (for Windows) to be installed.


The script is basically yet another example of restarting a service in a guest, which you might as well be my version of "Hello World", still I'm really excited to see how much easier it's getting to manage your guests with VMware software.


Thanks Daniel!


# Before using this sample, download the Daniel Doubrovkine's VIX

# interoperability library from

# http://www.codeproject.com/KB/library/VMWareTasks.aspx

# and change the variable below to point to it.

$vestrisDir = "C:\Vestris.VMWareTasks"


# Run a script inside a VM using VIX. This should work against ESX 3.5

# U2 or higher, as well as VMware Server and Workstation. See below

# for an example.

function Invoke-Script {

    param ($hostCred, $guestCred, $vm, $command, $args)


    $vmo = get-vm $vm

    $vmView = $vmo | get-view

    $vmx = $vmView.config.files.vmpathname


    $hostNet  = $hostCred.GetNetworkCredential()

    $guestNet = $guestCred.GetNetworkCredential()


    add-type -path "$vestrisDir\1.0\Bin\Vestris.VMWareLib.dll"

    add-type -path "$vestrisDir\1.0\Bin\Interop.VixCOM.dll"

    $myHost = new-object vestris.vmwarelib.vmwarevirtualhost

    $myHost.ConnectToVMWareVIServer("https://" +

        $defaultVIServer.Name + "/sdk", 0, $hostNet.UserName,


    $vm = $myHost.Open($vmx)

    $vm.Login($guestNet.UserName, $guestNet.Password)

    $vm.Execute($command, $args)



# Login stuff.

$ip = read-host "Enter the IP of your VC / ESX server"

write-host "Enter login and password to VC / ESX"

$hostCred = get-credential

write-host "Enter login and password to your VM"

$guestCred = get-credential


# Connect to ESX/VC

connect-viserver $ip -credential $hostCred


# This sample restarts the print spooler.

$command = "c:\windows\system32\net.exe"

$args    = "restart spooler"

Invoke-Script -hostCred $hostCred -guestCred $guestCred `

    -vm "Windows2k3SP2" -command $command -args $args

More VIX goodness, this time changing routes on Windows and Linux.

Cody Bunch over at Professional VMware has done it again, this time showing us how to add and delete routes on both Windows and Linux.

Those of us who follow Cody's blog are all wondering what tomorrow's big news is, my guess is we'll finally learn how many turtles you can fit on a rock.

You can also follow Cody on Twitter if you're in to that sort of thing.

Change a VM’s IP address with vmrun

Cody Bunch over at Professional VMware has an example that shows you how to change your IP address using vmrun. If you look carefully you'll see that despite getting an error as vmrun exits, he does manage to successfully change his IP address, so it seems to be fairly benign.

Managing VM guests using vmrun.

The easiest way to get started with VIX is by using the vmrun command which is packaged inside of VIX. It's a command-line executable that doesn't require a development environment, and is pretty easy to use once you get the hang of it.

Since I mostly use ESX, I want to take an ESX-centric view of using vmrun in this post, and I'm also going to focus on what you can do to the guest operating system within VMs. Specifically I'm going to address two use cases that I think are pretty important and useful:

  1. Installing an agent within a VM.
  2. Restarting a service on a Windows VM.

First, you'll need to install VIX 1.6.2. On Windows you'll find vmrun in %PROGRAMFILES%\VMware\VMware VIX. On Linux it should be available in your path, so you can just run it from anywhere. Next, make sure you've got an ESX 3.5 update 2 or higher server to run commands against. In addition, you'll need a VM whose operating system is running an up-to-date copy of VMware tools.

If you type vmrun with no arguments you'll get a listing of all the things vmrun can do, but I want to focus on guest operations, which are listed below.

Table 1: Guest operations supported by vmrun.

Operation Description Supported on ESX?
runProgramInGuest Run a program
in Guest OS
fileExistsInGuest Check if a file exists in Guest OS Yes
setSharedFolderState Modify a
Host-Guest shared folder
addSharedFolder Add a Host-Guest shared folder No
removeSharedFolder Remove a
Host-Guest shared folder
listProcessesInGuest List running processes in Guest OS Yes
killProcessInGuest Kill a process
in Guest OS
runScriptInGuest Run a script in Guest OS Yes
deleteFileInGuest Delete a file
in Guest OS
createDirectoryInGuest Create a directory in Guest OS Yes
deleteDirectoryInGuest Delete a
directory in Guest OS
listDirectoryInGuest List a directory in Guest OS Yes
copyFileFromHostToGuest Copy a file
from host OS to guest OS
copyFileFromGuestToHost Copy a file from guest OS to host OS Yes
renameFileInGuest Rename a file
in Guest OS
captureScreen Capture the screen of the VM to a local file No
writeVariable Write a
variable in the VM state
readVariable Read a variable in the VM state No

Use case: Installing an agent in your VM.

Installing agents is an integral part of managing large numbers of systems, whether physical or virtual. Usually you want to bake your agents into your OS image, but occasionally a new agent will come along that needs to be deployed to existing systems. This example shows how you can copy an agent from your desktop to your VM and install it. The only thing you need is an agent that can be installed without user intervention (which is the norm for agents anyway).

Please note: Commands should be entered all on one line, it's split up here just for the sake if fitting it into the blog.

Example 1: Copying an MSI file from your desktop to your VM.

vmrun -T esx -h https://esx.example.com/sdk
  -u root -p secretpw -gu user -gp userpw
copyFileFromHostToGuest "[storage1] Windows/Windows.vmx"
  "c:\program files\my agent software\agent.msi" c:\agent.msi

One thing to point out here is that, despite the command saying "From Host to Guest" the file is actually being copied from the same system where vmrun is invoked (for example your laptop or workstation).

Next we use runProgramInGuest to install the MSI.

Example 2: Invoke the MSI file we copied above.

vmrun -T esx -h https://esx.example.com/sdk
  -u root -p secretpw -gu user -gp userpw
  runProgramInGuest "[storage1] Windows/Windows.vmx"

Use case: Restarting a service on Windows.

This one's really easy so I'll just show it without explanation.

Example 3: Restart a service on Windows.

vmrun -T esx -h https://esx.example.com/sdk
  -u root -p secretpw -gu user -gp userpw
  runProgramInGuest "[storage1] Windows/Windows.vmx"
  c:\windows\system32\net.exe restart dhcp

If Linux is more your style, tweak the last part to say something like /etc/init.d/sshd restart and you'll be restarting Linux services just as easily.

How do I get the path to my VMX file?

The hardest part about dealing with vmrun when you're managing ESX is knowing where your VMX file is. To get this you have to either log in to your ESX system and locate the VMX file, or you can use the VI API. Within the VI API, the VirtualMachine object contains a data structure called VirtualMachineFileInfo. The vmPathName property of this structure tells you the path to your VMX file. Not very pretty, but over time you'll see us rolling out some more convenient ways to access the functionality VIX provides. In the meantime there's a lot of really useful stuff you can do with tools like vmrun.

VIX 1.6.2 Released!

Last week we released VIX 1.6.2, which officially adds support for remotely managing VMs on ESX servers, either directly or when accessed through vCenter (formerly known as VirtualCenter).

There's been quite a proliferation of VIX versions lately, so some comments are in order. VIX 1.6.0 and 1.6.1 managed VMware Workstation and VMware Server, respectively. These packages were distributed within the products and cannot be downloaded separately. 1.6.2 adds support for ESX / vCenter and is available for download separately.

If you're trying to make sense of all of this, here's a table that should help:



Supported Products


VIX 1.6.0

Workstation 6.5.0


VIX 1.6.1

VMware Server 2.0


VIX 1.6.2

ESX, ESXi and vCenter


VIX 1.6.3

Workstation 6.5.1

There are some important compatibility notes, as well as other helpful information in our release notes. One thing the release notes don't mention is that VIX 1.6.2 should work against VMware Server 2.0, although it is not tested very rigorously. Still it may be good to do development and testing using 1.6.2, managing a remote VMware Server system, but think twice before doing anything production related.

In the next post I'll give some examples of how you can get going with VIX quickly and easily using vmrun.