In this article, we will discuss how to prepare Visual Studio to perform various VMware tasks. VMware PowerCLI is a Windows PowerShell interface for managing and automating various vSphere tasks. Using VMware PowerCLI cmdlets in Visual Studio we can create, customize, or manage vSphere inventory objects.
a. Preparing Visual Studio to perform VMware tasks by using VMware vSphere PowerCLI cmdlets within the C# code:In order for powerCLI to interact with .NET, we need to call the assemblies and reference these in our .NET code. To do this, under the Solution Explorer, right click on References and click Add Reference.
Next, click on the browse tab and locate the System.Management.Automation.dll file under C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0 and click OK.
Now that we have added reference to these assemblies, let us go ahead and add these to the beginning of our C# code by writing:
b. Preparing Visual Studio to perform VMware Tasks by Using the VMware VIX API:In order to connect to a local VMware Workstation, local or remote VMware Server or a remote ESX server, and to perform VMware tasks, we need to add reference to Vestris.VMWareLib.dll available here.
Again, Under the Solution Explorer, right click on References and click Add Reference. Next, click on the browse tab and locate Vestris.VMWareLib.dll under the 'Bin' sub folder of this folder that we just downloaded.
Then add it to our C# code by including the following at the beginning of our C# code:
At this point, we have included references to assemblies for performing VMware tasks using Visual Studio via powerCLI cmdlets within C# and via the VIX API.
Now, let us demonstrate an example of performing VMware tasks using Visual Studio via powerCLI cmdlets within C#.
After having added reference to the assemblies and included the libraries, we will create the following function:
static void Execute_vCenter_Tasks(string vCenterIp, string vCenterUsername, string vCenterPassword)
The function Execute_vCenter_Tasks above takes in as input: the IP of the vCenter, the username of the vCenter and the password for the vCenter
In this function we, first instantiate a PowerShell object
var shell = PowerShell.Create();
Next, we add the VMware plug-in to issue PowerCli commands. We have to add a semi-colon at the end of each command in order for PowerShell to issue a new line
string PsCmd = "add-pssnapin VMware.VimAutomation.Core; $vCenterServer = '" + vCenterIp + "';$vCenterAdmin = '" + vCenterUsername + "' ;$vCenterPassword = '" + vCenterPassword + "';" + System.Environment.NewLine;
System.Environment.NewLine is like carriage return (return to the beginning of the current line without advancing downward)
We then issue the connection string:
PsCmd = PsCmd + "$VIServer = Connect-VIServer -Server $vCenterServer -User $vCenterAdmin -Password $vCenterPassword;" + System.Environment.NewLine;
Finally, we add the script and execute it.
Now to run this, call the Execute_vCenter_Tasks("", "", "") method in the Main method and pass the required values within the quotes (i.e. the IP, username, password)
static void Main(string args)
Execute_vCenter_Tasks("", "", "");
Thus, we have studied how to prepare Visual studio to perform VMware tasks by using PowerCLI cmdlets within the C# code and how to prepare it to perform VMware tasks by using the VMware VIX API.
In the posts to follow, we will learn, with examples, how we can automate a wide range of VMware tasks using this approach.