VMware

More VIX goodness, this time changing routes on Windows and Linux. | Main | PowerWF: Build workflows that use VIX and more!

December 23, 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,

        $hostNet.Password)

    $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

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d8341c328153ef010536966c08970c

Listed below are links to weblogs that reference Call VIX more easily from .NET (plus PowerShell goodness):

Comments

halr9000

It should be noted that this script requires PowerShell V2 CTP3 as opposed to the currently stable version 1.0.

Post a comment

If you have a TypeKey or TypePad account, please Sign In.