コード例 #1
0
        public string CreateVirtualSystemSnapshot(string vmName, vm_control vmctrl)
        {
            ManagementBaseObject result = null;
            string       ret            = "";
            const string Job            = "Job";
            const string JobState       = "JobState";


            if (scope == null)
            {
                scope = getRemoteScope();
            }

            ManagementObject virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");

            ManagementObject vm = Utility.GetTargetComputer(vmName, scope);

            ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("CreateVirtualSystemSnapshot");

            inParams["SourceSystem"] = vm.Path.Path;

            ManagementBaseObject outParams = virtualSystemService.InvokeMethod("CreateVirtualSystemSnapshot", inParams, null);

            if (Utility.JobCompleted(outParams, scope, vmctrl))
            {
                if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
                {
                    if (Utility.JobCompleted(outParams, scope, null))
                    {
                        ret = ("Snapshot was created successfully.");
                    }
                    else
                    {
                        ret = ("Failed to create snapshot VM");
                    }
                }
                else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
                {
                    ret = ("Snapshot was created successfully.");
                }
                else
                {
                    ret = String.Format("Create virtual system snapshot failed with error {0}", outParams["ReturnValue"].ToString());
                }
            }
            inParams.Dispose();
            outParams.Dispose();
            vm.Dispose();
            virtualSystemService.Dispose();

            return(ret);
        }
コード例 #2
0
ファイル: VM.cs プロジェクト: riccione83/Hyper-V-manager
        public string CreateVirtualSystemSnapshot(string vmName,vm_control vmctrl)
        {
            ManagementBaseObject result=null;
            string ret="";
            const string Job = "Job";
            const string JobState = "JobState";

            if (scope == null) scope = getRemoteScope();

            ManagementObject virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");

            ManagementObject vm = Utility.GetTargetComputer(vmName, scope);

            ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("CreateVirtualSystemSnapshot");
            inParams["SourceSystem"] = vm.Path.Path;

            ManagementBaseObject outParams = virtualSystemService.InvokeMethod("CreateVirtualSystemSnapshot", inParams, null);

            if (Utility.JobCompleted(outParams, scope, vmctrl))
            {

                if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
                {
                    if (Utility.JobCompleted(outParams, scope,null))
                    {
                        ret = ("Snapshot was created successfully.");

                    }
                    else
                    {
                        ret = ("Failed to create snapshot VM");
                    }
                }
                else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
                {
                    ret = ("Snapshot was created successfully.");
                }
                else
                {
                    ret = String.Format("Create virtual system snapshot failed with error {0}", outParams["ReturnValue"].ToString());
                }
            }
            inParams.Dispose();
            outParams.Dispose();
            vm.Dispose();
            virtualSystemService.Dispose();

            return ret;
        }
コード例 #3
0
        public void setControl(vm_control ct)
        {
            if (this.frm.InvokeRequired)
            {
                SetControlCAllback stc = new SetControlCAllback(setControl);
                frm.Invoke(stc, new object[] { ct });
            }
            else
            {
                bool       Fnd = false;
                vm_control pnl = null;
                for (int i = 0; i < frm.UI_VM_CONTAINER.Controls.Count; i++)
                //foreach (System.Windows.Forms.Control pnl in frm.UI_VM_CONTAINER.Controls)   //search between elements in container
                {
                    if (frm.UI_VM_CONTAINER.Controls[i] is vm_control)
                    {
                        pnl = (vm_control)frm.UI_VM_CONTAINER.Controls[i];
                        if (ct.lblName.Text == ((vm_control)pnl).lblName.Text)
                        {
                            ((vm_control)pnl).lblName.Text          = ct.lblName.Text;
                            ((vm_control)pnl).lblDescription.Text   = ct.lblDescription.Text;
                            ((vm_control)pnl).lblOS.Text            = ct.lblOS.Text;
                            ((vm_control)pnl).lblProcessorLoad.Text = ct.lblProcessorLoad.Text;
                            ((vm_control)pnl).lblState.Text         = ct.lblState.Text;
                            ((vm_control)pnl).imgVM.Image           = ct.imgVM.Image;
                            Fnd = true;
                        }
                    }
                }

                if (!Fnd)
                {
                    if (frm.imgWait.Visible == true)
                    {
                        frm.imgWait.Dispose();
                    }

                    frm.UI_VM_CONTAINER.Controls.Add(ct);
                }
            }
        }
コード例 #4
0
        public void getRemoteVM()
        {
            IN_UPGRADE = true;

            ObjectQuery queryObj = new ObjectQuery("SELECT * FROM Msvm_ComputerSystem");

            if (scope == null)
            {
                scope = getRemoteScope();
            }

            if (scope != null)
            {
                ManagementObjectSearcher   vmSearcher   = new ManagementObjectSearcher(scope, queryObj);
                ManagementObjectCollection vmCollection = vmSearcher.Get();
                try
                {
                    foreach (ManagementObject vm in vmCollection)
                    {
                        vm_control ctrl = new vm_control();
                        ctrl.frm                 = frm;
                        ctrl.lblName.Text        = vm["ElementName"].ToString();
                        ctrl.lblState.Text       = "[" + getState(vm["EnabledState"].ToString()) + "]";
                        ctrl.lblDescription.Text = vm["Description"].ToString();
                        ctrl.imgVM.Image         = GetVirtualSystemThumbnailImage(vm["ElementName"].ToString());
                        Dictionary <string, string> infos = getInfo(vm["ElementName"].ToString());
                        if (infos.Count > 0)
                        {
                            if (infos.ContainsKey("106"))
                            {
                                ctrl.lblOS.Text = "Sistema: " + infos["106"].ToString();
                            }
                            else
                            {
                                ctrl.lblOS.Text = "Sistema: na";
                            }
                            if (infos.ContainsKey("101"))
                            {
                                ctrl.lblProcessorLoad.Text = "CPU: " + infos["101"].ToString() + "%";
                            }
                            else
                            {
                                ctrl.lblProcessorLoad.Text = "CPU: na";
                            }
                        }
                        else
                        {
                            ctrl.lblOS.Text            = "Sistema: na";
                            ctrl.lblProcessorLoad.Text = "CPU: na";
                        }

                        this.setControl(ctrl);

                        IN_UPGRADE = false;
                        //  frm.UI_VM_CONTAINER.Controls.Add(ctrl);
                    }
                }
                catch
                {
                    frmMachine opt = new frmMachine();
                    opt.ShowDialog();
                }
            }
            else
            {
                frmMachine opt = new frmMachine();
                opt.ShowDialog();
            }
        }
コード例 #5
0
        public void doAction(string vmName, string action, vm_control vmctrl)
        {
            const int Enabled   = 2;
            const int Disabled  = 3;
            const int Reboot    = 10;
            const int Reset     = 11;
            const int Paused    = 32768;
            const int Suspended = 32769;

            if (scope == null)
            {
                scope = getRemoteScope();
            }

            ManagementObject vm = Utility.GetTargetComputer(vmName, scope);

            if (null == vm)
            {
                throw new ArgumentException(
                          string.Format(
                              "The virtual machine '{0}' could not be found.",
                              vmName));
            }

            ManagementBaseObject inParams = vm.GetMethodParameters("RequestStateChange");

            if (action.ToLower() == "start")
            {
                inParams["RequestedState"] = Enabled;
            }
            else if (action.ToLower() == "stop")
            {
                inParams["RequestedState"] = Disabled;
            }
            else if (action.ToLower() == "reboot")
            {
                inParams["RequestedState"] = Reboot;
            }
            else if (action.ToLower() == "reset")
            {
                inParams["RequestedState"] = Reset;
            }
            else if (action.ToLower() == "paused")
            {
                inParams["RequestedState"] = Paused;
            }
            else if (action.ToLower() == "suspend")
            {
                inParams["RequestedState"] = Suspended;
            }
            else
            {
                throw new Exception("Wrong action is specified");
            }

            ManagementBaseObject outParams = vm.InvokeMethod("RequestStateChange", inParams, null);

            if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
            {
                if (Utility.JobCompleted(outParams, scope, vmctrl))
                {
                    Console.WriteLine("{0} state was changed successfully.", vmName);
                }
                else
                {
                    Console.WriteLine("Failed to change virtual system state");
                }
            }
            else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
            {
                Console.WriteLine("{0} state was changed successfully.", vmName);
            }
            else
            {
                Console.WriteLine("Change virtual system state failed with error {0}", outParams["ReturnValue"]);
            }
        }
コード例 #6
0
        public static bool JobCompleted(ManagementBaseObject outParams, ManagementScope scope, vm_control vmctrl)
        {
            bool   jobCompleted = true;
            string old          = "";
            //Retrieve msvc_StorageJob path. This is a full wmi path
            string           JobPath = (string)outParams["Job"];
            ManagementObject Job     = new ManagementObject(scope, new ManagementPath(JobPath), null);

            //Try to get storage job information
            if (vmctrl != null)
            {
                old = vmctrl.lblState.Text;
            }
            Job.Get();
            while ((UInt16)Job["JobState"] == JobState.Starting ||
                   (UInt16)Job["JobState"] == JobState.Running)
            {
                //Console.WriteLine("In progress... {0}% completed.", Job["PercentComplete"]);
                if (vmctrl != null)
                {
                    vmctrl.lblState.Text = String.Format("In progress... {0}% completed.", Job["PercentComplete"]);
                    vmctrl.Refresh();
                }
                System.Threading.Thread.Sleep(1000);
                Job.Get();
            }
            if (vmctrl != null)
            {
                vmctrl.lblState.Text = old;
            }
            //Figure out if job failed
            UInt16 jobState = (UInt16)Job["JobState"];

            if (jobState != JobState.Completed)
            {
                UInt16 jobErrorCode = (UInt16)Job["ErrorCode"];
                Console.WriteLine("Error Code:{0}", jobErrorCode);
                Console.WriteLine("ErrorDescription: {0}", (string)Job["ErrorDescription"]);
                jobCompleted = false;
            }
            return(jobCompleted);
        }
コード例 #7
0
ファイル: VM.cs プロジェクト: riccione83/Hyper-V-manager
        public void setControl(vm_control ct)
        {
            if (this.frm.InvokeRequired)
            {
                SetControlCAllback stc = new SetControlCAllback(setControl);
                frm.Invoke(stc, new object[] { ct });
            }
            else
            {
                bool Fnd = false;
                vm_control pnl = null;
                for(int i=0;i< frm.UI_VM_CONTAINER.Controls.Count;i++)
                //foreach (System.Windows.Forms.Control pnl in frm.UI_VM_CONTAINER.Controls)   //search between elements in container
                {
                    if (frm.UI_VM_CONTAINER.Controls[i] is vm_control)
                    {
                        pnl = (vm_control)frm.UI_VM_CONTAINER.Controls[i];
                        if (ct.lblName.Text == ((vm_control)pnl).lblName.Text)
                        {
                            ((vm_control)pnl).lblName.Text = ct.lblName.Text;
                            ((vm_control)pnl).lblDescription.Text = ct.lblDescription.Text;
                            ((vm_control)pnl).lblOS.Text = ct.lblOS.Text;
                            ((vm_control)pnl).lblProcessorLoad.Text = ct.lblProcessorLoad.Text;
                            ((vm_control)pnl).lblState.Text = ct.lblState.Text;
                            ((vm_control)pnl).imgVM.Image = ct.imgVM.Image;
                            Fnd = true;
                        }
                    }
                }

                if (!Fnd)
                {
                    if (frm.imgWait.Visible == true)
                        frm.imgWait.Dispose();

                    frm.UI_VM_CONTAINER.Controls.Add(ct);
                }
            }
        }
コード例 #8
0
ファイル: VM.cs プロジェクト: riccione83/Hyper-V-manager
        public void getRemoteVM()
        {
            IN_UPGRADE = true;

            ObjectQuery queryObj = new ObjectQuery("SELECT * FROM Msvm_ComputerSystem");

            if (scope == null) scope = getRemoteScope();

            if (scope != null)
            {
                ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(scope, queryObj);
                ManagementObjectCollection vmCollection = vmSearcher.Get();
                try
                {
                    foreach (ManagementObject vm in vmCollection)
                    {
                        vm_control ctrl = new vm_control();
                        ctrl.frm = frm;
                        ctrl.lblName.Text = vm["ElementName"].ToString();
                        ctrl.lblState.Text = "[" + getState(vm["EnabledState"].ToString()) + "]";
                        ctrl.lblDescription.Text = vm["Description"].ToString();
                        ctrl.imgVM.Image = GetVirtualSystemThumbnailImage(vm["ElementName"].ToString());
                        Dictionary<string, string> infos = getInfo(vm["ElementName"].ToString());
                        if (infos.Count > 0)
                        {
                            if (infos.ContainsKey("106"))
                                ctrl.lblOS.Text = "Sistema: " + infos["106"].ToString();
                            else ctrl.lblOS.Text = "Sistema: na";
                            if (infos.ContainsKey("101"))
                                ctrl.lblProcessorLoad.Text = "CPU: " + infos["101"].ToString() + "%";
                            else ctrl.lblProcessorLoad.Text = "CPU: na";
                        }
                        else
                        {
                            ctrl.lblOS.Text = "Sistema: na";
                            ctrl.lblProcessorLoad.Text = "CPU: na";
                        }

                        this.setControl(ctrl);

                        IN_UPGRADE = false;
                        //  frm.UI_VM_CONTAINER.Controls.Add(ctrl);
                    }
                }
                catch
                {
                    frmMachine opt = new frmMachine();
                    opt.ShowDialog();
                }
            }
            else
            {
                frmMachine opt = new frmMachine();
                opt.ShowDialog();
            }
        }
コード例 #9
0
ファイル: VM.cs プロジェクト: riccione83/Hyper-V-manager
        public void doAction(string vmName, string action,vm_control vmctrl)
        {
            const int Enabled = 2;
            const int Disabled = 3;
            const int Reboot = 10;
            const int Reset = 11;
            const int Paused = 32768;
            const int Suspended = 32769;

            if (scope == null) scope = getRemoteScope();

            ManagementObject vm = Utility.GetTargetComputer(vmName, scope);

            if (null == vm)
            {
                throw new ArgumentException(
                    string.Format(
                    "The virtual machine '{0}' could not be found.",
                    vmName));
            }

            ManagementBaseObject inParams = vm.GetMethodParameters("RequestStateChange");

            if (action.ToLower() == "start")
            {
                inParams["RequestedState"] = Enabled;
            }
            else if (action.ToLower() == "stop")
            {
                inParams["RequestedState"] = Disabled;
            }
            else if (action.ToLower() == "reboot")
            {
                inParams["RequestedState"] = Reboot;
            }
            else if (action.ToLower() == "reset")
            {
                inParams["RequestedState"] = Reset;
            }
            else if (action.ToLower() == "paused")
            {
                inParams["RequestedState"] = Paused;
            }
            else if (action.ToLower() == "suspend")
            {
                inParams["RequestedState"] = Suspended;
            }
            else
            {
                throw new Exception("Wrong action is specified");
            }

            ManagementBaseObject outParams = vm.InvokeMethod("RequestStateChange", inParams, null);

            if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
            {
                if (Utility.JobCompleted(outParams, scope,vmctrl))
                {
                    Console.WriteLine("{0} state was changed successfully.", vmName);
                }
                else
                {
                    Console.WriteLine("Failed to change virtual system state");
                }
            }
            else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
            {
                Console.WriteLine("{0} state was changed successfully.",vmName);
            }
            else
            {
                Console.WriteLine("Change virtual system state failed with error {0}",outParams["ReturnValue"]);
            }
        }
コード例 #10
0
ファイル: Utility.cs プロジェクト: riccione83/Hyper-V-manager
 public static bool JobCompleted(ManagementBaseObject outParams, ManagementScope scope,vm_control vmctrl)
 {
     bool jobCompleted = true;
     string old="";
     //Retrieve msvc_StorageJob path. This is a full wmi path
     string JobPath = (string)outParams["Job"];
     ManagementObject Job = new ManagementObject(scope, new ManagementPath(JobPath), null);
     //Try to get storage job information
     if(vmctrl!=null)
         old = vmctrl.lblState.Text;
     Job.Get();
     while ((UInt16)Job["JobState"] == JobState.Starting
         || (UInt16)Job["JobState"] == JobState.Running)
     {
         //Console.WriteLine("In progress... {0}% completed.", Job["PercentComplete"]);
         if (vmctrl != null)
         {
             vmctrl.lblState.Text = String.Format("In progress... {0}% completed.", Job["PercentComplete"]);
             vmctrl.Refresh();
         }
         System.Threading.Thread.Sleep(1000);
         Job.Get();
     }
     if(vmctrl!=null)
         vmctrl.lblState.Text = old;
     //Figure out if job failed
     UInt16 jobState = (UInt16)Job["JobState"];
     if (jobState != JobState.Completed)
     {
         UInt16 jobErrorCode = (UInt16)Job["ErrorCode"];
         Console.WriteLine("Error Code:{0}", jobErrorCode);
         Console.WriteLine("ErrorDescription: {0}", (string)Job["ErrorDescription"]);
         jobCompleted = false;
     }
     return jobCompleted;
 }