上篇介绍备份和还原的整体流程,本骗具体介绍实现过程。
第一步 hyper-v虚拟机信息的获取
fd端获取所有hyper-v信息,同过dir给fd发送一个命令,fd端接收到命令字处理请求,调用一个system(“quryvm.exe”),在hyper-v端生成一个 hyper-v.conf文件,里面目前存放每一个hyper-v的三个主要参数,hyper-v虚拟机名,hyper-v虚拟机所在host名,hyper-v虚拟机硬盘文件路径;
若下图,hyper-v.conf 文件
big
HS22-3
C:/Users/Public/Documents/Hyper-V/Virtual Hard Disks/big.vhdx
418
SZY
C:/Users/Public/Documents/Hyper-V/Virtual Hard Disks/418.vhdx
对应的hyper-v管理界面如下图所示;
虚拟机big 在host为HS22-3的hyper-v服务器上 路径为 HS22-3的C:/Users/Public/Documents/Hyper-V/Virtual Hard Disks/big.vhdx
虚拟机418在host为SZY的hyper-v服务器上,路径为SZY的C:/Users/Public/Documents/Hyper-V/Virtual Hard Disks/418.vhdx
所有的信息都是通过quryvm.exe这个程序获得,下面请看quryvm.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 queryvm
{
#region Return Value of RequestStateChange Method of the Msvm_ComputerSystem Class
//Return Value of RequestStateChange Method of the Msvm_ComputerSystem Class
//This method returns one of the following values.
//Completed with No Error (0)
//DMTF Reserved (7–4095)
//Method Parameters Checked - Transition Started (4096)
//Failed (32768)
//Access Denied (32769)
//Not Supported (32770)
//Status is unknown (32771)
//Timeout (32772)
//Invalid parameter (32773)
//System is in use (32774)
//Invalid state for this operation (32775)
//Incorrect data type (32776)
//System is not available (32777)
//Out of memory (32778)
#endregion
public class VMManagement
{
private stat