Hyper-V Script: Validate VHD
Hyper-V provides a handy scripting interface that allows you to validate a virtual hard disk. You can use this for two main purposes:
- To confirm that a standard VHD file is correctly structured and not corrupted.
- To confirm that a differencing disk has all of its parent disks present.
Here is a sample PoweShell script:
# Prompt for the Hyper-V Server to use
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
# Get name for VHD
$VHDName = Read-Host "Specify the name of the virtual had disk to validate"
# Get the Msvm_ImageManagementService object
$ImageManagementService = gwmi Msvm_ImageManagementService -namespace "root\virtualization" -computername $HyperVServer
# Validate the VHD
$result = $ImageManagementService.ValidateVirtualHardDisk($VHDName)
#Return success if the return value is "0"
if ($Result.ReturnValue -eq 0)
{write-host "The virtual hard disk is valid."}
#If the return value is not "0" or "4096" then the operation failed
ElseIf ($Result.ReturnValue -ne 4096)
{write-host "The virtual hard disk is not valid. Error value:" $Result.ReturnValue}
Else
{#Get the job object
$job=[WMI]$Result.job
#Provide updates if the jobstate is "3" (starting) or "4" (running)
while ($job.JobState -eq 3 -or $job.JobState -eq 4)
{write-host "Validating. "$job.PercentComplete "% complete"
start-sleep 1
#Refresh the job object
$job=[WMI]$Result.job}
#A jobstate of "7" means success
if ($job.JobState -eq 7)
{write-host "The virtual hard disk is valid."}
Else
{write-host "The virtual hard disk is not valid."
write-host "ErrorCode:" $job.ErrorCode
write-host "ErrorDescription" $job.ErrorDescription}
}
And a VBScript sample:
Option Explicit
Dim HyperVServer
Dim VHDName
Dim WMIService
Dim Msvm_ImageManagementService
Dim Result
Dim Job
Dim InParam
Dim OutParam
'Prompt for the Hyper-V Server to use
HyperVServer = InputBox("Specify the Hyper-V Server to create the virtual machine on:")
'Get name for VHD
VHDName = InputBox("Specify the name of the virtual had disk to validate:")
'Get an instance of the WMI Service in the virtualization namespace.
Set WMIService = GetObject("winmgmts:\\" & HyperVServer & "\root\virtualization")
'Get the Msvm_ImageManagementService object
Set Msvm_ImageManagementService = WMIService.ExecQuery("SELECT * FROM Msvm_ImageManagementService").ItemIndex(0)
'Setup the input parameter list
Set InParam = Msvm_ImageManagementService.Methods_("ValidateVirtualHardDisk").InParameters.SpawnInstance_()
InParam.Path = VHDName
'Execute the method and store the results in OutParam
Set OutParam = Msvm_ImageManagementService.ExecMethod_("ValidateVirtualHardDisk", InParam)
'Check to see if the job completed synchronously
if (OutParam.ReturnValue = 0) then
Wscript.Echo "The virtual hard disk is valid."
elseif (OutParam.ReturnValue <> 4096) then
Wscript.Echo "The virtual hard disk is not valid."
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 "Validating. " & Job.PercentComplete & "% complete"
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 "The virtual hard disk is not valid."
Wscript.Echo "ErrorCode:" & Job.ErrorCode
Wscript.Echo "ErrorDescription:" & Job.ErrorDescription
else
Wscript.Echo "The virtual hard disk is valid."
end If
end if
Cheers,
Ben