VMware Cloud Community
esloof
Expert
Expert

VI Toolkit and .net Integration

[http://www.ntpro.nl/blog/uploads/VIToolkit.jpg]

Hello gentlemen,

I have a question regarding the DLL files in the Windows VI Toolkit. Is there a possibility to address the objects from a .net programming language like Visual Basic 2005. What I can do is use the object browser from Visual Basic 2005 and look at the properties and methods, but how can you address them ? In the screen shot you see the object browser looking at the properties of the SetCDDrive object.

Regards,

Eric Sloof

0 Kudos
12 Replies
bshell
Enthusiast
Enthusiast

I can't see why not. A .NET class is a .NET class. As long as you register the DLL you should be able to access the classes it exposes.

I am not on the dev team, but I assume that is precisely what the snap-ins in the VI Toolkit do.

0 Kudos
JPatten
Enthusiast
Enthusiast

I have successfully added the DLLs from the toolkit into a VS 2005 C# application and I am able to reference them with "using VMware;" in the header of the code. Now, just have to figure out how to use it.

0 Kudos
esloof
Expert
Expert

I'm trying to setup a connection to my VC server and it looks something like this:

Imports VMware

Public Class Form1

Dim List As New VimAutomation.Commands.GetVM

Dim Connect As New VimAutomation.Commands.GetVIServer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Connect.Server(0) = "NL-NWG-TERM1"

MsgBox(Connect.Server(0))

MsgBox(Connect.CommandRuntime)

End Sub

End Class

I'm not sure about the Connect.CommandRuntime line and I have tried invoke, but it doesn't work.

0 Kudos
JPatten
Enthusiast
Enthusiast

I may be a little farther than you, but running into some powershell syntax now. Here is my code:

GetVIServer test = new GetVIServer();

test.User = "Userr";

test.Password = "PW";

test.Server[0] = "IP";

test.Port = 443;

IEnumerable<PSObject> results;

results = test.Invoke<PSObject>();

MessageBox.Show(results.ToString());

foreach (PSObject item in results)

{

PSPropertyInfo prop = (PSPropertyInfo)item.Properties["Name"];

if (prop != null)

{

MessageBox.Show(prop.Value.ToString());

}

}

It receives an error on the "foreach" line with this error: "Cmdlets derived from PSCmdlet cannot be invoked directly.")

Did some quick google searching and I was unable to find anything.

0 Kudos
JPatten
Enthusiast
Enthusiast

While I haven't quite given up with the method I had in my previous post, I decided to try pure Poweshell commands from c# and was able to get goog results. Here is one of my C# commands to stop a VM by name:

private void StopVMByName(string VMName, string VIServer, string VIPort, string User, string Password)

{

RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();

PSSnapInException snapInException = null;

rsConfig.AddPSSnapIn("VMware.VimAutomation.Core", out snapInException);

rsConfig.AddPSSnapIn("VIPowerShellCommandsSnapIn", out snapInException);

Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);

myRunSpace.Open();

Pipeline pipeLine = myRunSpace.CreatePipeline();

pipeLine.Commands.AddScript("$server = get-viserver -server " + VIServer + " -port " + VIPort + " -user " + User + " -password " + Password);

pipeLine.Commands.AddScript("Get-VM | where {$_.Name -eq \"" + VMName + "\"} | stop-vm -confirm:$false");

Collection<PSObject> commandResults = pipeLine.Invoke();

myRunSpace.Close();

}

The downside to this method so far is that I receive no error or status back from powershell. There may be a way to, but this is my first time using powershell from within c#.

0 Kudos
esloof
Expert
Expert

Finally,

I received a real True in a box ;-\0

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Config As New VMware.VimAutomation.Client20.VimClient

Config.ConnectivityService.Login("https", "192.168.178.101", "443", "root", "vmware")

MsgBox(Config.ConnectivityService.IsConnected)

End Sub

End Class

Next step :

Config.InventoryService.GetVM()..............

0 Kudos
alanrenouf
VMware Employee
VMware Employee

Hi, Im trying to use Visual Web Developer 2005 to create a website in C# which passes some powershell commands to the vmware infrastructure and at the moment just outputs the results to the webpage.

I stumbled across this post and it looks like you guys are doing similar things (in a roundabout way)

Im new to C# but have produced a few powershell scripts (http://teckinfo.blogspot.com/2008/07/i-have-now-updated-my-first-powershell.html) and just wondered if you could give me some starting points on what to do, what references are you using ?

Is there an easy way to output the get-vm results to a webpage from within C# ?

Thanks

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
halr9000
Commander
Commander

You may want to look at PowerShellASP, but according to , there is a bug in the toolkit which prevents us from using the VITK from poshasp. Hopefully it'll get fixed soon.

Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
alanrenouf
VMware Employee
VMware Employee

Ha ha, thanks, you just pointed me back to my own post Smiley Happy I was pursuing other avenues !

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
halr9000
Commander
Commander

It has been that kind of day. Smiley Happy

Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
admin
Immortal
Immortal

Alan you may want to look at using PowerShell runspaces in your program

Here's a very basic example. As always, apologies for the bad formatting, I've also attached the program to this message.

As you see below, you can basically just plug any script you want in.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using VMware.VimAutomation.Client20;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
PSSnapInException warn;
Runspace run;

run = RunspaceFactory.CreateRunspace();

run.RunspaceConfiguration.AddPSSnapIn("VMware.VimAutomation.Core", out warn);
run.Open();

Collection<PSObject> result;

Pipeline p = run.CreatePipeline();
using (p) {
p.Commands.AddScript("Get-VIServer <IP> -user <User> -pass <Password>");
p.Commands.AddScript("Get-VM");
result = p.Invoke();
} foreach (PSObject o in result) {
VirtualMachineImpl vm = (VirtualMachineImpl)o.BaseObject;
System.Console.WriteLine(vm.Name);
}
}
}
}

0 Kudos
alanrenouf
VMware Employee
VMware Employee

Thanks, looks like the kind of thing I am after but as I said I am new to Visual web dev and C# so please be kind....

I have added the code to my form load as below and added a label for the output.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using VMware.VimAutomation.Client20;
public partial class Default : System.Web.UI.Page
{
protected void Page
Load(object sender, EventArgs e)
{
PSSnapInException warn;
Runspace run;
run = RunspaceFactory.CreateRunspace();
run.RunspaceConfiguration.AddPSSnapIn("VMware.VimAutomation.Core", out warn);
run.Open();
Collection<PSObject> result;
Pipeline p = run.CreatePipeline();
using (p)
{
p.Commands.AddScript("connect-VIServer myserver -user myuser -pass mypass");
p.Commands.AddScript("Get-VM");
result = p.Invoke();
} foreach (PSObject o in result)
{
VirtualMachineImpl vm = (VirtualMachineImpl)o.BaseObject;
Label1.Text= vm.Name"\r\n";
}
}
}

I now get the following error: exePath must be specified when not running inside a stand alone exe.

Which incedently is the same error i get when using PowerShellASPas per here...[http://communities.vmware.com/message/992849#992849|m-992849]

(I also changed the command to connect-viserver as am now using the released version of VI toolkit)

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos