Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a starter in c# and I have a small knowledge. I have made a windows application on c# that shutdwon windows servers in my network remotely. I have a v-center server that hosts two Virtual Hosts. my issue is how to shutdown the host:
I tried to write a code to shutdwon the hosts using VIX API in c#, I couldn't find a code to shutdown. All I get is to disconnect them.

Here is my code


C#
try
{
    VMWareVirtualHost host = new VMWareVirtualHost();

    host.ConnectToVMWareVIServer("172.16.1.72", "root","123456");

    host.Disconnect();

    IVMWareVirtualMachine machine = new VMWareVirtualMachine();

    machine = host.Open("[172.16.1.72] Kerio contarol.vmxf");

    machine.ShutdownGuest();

    if (machine.IsRunning == true)
    {
        MessageBox.Show("Machine is running");
    }
    else
    {
        MessageBox.Show("Machine is not rinning");
    }

}
catch(Exception ex)
{
    MessageBox.Show(ex.Message);
}
Posted
Updated 11-Apr-15 20:18pm
v2
Comments
Joan Magnet 7-Apr-15 11:13am    
Have you tried to do not call host.Disconnect() just after calling host.ConnectToVMWareVIServer(---)?

There is an example in CodeProject.

http://www.codeproject.com/Articles/31961/Automating-VMWare-Tasks-in-C-with-the-VIX-API
Member 10685840 8-Apr-15 4:43am    
I have looked at that link before, but it does not illustrate how to connect to a virtual machine in a network remotely neither how to shutdown the host.
Member 10685840 8-Apr-15 5:50am    
I have solved my second issue which i could connect to virtual machine as I was adding the wrong virtual machine configuration file which is ("[172.16.1.72] Kerio contarol.vmxf"); while the right path is ([datastore1] Kerio contarol/Kerio contarol.vmx).
Joan Magnet 8-Apr-15 7:24am    
Powering Off a Virtual Machine

Connect to the host on which the virtual machine is located. Refer to Connecting to a Host.
Get a handle to the virtual machine. Refer to Getting a Handle to a Virtual Machine.
Use the virtual machine handle in a call to VixVM_PowerOff().
Example 3-12.

VixError err = VIX_OK;
VixHandle jobHandle = VIX_INVALID_HANDLE;

// Power off the virtual machine.
jobHandle = VixVM_PowerOff(vmHandle,
0, // powerOffOptions,
VIX_INVALID_HANDLE, // propertyListHandle,
NULL, // callbackProc,
NULL); // clientData
err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE);
if (VIX_OK != err) {
// Handle the error...
goto abort;
}
Vix_ReleaseHandle(jobHandle);
jobHandle = VIX_INVALID_HANDLE;
Member 10685840 8-Apr-15 8:49am    
Thank you for your replay, but I have solved the issue on how to shutdown the virtual machine.
Note: powering off the machine is not safe as shutting down.
my code was right, except that i mistaken the configuration path. the right code is as follow:

VMWareVirtualHost host = new VMWareVirtualHost();
host.ConnectToVMWareVIServer("172.16.1.72", "root","123456");
IVMWareVirtualMachine machine = new VMWareVirtualMachine();
machine = host.Open("[datastore1] Kerio contarol/Kerio contarol.vmx");
machine.ShutdownGuest();
-------------------------------------
The current problem is how to shutdown the host itself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900