GetVirtualHardDiskInfo method of the Msvm_ImageManagementService class

Retrieves information about the virtual hard disk files.

Syntax

uint32 GetVirtualHardDiskInfo(
  [in]  string              Path,
  [out] string              Info,
  [out] CIM_ConcreteJob REF Job
);

Parameters

Path [in]

Type: string

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

Info [out]

Type: string

If successful, this object contains the information for the requested virtual hard disk. This is an embedded instance of Msvm_VirtualHardDiskInfo.

Job [out]

Type: CIM_ConcreteJob

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

Return value

Type: uint32

This method can return one of the following values.

Completed with No Error (0)

Method Parameters Checked - Job Started (4096)

Failed (32768)

Access Denied (32769)

Not Supported (32770)

Status is unknown (32771)

Timeout (32772)

Invalid parameter (32773)

System is in used (32774)

Invalid state for this operation (32775)

Incorrect data type (32776)

System is not available (32777)

Out of memory (32778)

File not found (32779)

Remarks

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

Examples

The following C# example retrieves information about the virtual hard disk files. The referenced utilities can be found in Common Utilities for the Virtualization Samples.

using System;
using System.Xml;
using System.Management;

namespace HyperVSamples
{

    class GetVirtualHardDiskInfoClass
    {
        static void DisplayPropertyValue(string propertyName, XmlDocument doc)
        {
            string xpath = string.Format(@"//PROPERTY[@NAME = '{0}']/VALUE/child::text()", propertyName);
            XmlNode node = doc.SelectSingleNode(xpath);
            if (node == null)
            {
                if (propertyName != "ParentPath")
                {
                    throw new Exception("GetVirtualHardDiskInfo returned an invalid CIM_XML instance");
                }
            }
            else
            {
                Console.WriteLine("{0}:{1}", propertyName, node.Value);
            }
        }

        static void DisplayVHDProperties(string diskInfo)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(diskInfo);

            //fileSize
            DisplayPropertyValue("FileSize", doc);

            //MaxInternalSize
            DisplayPropertyValue("MaxInternalSize", doc);

            //disk type
            DisplayPropertyValue("Type", doc);

            //InUse should be false
            DisplayPropertyValue("InUse", doc);

            //InSavedState should be FALSE
            DisplayPropertyValue("InSavedState", doc);

            //ParentPath
            DisplayPropertyValue("ParentPath", doc);
            
        }

        static void GetVirtualHardDiskInfo(string path)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization", null);
            ManagementObject imageService = Utility.GetServiceObject(scope, "Msvm_ImageManagementService");
            ManagementBaseObject inParams = imageService.GetMethodParameters("GetVirtualHardDiskInfo");
            inParams["Path"] = path;

            ManagementBaseObject outParams = imageService.InvokeMethod("GetVirtualHardDiskInfo", inParams, null);

            if (outParams != null)
            {
                if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
                {
                    //no error
                    string diskInfo = (string)outParams["Info"];
                    DisplayVHDProperties(diskInfo);
                }
                else
                {   // error occurred
                    Console.WriteLine("GetVirtualHardDiskInfo failed. ReturnValue:{0}", (UInt32)outParams["ReturnValue"]);
                }
            }
            else
            {
                Console.WriteLine("WMI failed");
            }

            inParams.Dispose();
            outParams.Dispose();
            imageService.Dispose();
        }

        static void Main(string[] args)
        {
            if (args != null && args.Length != 1)
            {
                Console.WriteLine("Usage: GetVirtualHardDiskInfo path");
                return;
            }
            GetVirtualHardDiskInfo(args[0]);
        }
    }
}

The following VBScript example retrieves information about the virtual hard disk files.

option explicit

dim objWMIService
dim managementService
dim fileSystem

const wmiSuccessful = 0

Main()

'-----------------------------------------------------------------
' Main
'-----------------------------------------------------------------
Sub Main()

    dim path, objArgs, computer
    
    set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject")

    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
       path = objArgs.Unnamed.Item(0)
    else
       WScript.Echo "usage: cscript GetVirtualHardDiskInfo.vbs Path"
       WScript.Quit(1)
    end if

    if GetVirtualHardDiskInfo(path) then
        WriteLog "Done"
        WScript.Quit(0)
    else
        WriteLog "GetVirtualHardDiskInfo failed"
        WScript.Quit(1)
    end if

End Sub

'-----------------------------------------------------------------
' Query a filed value from CIM_XML and display it
'-----------------------------------------------------------------
Sub DisplayPropertyValue(propertyName, doc)
   dim xpath, node
   
   xpath = Format1("//PROPERTY[@NAME = '{0}']/VALUE/child:text()", propertyName)
   set node = doc.selectSingleNode(xpath) 
   
   if node Is Nothing then
       if propertyName <> "ParentPath" then
           WriteLog "GetVirtualHardDiskInfo returned an invalid CIM_XML instance"
       end if
   else
       WriteLog Format2("{0}:{1}", propertyName, node.Text)
   end if
End Sub


'-----------------------------------------------------------------
' Display the VHD properties
'-----------------------------------------------------------------
Sub DisplayVHDProperties(diskInfo)

    dim objXMLDoc 
    
    set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
    objXMLDoc.async = False 
    objXMLDoc.loadXML(diskInfo) 
    
    'fileSize
    DisplayPropertyValue "FileSize", objXMLDoc

    'MaxInternalSize
    DisplayPropertyValue "MaxInternalSize", objXMLDoc

    'disk type
    DisplayPropertyValue "Type", objXMLDoc

    'InUse 
    DisplayPropertyValue "InUse", objXMLDoc

    'InSavedState 
    DisplayPropertyValue "InSavedState", objXMLDoc

    'ParentPath
    DisplayPropertyValue "ParentPath", objXMLDoc

End Sub

'-----------------------------------------------------------------
' Execute ExpandVirtualHardDisk
'-----------------------------------------------------------------
Function GetVirtualHardDiskInfo(path)
    
    WriteLog Format1("Sub GetVirtualHardDiskInfo({0})",  path)
    
    dim objInParam, objOutParams

    GetVirtualHardDiskInfo = false

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

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

    if (objOutParams.ReturnValue = wmiSuccessful) then
        DisplayVHDProperties objOutParams.Info
        GetVirtualHardDiskInfo = true
    else
        WriteLog Format2("GetVirtualHardDiskInfo {0} failed with error {1}", Path, objOutParams.ReturnValue)
    end if

End Function


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

End Sub

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

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

Requirements

Minimum supported client
None supported
Minimum supported server
Windows Server 2008
End of client support
None supported
End of server support
Windows Server 2012
Namespace
Root\Virtualization
MOF
WindowsVirtualization.mof

See also

GetVirtualHardDiskSettingData (V2)

CIM_ConcreteJob

Msvm_ImageManagementService