首页 » 默认不分类 » VBS挂载VHD文件脚本

VBS挂载VHD文件脚本

Hyper-V的磁盘文件是VHD文件,如果我想直接浏览这文件中的内容怎么办,以前的做法是打开一台VPS的设置,添加硬盘,选择这块硬盘,再进入系统中设置个盘符就能看到。
其实没那么麻烦,直接用命令就可以在我的电脑中挂载VHD虚拟硬盘文件,以下脚本在win2008上通过:

将以下脚本另存为vhdmount.vbs,然后在DOS下运行:cscript d:\vhdmount.vbs d:\diske.vhd 能看懂吧

1
2
3
4
5
6
7
8
9
10
11
12
Option Explicit
Dim objWMIService, objVHDService, strComputer, strVHDFile
strComputer ="."
' Check all arguments required have been passed
If Wscript.Arguments.Count < 1 Then
  Wscript.Echo "Arguments <VHD File> required. For example:" & vbCrLf & "cscript vhdmount.vbs disk.vhd"
  Wscript.Quit(0)
End If
strVHDFile = Wscript.Arguments(0)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\virtualization")
Set objVHDService = objWMIService.ExecQuery("SELECT * FROM Msvm_ImageManagementService").ItemIndex(0)
objVHDService.Mount(strVHDFile)

现在您打开我的电脑,还不能看到盘的,右击我的电脑,管理,磁盘管理,就能看到多了个磁盘1,然后右击区块设置盘符。用完了要删掉还得用脚本才行。
将以下脚本另存为vhdunmount.vbs,然后在DOS下运行:cscript d:\vhdunmount.vbs d:\diske.vhd 就能取消挂载

1
2
3
4
5
6
7
8
9
10
11
12
Option Explicit
Dim objWMIService, objVHDService, strComputer, strVHDFile
strComputer ="."
' Check all arguments required have been passed
If Wscript.Arguments.Count < 1 Then
  Wscript.Echo "Arguments <VHD File> required. For example:" & vbCrLf & "cscript vhdmount.vbs disk.vhd"
  Wscript.Quit(0)
End If
strVHDFile = Wscript.Arguments(0)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\virtualization")
Set objVHDService = objWMIService.ExecQuery("SELECT * FROM Msvm_ImageManagementService").ItemIndex(0)
objVHDService.Unmount(strVHDFile)

, , ,

转发到新浪微博 转发到新浪微博

目前这篇文章有1条评论(Rss)

  1. Reklama | #1
    05/17/2012 at 08:12

    I don’t usually comment but I gotta state thanks for the post on this one : D

我要评论