zoukankan      html  css  js  c++  java
  • 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 = 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
  • 相关阅读:
    python线程与进程手记
    3.08课·········switch case及if else嵌套(日期格式)
    3.07课·········if分支语句
    3.06课·········C#语言基础
    3.05课·········进制转换
    Oracle profile 使用技巧
    sys用户密码丢失找回密码的步骤和命令
    oracle帐号scott被锁定如何解锁
    SQL中哪些情况会引起全表扫描
    Oracle创建用户、角色、授权、建表
  • 原文地址:https://www.cnblogs.com/PEPE/p/3998376.html
Copyright © 2011-2022 走看看