The main idea is: revert to a well-known state and do the sanity-tests from it. So far for the idea, now for the reality. The first thing needed is the WM Server from VMWare. After installation I've still got troubles with the COM object it uses.
Working with the local workstation was by far easier, and the solution was tested on it.
Step 1: connect to the VM.
I've downloaded the VMWareTasks for the job, it's easy to use and removes the hassle of finding when/what went a miss with the VM.
The code for connecting to a VM is simple:
VMWareVirtualHost virtualHost = new VMWareVirtualHost();
virtualHost.ConnectToVMWareWorkstation();
VMWareVirtualMachine virtualMachine = virtualHost.Open(VMX_PATH);
virtualMachine.PowerOn();
Now we have a machine turned on and running.
Step 2: Revert to snapshot
VMWareSnapshot foundSnapshot = virtualMachine.Snapshots.GetNamedSnapshot(SNAPSHOT_NAME);
if (foundSnapshot == null)
return;
foundSnapshot.RevertToSnapshot(TIMEOUT);
virtualMachine.WaitForToolsInGuest();
The last line is a bit weird, but very important. The machine must be fully running in order to work. As we all know, M$ do a fake readiness state while loading necessary things in the back (and preventing work on the way).
Step 3: Login and run things
virtualMachine.LoginInGuest(VM_USERNAME, VM_PASSWORD, 8, TIMEOUT);
VIX_LOGIN_IN_GUEST_REQUIRE_INTERACTIVE_ENVIRONMENT = 8
Create needed folders
foreach (string dir in Dirs)
{
if (!virtualMachine.DirectoryExistsInGuest(dir))
{
virtualMachine.CreateDirectoryInGuest(dir);
Console.WriteLine("Created " + dir);
}
}
Copy files
foreach (KeyValuePair
{
CopyFileToMachine(file.Key, file.Value, virtualMachine);
}
private static void CopyFileToMachine(string source, string target, VMWareVirtualMachine virtualMachine)
{
if (virtualMachine.FileExistsInGuest(target))
{
virtualMachine.DeleteFileFromGuest(target);
}
virtualMachine.CopyFileFromHostToGuest(source, target);
}
Run programs
foreach (KeyValuePair
{
virtualMachine.RunProgramInGuest(prog.Key, prog.Value,
}
Step 4: Get the output back & turn the machine off
foreach (string op_file in ReturnedFiles)
{
CopyFileFromMachine(op_file, virtualMachine);
}
private static void CopyFileFromMachine(string source, VMWareVirtualMachine virtualMachine)
{
if (virtualMachine.FileExistsInGuest(source))
{
virtualMachine.CopyFileFromGuestToHost(source, source.Substring(source.LastIndexOf('\\') + 1));
}
}
virtualMachine.PowerOff();
That's all, really.
It's worthy to mention there are 4 different types of VMs, but it's important only when initiating the connection, afterwards it's the same.
Happy VMing