Search code examples
c#vmwarevspherepowercli

C# VMWare PowerCli 6.0 Start-VM from a folder


I'm in advance sorry if this question had already been answered, I didn't find anything about it.

I'm working on PowerCLi 6.0 for vSphere 6.

I have all permissions for a specific folder (called "XXXFolder") but not for the whole host. In this folder I'm trying to automate the Power Off/On operations with C#.

VMware.Vim.VimClientImpl myClient = new VimClientImpl();
myClient.Connect("https://" + hostName + ":443/sdk");
myClient.Login(userName, passWord);
NameValueCollection propertyFilter = new NameValueCollection();
propertyFilter.Add("name", "VMName");
VMware.Vim.VirtualMachine myVM = (VirtualMachine)myClient.FindEntityView(typeof(VirtualMachine), null, propertyFilter, null);   

The powerOff operation works fine this way:

myVM.PowerOffVM();

But for the PowerOn method, it takes as an argument the host the VM seats on it. So something like :

myVM.PowerOn(myVM.Runtime.Host);

But because I don't have permissions on the host (only on the specific folder the VM is in I remind), I get out in exception with the following message :

"Permission to perform this operation was denied."

Note that with the following Powershell command works fine :

Start-VM -VM $myVM

Does someone know in C#, how to Power On a VM which is located in a specific folder ?

Thanks a lot for the help.


Solution

  • Thanks to Gregu, I resolved the issue. This is the C# code for Powering ON a VM located in a folder. (PowerOnMultiVM_Task doesn't need permissions on whole host)

    Please note that in this following code, I removed :

    • conditions (to check objects are not empty, and state of VM is powered off),
    • error handling (Try/Catch)
    • Sync (wait for task)

    in order this portion of code to be more understandable.

    AGAIN, Thanks to @Gregu

        // dummy values examples
        string hostName = "10.0.0.20"; 
        string userName = "myUser"; 
        string sFolderName = "myFolderName"; 
        string sMyDataCenter = "myDataCenterName"; 
        string sMyVM = "sMyVMNAme"; 
        
        List<VMware.Vim.ManagedObjectReference> MOF = new List<VMware.Vim.ManagedObjectReference>();
        
        // Create Client Object
        VMware.Vim.VimClientImpl myClient = new VimClientImpl();
        
        // Connect to host
        myClient.Connect("https://" + hostName + ":443/sdk");
        myClient.Login(userName, passWord);
        
        // Create a Folder object which has the name defined
        NameValueCollection propertyFilterFolder = new NameValueCollection();
        propertyFilterFolder.Add("name", sFolderName);
        VMware.Vim.Folder myFolder = (Folder)myClient.FindEntityView(typeof(Folder), null, propertyFilterFolder, null);
        
        // Create a DataCenter object which has the name defined
        NameValueCollection propertyFilterDC = new NameValueCollection();
        propertyFilterDC.Add("name", sMyDataCenter);
        VMware.Vim.Datacenter myDC = (Datacenter)myClient.FindEntityView(typeof(Datacenter), null, propertyFilterDC, null);
        
        // Create a VM object which has the name defined (From the specified folder)
        NameValueCollection propertyFilter = new NameValueCollection();
        propertyFilter.Add("name", sMyVM);
        VMware.Vim.VirtualMachine myVMF = (VirtualMachine)myFolder.Client.FindEntityView(typeof(VirtualMachine), null, propertyFilter, null);
        
        MOF.Add(myVMF.MoRef);
        
        // Calling the Start VM method
        myDC.PowerOnMultiVM_Task(MOF.ToArray(), null);