ReconnectParentVirtualHardDisk method of the Msvm_ImageManagementService class

Fixes broken links between parent and child disk image files.

Syntax

uint32 ReconnectParentVirtualHardDisk(
  [in]  string              ChildPath,
  [in]  string              ParentPath,
  [in]  boolean             Force,
  [out] CIM_ConcreteJob REF Job
);

Parameters

ChildPath [in]

Type: string

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

ParentPath [in]

Type: string

A fully qualified path that specifies the location of the parent virtual hard disk file.

Force [in]

Type: boolean

This parameter has no effect.

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 reconnects links between the specified parent and child. The referenced utilities can be found in Common Utilities for the Virtualization Samples.

using System;
using System.Management;
namespace HyperVSamples
{
    class ReconnectVHD
    {
        static void ReconnectParentVirtualHardDisk(string childPath, string parentPath, bool force)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization", null);
            ManagementObject imageService = Utility.GetServiceObject(scope, "Msvm_ImageManagementService");

            ManagementBaseObject inParams = imageService.GetMethodParameters("ReconnectParentVirtualHardDisk");
            inParams["ParentPath"] = parentPath;
            inParams["ChildPath"] = childPath;
            inParams["Force"] = force;
            ManagementBaseObject outParams = imageService.InvokeMethod("ReconnectParentVirtualHardDisk", inParams, null);
            
            if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
            {
                Console.WriteLine("{0} was reconnected successfully.", inParams["ChildPath"]);
            }
            else
            {
                Console.WriteLine("Unable to reconnect {0}", inParams["ChildPath"]);
            }
            
            inParams.Dispose();
            outParams.Dispose();
            imageService.Dispose();
        }


        static void Main(string[] args)
        {
            if (args != null && args.Length != 3)
            {
                Console.WriteLine("Usage: ReconnectVHD ChildPath, ParentPath, Force");
                Console.WriteLine("Force: True|False");
                return;
            }
            string childPath = args[0];
            string parentPath = args[1];
            bool force = bool.Parse(args[2]);
            ReconnectParentVirtualHardDisk(childPath, parentPath, force);
        }
    }
}

The following VBScript example reconnects links between the specified parent and child.

option explicit 

dim objWMIService
dim managementService
dim fileSystem

const wmiSuccessful = 0

Main()

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

    dim computer, childPath, parentPath, force, objArgs
    
    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 = 3 then
        childPath = objArgs.Unnamed.Item(0)
        parentPath = objArgs.Unnamed.Item(1)
        force = objArgs.Unnamed.Item(2)
    else
        WScript.Echo "usage: cscript ReconnectVHD.vbs ChildPath ParentPath Force"
        WScript.Echo "Force: true|false"
        WScript.Quit(1)
    end if

    if ReconnectParentVirtualHardDisk(childPath, parentPath, force) then
        WriteLog "Done"
        WScript.Quit(0)
    else
        WriteLog "ReconnectVHD failed."
        WScript.Quit(1)
    end if

End Sub

'-----------------------------------------------------------------
' Execute ReconnectParentVirtualHardDisk
'-----------------------------------------------------------------
Function ReconnectParentVirtualHardDisk(childPath, parentPath, force)
    WriteLog Format2("Sub ReconnectParentVirtualHardDisk({0}, {1})",  childPath, parentPath)
    
    dim objInParam, objOutParams

    ReconnectParentVirtualHardDisk = false

    set objInParam = managementService.Methods_("ReconnectParentVirtualHardDisk").InParameters.SpawnInstance_()
    objInParam.ChildPath = childPath
    objInParam.ParentPath = parentPath
    objInParam.Force = force

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

    if objOutParams.ReturnValue = wmiSuccessful then
        WriteLog Format2("'{0}' was successfully reconnected to the parent '{1}'.", ChildPath, ParentPath)
        ReconnectParentVirtualHardDisk = true
    else
        WriteLog Format1("ReconnectParentVirtualHardDisk operation failed with error {0}", objOutParams.ReturnValue)
    end if

End Function

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

End Sub

'------------------------------------------------------------------------------
' The string formatting functions to avoid string concatenation.
'------------------------------------------------------------------------------
Function Format3(myString, arg0, arg1, arg2)

    Format3 = Format2(myString, arg0, arg1)
    Format3 = Replace(Format3, "{2}", arg2)

End Function


'------------------------------------------------------------------------------
' 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

SetParentVirtualHardDisk (V2)

CIM_ConcreteJob

Msvm_ImageManagementService