Hyper-v虚拟机备份与还原实现(三)

备份完还原就比较简单了;

第一步 dir管理端执行还原任务从数据库获取需要还原的hyperv虚拟机和host名;

不做过多说明;

第二步 fd端通过hyperv虚拟机名和host名 还原虚拟机

还原主要通过一个New-VM命令,fd通过调用system(“restore.exe”)来执行还原;

restore.exe源码如下:

using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.IO;
using System.Text;
using System.Management;
using System.Text.RegularExpressions;

namespace restore
{

   public class Program
   {
      private static string script = File.ReadAllText(@"C:\hyperv.ps1");
      public static string ReadLineAddress()
      {
         String textFileName = @"C:\name.conf";
         TextReader tr = new StreamReader(textFileName);
         StringBuilder strtmp = new StringBuilder();
         string findstr = "";
         int i = 0;
         while (tr.Peek() > 0)
         {
            i++;
            findstr = tr.ReadLine();
            if(findstr.Contains("#"))
            {
               continue;
            }
            if (string.IsNullOrEmpty(findstr))
            {
               continue;
            }else{
               strtmp.Append(findstr);
               strtmp.Append("+");
            }
         }
         tr.Close();
         return strtmp.ToString();
      }

      private static string  getmemory(string vmname, string hostname)
      {
         string ret="";
         using (Runspace runspace = RunspaceFactory.CreateRunspace())
         {
            runspace.Open();
            PowerShell ps = PowerShell.Create();
            ps.Runspace = runspace;
            ps.AddScript(script);
            ps.Invoke();
            ps.AddCommand("getmemory").AddParameters(
               new Dictionary(){
                  {"vname", vmname},
                  {"hostname",hostname}
               }
            );

            foreach (PSObject result in ps.Invoke())
            {
               Console.WriteLine(result);
               ret = result.ToString();
            }
         }
         return ret;
      }

      private static string getcpunum(string vmname,string hostname)
      {
         string ret = "";
         using (Runspace runspace = RunspaceFactory.CreateRunspace())
         {
            runspace.Open();
            PowerShell ps = PowerShell.Create();
            ps.Runspace = runspace;
            p

你可能感兴趣的:(Hyper-v备份还原,Hyper-v,posershell,C#,C)