Option Explicit
Dim WMIService
Dim VHDService
Dim VHD
Dim Result
Dim Job
Dim InParam
Dim OutParam
'Specify the VHD to be mounted
VHD = "C:\Hyper-V Virtual Machines\Virtual Hard Disks\SCVMM - Domain Controller.vhd"
'Get instance of 'virtualization' WMI service on the local computer
Set WMIService = GetObject("winmgmts:\\.\root\virtualization")
'Get the MSVM_ImageManagementServiceSet
Set VHDService = WMIService.ExecQuery("SELECT * FROM Msvm_ImageManagementService").ItemIndex(0)
'Setup the input parameter list
Set InParam = VHDService.Methods_("mount").InParameters.SpawnInstance_()InParam.Path = VHD
'Execute the method and store the results in OutParam
Set OutParam = VHDService.ExecMethod_("mount", InParam)
'Check to see if the jub completed synchronously
if (OutParam.ReturnValue = 0) then
Wscript.Echo "Operation succeeded"
elseif (OutParam.ReturnValue <> 4096) then
Wscript.Echo "Operation failed"
else
'Get the job object
set Job = WMIService.Get(OutParam.Job)
'Wait for the job to complete (3 == starting, 4 == running)
while (Job.JobState = 3) or (Job.JobState = 4)
Wscript.Echo Job.PercentComplete
WScript.Sleep(1000)
'Refresh the job object
set Job = WMIService.Get(OutParam.Job)
Wend
'Provide details if the job fails (7 == complete)
if (Job.JobState <> 7) then
Wscript.Echo "Operation failed"
Wscript.Echo "ErrorCode:" & Job.ErrorCode
Wscript.Echo "ErrorDescription:" & Job.ErrorDescription
else
Wscript.Echo "Operation succeeded"
end If
end if