Exemplo n.º 1
0
        protected void InvokeKvpOperation(IDictionary <String, Object> args, IDictionary <String, Object> data, String operationName)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (data.Count() == 0)
            {
                return;
            }

            var kvpItems = ToKvpItems(data);

            using (var vmMgmt = WMIHelper.GetFirstInstance(WMI_Namespace, WMI_Class_Msvm_VirtualSystemManagementService))
                using (var vm = WMIHelper.QueryFirstInstacne(WMI_Namespace, WMI_Class_Msvm_ComputerSystem, String.Format("Name='{0}'", args["VirtualMachineName"])))
                {
                    if (batchMode)
                    {
                        InvokeKvpOperation(vmMgmt, vm, operationName, kvpItems);
                    }
                    else
                    {
                        foreach (var kvpItem in kvpItems)
                        {
                            InvokeKvpOperation(vmMgmt, vm, operationName, new String[] { kvpItem });
                        }
                    }
                }
        }
Exemplo n.º 2
0
 protected static String ToKvpItem(KeyValuePair <String, Object> kvp)
 {
     using (var kvpItem = WMIHelper.CreateInstance(WMI_Namespace, WMI_Class_Msvm_KvpExchangeDataItem))
     {
         kvpItem["Name"]   = kvp.Key;
         kvpItem["Data"]   = kvp.Value;
         kvpItem["Source"] = 0;
         return(((ManagementBaseObject)kvpItem).GetText(TextFormat.CimDtd20));
     }
 }
Exemplo n.º 3
0
        protected void InvokeKvpOperation(ManagementObject vmMgmt, ManagementObject vm, String operationName, String[] kvpItems)
        {
            using (var inParams = vmMgmt.GetMethodParameters(operationName))
            {
                inParams["DataItems"]    = kvpItems;
                inParams["TargetSystem"] = vm;

                using (var outParams = vmMgmt.InvokeMethod(operationName, inParams, null))
                {
                    //Delay a small mount of time to avoid overwhelm the KVP VSC in the guest.
                    Task.Delay(this.writeInterval);
                    WMIHelper.WaitForAsyncJob(outParams, WMI_Namespace);
                }
            }
        }
Exemplo n.º 4
0
        public IList <IDictionary <String, Object> > GetSupportedVM()
        {
            using (var vms = WMIHelper.QueryInstacnes(WMI_Namespace, WMI_Class_Msvm_ComputerSystem, "EnabledState = 2 and Caption='Virtual Machine'"))
            {
                var tasks = new List <Task <IDictionary <String, Object> > >();
                foreach (var vm in vms.Cast <ManagementObject>())
                {
                    tasks.Add(Task.Run <IDictionary <String, Object> >(() =>
                    {
                        var objs = vm.GetRelated(WMI_Class_Msvm_KvpExchangeComponent);
                        foreach (var obj in objs)
                        {
                            var kvps = obj.Properties["GuestExchangeItems"].Value as String[];

                            if (kvps.Any(kvp => kvp.IndexOf(this.SupportedVMKey) >= 0))
                            {
                                var vmdata = new Dictionary <String, Object>();
                                vmdata.Add("VirtualMachineName", vm.Properties["Name"].Value);
                                vmdata.Add("VirtualMachineElementName", vm.Properties["ElementName"].Value);
                                vmdata.Add("VirtualMachinePath", vm.Path);
                                vmdata.Add("GuestExchangeItems", kvps);
                                return(vmdata);
                            }
                        }
                        return(null);
                    }));
                }
                try
                {
                    var task = Task.WhenAll <IDictionary <String, Object> >(tasks);
                    task.Wait();
                    return(task.Result.Where(x => x != null).ToList());
                }
                catch (AggregateException e)
                {
                    foreach (var inner in e.InnerExceptions)
                    {
                        Logger.Error(inner);
                        throw inner;
                    }
                    throw;
                }
            }
        }