首页 > 代码库 > Hyper-V、Esxi虚机还原,远程重启(关机)

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

 /// <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 = http://www.mamicode.com/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虚机还原,远程重启(关机)