/// <Summary> /// restore the VM // </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 ("snapshot restored successfully" + info); If (isneedstatetozero) {fo. freerdp (rdpid);} else {fo. hasrdp (rdpid);} Jr. data = new {state = "success", MSG = "snapshot restored" };} else {Jr. data = new {state = "error", MSG = "snapshot restoration failed"}; // logwriter. info ("snapshot restoration failed" + info) ;}catch (exception ex) {// logwriter. info ("Restore snapshot exception" + info + "exception information:" + ex. message); Jr. data = new {state = "error", MSG = ex. message };} return Jr ;}# region hyper-v vm restores 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 VM restore 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 restart /// <summary> /// </Summary> /// <Param name = "strname"> logon name </param>/ // <Param name = "strpwd"> password </param> /// <Param name = "ip"> machine IP address </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; // ''or your account (Be sure to have administrator permissions) op. password = strpwd; // ''Your password 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"); explain query1 = new evaluate (scope, OQ); // get the WMI control metrics querycollection1 = query1.get (); foreach (managementobject mobj in querycollection1) {string [] STR = {""}; mobj. invokemethod (doinfo, STR);} // MessageBox. show ("operation successful");} catch (exception ey) {// MessageBox. show (ey. message); // This. button1.w.mclick () ;}# endregion
Restore hyper-V and esxi virtual machines and restart remotely (shut down)