码迷,mamicode.com
首页 > 其他好文 > 详细

Hyper-V、Esxi虚机还原,远程重启(关机)

时间:2014-09-28 18:14:23      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   sp   div   

 /// <summary>
        /// 还原虚拟机
        /// </summary>
        /// <param name="RDPID"></param>
        /// <returns></returns>
        public ActionResult RevertVM(string RDPID, bool isNeedStateTOZero)
        {
            JsonResult jr = new JsonResult();
            try
            {
                bool flag = false;
                RDPInfoFO fo = new RDPInfoFO();
                RDPInfoDTO rdp = fo.GetRDPHostInfo(RDPID);
                if (string.IsNullOrEmpty(rdp.VMservice))
                {
                    flag = RevertESXI(RDPID);
                }
                else
                {
                    flag = RevertHyper_V(RDPID);
                }
                if (flag)
                {
                    //LogWriter.Info("还原快照成功" + info);
                    if (isNeedStateTOZero)
                    {
                        fo.FreeRDP(RDPID);
                    }
                    else
                    {
                        fo.HasRDP(RDPID);
                    }
                    jr.Data = new { State = "success", Msg = "还原快照成功" };
                }
                else
                {
                    jr.Data = new { State = "error", Msg = "还原快照失败" };
                    //LogWriter.Info("还原快照失败" + info);
                }
            }
            catch (Exception ex)
            {
                //LogWriter.Info("还原快照出现异常" + info + " 异常信息:" + ex.Message);
                jr.Data = new { State = "error", Msg = ex.Message };
            }
            return jr;
        }



        #region Hyper-V虚拟机还原
        private bool RevertHyper_V(string RDPID)
        {
            RDPInfoFO fo = new RDPInfoFO();
            RDPInfoDTO rdp = fo.GetRDPHostInfo(RDPID);
            object result = WSHelper.InvokeWebService(rdp.VMservice, "RollBack", new string[] { rdp.VMName, rdp.HostServerIP, rdp.HostUserName, rdp.HostPassword });
            bool flag = (bool)result;
            return flag;
        }
        #endregion


        #region ESXI虚拟机还原
        private bool RevertESXI(string RDPID)
        {
            RDPInfoFO fo = new RDPInfoFO();
            RDPInfoDTO rdp = fo.GetRDPHostInfo(RDPID);
            string info = "HostServerIP:" + rdp.HostServerIP + "  VMName:" + rdp.VMName + "  username:" + rdp.HostUserName + "  password:" + rdp.HostPassword;
            bool rel = VMSnapshot.RevertVm(rdp.HostServerIP, rdp.VMName, rdp.HostUserName, rdp.HostPassword);
            return rel;
        }
        #endregion

        #region Windwos7重启
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strname">登陆名</param>
        /// <param name="strpwd">密码</param>
        /// <param name="ip">机器IP地址</param>
        /// <param name="doinfo">Reboot/Shutdown</param>
        private void CloseComputer(string strname, string strpwd, string ip, string doinfo)
        {
            ConnectionOptions op = new ConnectionOptions();
            op.Username = strname;//‘‘或者你的帐号(注意要有管理员的权限) 
            op.Password = strpwd; //‘‘你的密码 
            ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2:Win32_Service", op);
            try
            {
                scope.Connect();
                System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
                ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq);
                //得到WMI控制 
                ManagementObjectCollection queryCollection1 = query1.Get();
                foreach (ManagementObject mobj in queryCollection1)
                {
                    string[] str = { "" };
                    mobj.InvokeMethod(doinfo, str);
                }
                //MessageBox.Show("操作成功");
            }
            catch (Exception ey)
            {
                //MessageBox.Show(ey.Message);
                //this.button1.PerformClick();
            }
        }

        #endregion

 

Hyper-V、Esxi虚机还原,远程重启(关机)

标签:style   blog   color   io   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/PEPE/p/3998376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!