Backgroud:
在日常工作中,大量用到Hyper-V的管理功能.特别是如今Hyper-V虚机在创建VHD文件后对其无引用空间的自动释放,需要用到VHD->Compact功能.
下面通过这个例子,简单的介绍一下Hyper-V和VHD的自动化管理:
In darily work, need use Hyper-V management feature, special
currently Hyper-V VM does NOT release no reference space after VHD
be created, to release manually kick off
VHD->Compact function. The following is an example
how to operate Hyper-V and VHD automatically.
需求:
Requirement:
自动查询宿主上的每台关机状态的虚机,如果有,压缩当前虚机的VHD文件.
Query each VM in host with status equal to
close, if have, compact VHD file.
实现
Implements
1. 加入对system.management.dll的引用:
1. Add reference to system.management.dll:
2. 创建一个NameSpace:
2. Create a NameSpace:
对于本机是 this.scope