VMware Cloud Community
yanivamrami
Enthusiast
Enthusiast

C# and VI Toolkit

Hi,

I have this code that starts Windows PowerShell. It fails since Windows PS doesn't have the Connect-VIServer command.

How can I start the VI Toolkit instead or on top of Windows PS.

Runspace runspace = RunspaceFactory.CreateRunspace();

runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();

pipeline.Commands.AddScript("Connect-VIServer -Server " + vcserver);

Collection results = pipeline.Invoke();

runspace.Close();

Thanks,

Yaniv

Please rate helpful / correct answers. Thank you
0 Kudos
4 Replies
Brett_Yeagley
Contributor
Contributor

I've only played with the toolkit briefly so far, but ran into some trouble getting things to start correctly until I added the following line to the beginning of my powershell scripts:

Add-PSSnapin VMware.VimAutomation.Core

After that I could call the connect-VIserver function. I'm not sure how this plays into your use of C# with powershell, but hopefully it may give you some other ideas to try.

I hope this helps to some degree.

0 Kudos
JPatten
Enthusiast
Enthusiast

The way I usually get around this is to use a PowerShell profile to load all of the snap-ins I want without having to re-type the commands every time I open PowerShell.

There are various locations for the profile, based on who the profile is for (either user or system wide). Do a quick google search on PowerShell profiles and you will get lots of information.

yanivamrami
Enthusiast
Enthusiast

I managed to solve this one -

I've created a configuration that includes the PS snap in:

// create PS runspace config.

RunspaceConfiguration config = RunspaceConfiguration.Create();

PSSnapInException snapInException = null;

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

I get other error but I'll open a new thread.

Thank you,

Yaniv

Please rate helpful / correct answers. Thank you

Please rate helpful / correct answers. Thank you
0 Kudos
tmedlin
Contributor
Contributor

Hi there. Were you ever able to get config.AddPSSnapIn("VMware.VimAutomation.Core", out snapInException) working in your C# asp page? I am running into the same issue. Thanks!

Tim

0 Kudos