CompactVirtualHardDisk method of the Msvm_ImageManagementService class
Compacts a virtual hard disk file. Compacting is the process of freeing unused portions of the virtual hard disk. The virtual hard disk is not automatically mounted. See Remarks for usage restrictions for this method.
Syntax
uint32 CompactVirtualHardDisk(
[in] string Path,
[in] uint16 Mode,
[out] CIM_ConcreteJob REF Job
);
Parameters
-
Path [in]
-
Type: string
A fully qualified path that specifies the location of the merging file.
-
Mode [in]
-
Type: uint16
Specifies the mode for the compact operation. This must be one of the following values.
-
Full (0)
-
Quick (1)
-
Retrim (2)
-
Pretrimmed (3)
-
Prezeroed (4)
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 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)
Remarks
Only the following types of virtual hard disks can be used with this method:
- Fixed VHDX
- Dynamic VHD
- Dynamic VHDX
- Differencing VHD
- Differencing VHDX
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 compacts a virtual hard disk. The referenced utilities can be found in Common utilities for the virtualization samples (V2).
public enum VirtualHardDiskCompactMode
{
Full = 0,
Quick = 1,
Retrim = 2,
Pretrimmed = 3,
Prezeroed = 4
}
public static void CompactVirtualHardDisk(string vhdPath, VirtualHardDiskCompactMode compactMode)
{
ManagementScope scope = new ManagementScope(@"root\virtualization\v2", null);
ManagementObject imageService = Utility.GetServiceObject(scope, "Msvm_ImageManagementService");
ManagementBaseObject inParams = imageService.GetMethodParameters("CompactVirtualHardDisk");
inParams["Path"] = vhdPath;
inParams["Mode"] = compactMode;
ManagementBaseObject outParams = imageService.InvokeMethod("CompactVirtualHardDisk", inParams, null);
if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
{
if (Utility.JobCompleted(outParams, scope))
{
Console.WriteLine("{0} was compacted successfully.", inParams["Path"]);
}
else
{
Console.WriteLine("Unable to compact {0}", inParams["Path"]);
}
}
else
{
Console.WriteLine("Compact {0} returned error {1}", inParams["Path"], outParams["ReturnValue"]);
}
inParams.Dispose();
outParams.Dispose();
imageService.Dispose();
}
Requirements
Requirement | Value |
---|---|
Minimum supported client |
Windows 8 [desktop apps only] |
Minimum supported server |
Windows Server 2012 [desktop apps only] |
Namespace |
Root\Virtualization\V2 |
MOF |
|
DLL |
|