Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

ValidateVirtualHardDisk method of the Msvm_ImageManagementService class


Validates whether a virtual disk image can be opened in read-only mode.


uint32 ValidateVirtualHardDisk(
  [in]   string Path,
  [out]  CIM_ConcreteJob REF Job


Path [in]

Type: string

A fully qualified path that specifies the location of the disk image file.

Job [out]

Type: CIM_ConcreteJob

A reference to the job (can be null if the task is completed successfully).

Return value

Type: uint32

This method can return one of the following values.

Completed with No Error (0)
Method Parameters Checked - JobStarted (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)
File Not Found (32779)


If the virtual hard disk is a differencing disk, the entire virtual disk chain is opened. If the link is broken in the disk chain, a job object is returned with the appropriate error initialized and the child and parent properties are initialized to the location where the link is broken.

Access to the Msvm_ImageManagementService class might be restricted by UAC Filtering. For more information, see User Account Control and WMI.


The following C# example validates a virtual hard disk image. The referenced utilities can be found in Common Utilities for the Virtualization Samples.

using System;
using System.Management;

namespace HyperVSamples
    class ValidateVHD
        static void ValidateVirtualHardDisk(string vhdPath)
            ManagementScope scope = new ManagementScope(@"root\virtualization", null);
            ManagementObject imageService = Utility.GetServiceObject(scope, "Msvm_ImageManagementService");

            ManagementBaseObject inParams = imageService.GetMethodParameters("ValidateVirtualHardDisk");
            inParams["Path"] = vhdPath;
            ManagementBaseObject outParams = imageService.InvokeMethod("ValidateVirtualHardDisk", inParams, null);
            if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
                if (Utility.JobCompleted(outParams, scope))
                    Console.WriteLine("{0} is a valid virtual hard disk.", inParams["Path"]);
                    Console.WriteLine("Unable to validate {0}", inParams["Path"]);


        static void Main(string[] args)
            if (args != null && args.Length != 1)
                Console.WriteLine("Usage: ValidateVHD vhdPath");


The following VBScript example validates a virtual hard disk image.

option explicit 

dim objWMIService
dim managementService
dim fileSystem
dim vhdPath

const JobStarting = 3
const JobRunning = 4
const JobCompleted = 7
const wmiStarted = 4096


' Main
Sub Main()
    set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject")
    dim computer, objArgs
    computer = "."
    set objWMIService = GetObject("winmgmts:\\" & computer & "\root\virtualization")
    set managementService = objWMIService.ExecQuery("Select * from Msvm_ImageManagementService").ItemIndex(0)

    set objArgs = WScript.Arguments
    if WScript.Arguments.Count = 1 then
        vhdPath = objArgs.Unnamed.Item(0)
        WScript.Echo "usage: cscript ValidateVHD.vbs path"
    end if

    if ValidateVHD(vhdPath) then
        WriteLog "Done"
        WriteLog "ValidateVHD failed"
    end if

End Sub

' Execute ValidateVirtualHardDisk
Function ValidateVHD(vhdPath)
    WriteLog Format1("Function ValidateVHD({0})",  vhdPath)
    dim objInParam, objOutParams

    ValidateVHD = false

    set objInParam = managementService.Methods_("ValidateVirtualHardDisk").InParameters.SpawnInstance_()
    objInParam.Path = vhdPath

    set objOutParams = managementService.ExecMethod_("ValidateVirtualHardDisk", objInParam)

    if (WMIMethodStarted(objOutParams)) then
        if (WMIJobCompleted(objOutParams)) then
            WriteLog Format1("{0} is a valid virtual hard disk.", vhdPath)
            ValidateVHD = true
        end if
    end if

End Function

' Handle wmi return values
Function WMIMethodStarted(outParam)
    WriteLog "Function WMIMethodStarted"
    dim wmiStatus
    WMIMethodStarted = false

    if Not IsNull(outParam) then
        wmiStatus = outParam.ReturnValue

        if  wmiStatus = wmiStarted then
            WMIMethodStarted = true
            WriteLog Format1("Unable to validate {0}", vhdPath)
        end if
   end if

End Function

' Handle wmi Job object
Function WMIJobCompleted(outParam)
    WriteLog "Function WMIJobCompleted"
    dim WMIJob, jobState

    set WMIJob = objWMIService.Get(outParam.Job)

    WMIJobCompleted = true

    jobState = WMIJob.JobState

    while jobState = JobRunning or jobState = JobStarting
        WriteLog Format1("In progress... {0}% completed.",WMIJob.PercentComplete)
        set WMIJob = objWMIService.Get(outParam.Job)
        jobState = WMIJob.JobState

    if (jobState <> JobCompleted) then
        WriteLog Format1("ErrorCode:{0}", WMIJob.ErrorCode)
        WriteLog Format1("ErrorDescription:{0}", WMIJob.ErrorDescription)
        WMIJobCompleted = false
    end if

End Function

' Create the console log files.
Sub WriteLog(line)
    dim fileStream
    set fileStream = fileSystem.OpenTextFile(".\ValidateVHD.log", 8, true)
    WScript.Echo line
    fileStream.WriteLine line

End Sub

' The string formatting functions to avoid string concatenation.
Function Format1(myString, arg0)
    Format1 = Replace(myString, "{0}", arg0)
End Function


Minimum supported client

None supported

Minimum supported server

Windows Server 2008

End of client support

None supported

End of server support

Windows Server 2012





See also

ValidateVirtualHardDisk (V2)



Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.