Restore hyper-V and esxi virtual machines and restart remotely (shut down)

Source: Internet
Author: User
/// <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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.