Compartir a través de


Unmount method of the Msvm_ImageManagementService class

[The Unmount method is available for use in the operating systems specified in the Requirements section. It may be altered or unavailable in subsequent versions. Instead, use the Unmount method of the Msvm_MountedStorageImage class.]

Unmounts an existing virtual disk image file in loopback mode.

Syntax

uint32 Unmount(
  [in] string Path
);

Parameters

Path [in]

Type: string

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

Return value

Type: uint32

This method can return one of the following values.

Return code/value Description
Completed with No Error
0
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
[!Note]
Added in Windows Server 2012 R2.

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 unmounts a virtual disk image file. The referenced utilities can be found in Common Utilities for the Virtualization Samples.

using System;
using System.Management;

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

            ManagementBaseObject inParams = imageService.GetMethodParameters("Unmount");
            inParams["Path"] = vhdPath;
            ManagementBaseObject outParams = imageService.InvokeMethod("Unmount", inParams, null);
            if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
            {
                Console.WriteLine("{0} was unmounted successfully.", inParams["Path"]);
            }
            else
            {
                Console.WriteLine("Unmount operation for {0} failed with error {1}.", vhdPath, outParams["ReturnValue"]);
            }

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

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

The following VBScript example unmounts a virtual disk image file.

option explicit 

dim objWMIService
dim vhdPath
dim managementService
dim fileSystem

const wmiCompleted = 0

Main()

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

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


    if ExecuteUnmountVHD then
        WriteLog "Done"
        WScript.Quit(0)
    else
        WriteLog "Unmount operation failed."
        WScript.Quit(1)
    end if

End Sub

'-----------------------------------------------------------------
' ExecuteUnmountVHD
'-----------------------------------------------------------------
Function ExecuteUnmountVHD ()

    WriteLog Format1("Sub ExecuteUnmountVHD ({0})",  vhdPath)
    
    dim objInParam, objOutParams

    ExecuteUnmountVHD = false

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

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

    if  objOutParams.ReturnValue = wmiCompleted then
        ExecuteUnmountVHD = true
        WriteLog Format1("{0} was unmounted successfully.", vhdPath)
    else
        WriteLog Format2("Unmount operation for {0} failed with error {1}", vhdPath, objOutParams.ReturnValue)
    end if
    
End Function

'-----------------------------------------------------------------
' Create the console log files.
'-----------------------------------------------------------------
Sub WriteLog(line)
    dim fileStream
    set fileStream = fileSystem.OpenTextFile(".\UnmountVHD.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

DetachVirtualHardDisk (V2)

Msvm_ImageManagementService