Hi guyz, i got the below code from MSDN regarding removal of resources from vm. The below example is for removing some nic settings.
What i need is to remove or Eject the Floppy disk which i have earlier attached from a virtual machine.
I followed the below procedure, but every time it gives me "Failed to remove resources from virtual machine"
Can any plz give working c# code to eject floppy drive(not through Powershell)
using System;
using System.Management;
namespace HyperVSamples
{
class RemoveVirtualSystemResourcesClass
{
ManagementObject virtualSystemService = null;
ManagementScope scope = null;
RemoveVirtualSystemResourcesClass()
{
scope = new ManagementScope(@"root\virtualization", null);
virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");
}
ManagementObject GetVirtualSystemEthernetPortSetting(ManagementObject vmSetting, string SyntheticNicElementName)
{
ManagementObject SyntheticNicSetting = null;
ManagementObjectCollection SyntheticNicSettings = vmSetting.GetRelated
(
"Msvm_SyntheticEthernetPortSettingData","Msvm_VirtualSystemSettingDataComponent",
null,
null,"PartComponent","GroupComponent",
false,
null
);
foreach (ManagementObject instance in SyntheticNicSettings)
{
if (instance["ElementName"].ToString().ToLower() == SyntheticNicElementName.ToLower())
{
SyntheticNicSetting = instance;
break;
}
}
return SyntheticNicSetting;
}
void RemoveVirtualSystemResources(string vmName, string syntheticNicName)
{
ManagementObject vm = Utility.GetTargetComputer(vmName, scope);
ManagementObject vmSetting = Utility.GetVirtualSystemSettingData(vm);
ManagementObject syntheticNicPortSetting = GetVirtualSystemEthernetPortSetting(vmSetting, syntheticNicName);
ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("RemoveVirtualSystemResources");
inParams["TargetSystem"] = vm.Path.Path;
string[] resources = new string[1];
resources[0] = syntheticNicPortSetting.Path.Path;
inParams["ResourcesettingData"] = resources;
ManagementBaseObject outParams = virtualSystemService.InvokeMethod("RemoveVirtualSystemResources", inParams, null);
if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
{
if (Utility.JobCompleted(outParams, scope))
{
Console.WriteLine("SyntheticNic was removed successfully.");
}
else
{
Console.WriteLine("Failed to remove SyntheticNic from VM");
}
}
else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
{
Console.WriteLine("SyntheticNic was removed successfully.");
}
else
{
Console.WriteLine("Remove SyntheticNic failed with error : {0}", outParams["ReturnValue"]);
}
inParams.Dispose();
outParams.Dispose();
vm.Dispose();
vmSetting.Dispose();
syntheticNicPortSetting.Dispose();
}
void Close()
{
this.virtualSystemService.Dispose();
}
static void Main(string[] args)
{
if (args != null && args.Length != 2)
{
Console.WriteLine("Usage: RemoveVirtualSystemResources vmName syntheticNicName");
Console.WriteLine("Example: RemoveVirtualSystemResources MyFirstVM myStaticNic");
return;
}
RemoveVirtualSystemResourcesClass removeResource = new RemoveVirtualSystemResourcesClass();
removeResource.RemoveVirtualSystemResources(args[0], args[1]);
removeResource.Close();
}
}
}
Similar to the above example, i have used the getfloppy setting instead of nic setting, but here it always gives me, count =0 in "cSettings" object, so the foreach loop doesn't even iterates once. I have simple replaced "Msvm_SyntheticEthernetPortSettingData" with "Msvm_DisketteDrive"... i know that im definitely doing something wrong here... can any one please help me here
public static ManagementObject GetFloppySetting(ManagementObject vmSetting, string floppyElementName)
{
ManagementObject floppySetting = null;
ManagementObjectCollection cSettings = vmSetting.GetRelated
("Msvm_DisketteDrive","Msvm_VirtualSystemSettingDataComponent",
null,
null,"PartComponent","GroupComponent",
false,
null
);
foreach (ManagementObject instance in SyntheticNicSettings)
{
if (instance["ElementName"].ToString().ToLower() == floppyElementName.ToLower())
{
floppySetting = instance;
break;
}
}
return floppySetting;
}