Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Converts the type of an existing virtual hard disk.
Syntax
uint32 ConvertVirtualHardDisk(
[in] string SourcePath,
[in] string DestinationPath,
[in] uint16 Type,
[out] CIM_ConcreteJob REF Job
);
Parameters
-
SourcePath [in]
-
Type: string
A fully qualified path that specifies the location of the virtual hard disk file. This file will not be modified as a result of this operation.
-
DestinationPath [in]
-
Type: string
A fully qualified path that specifies the location of the destination virtual hard disk file.
-
Type [in]
-
Type: uint16
The type of the new virtual hard disk file.
-
Fixed (2)
-
Dynamic (3)
-
PhysicalDrive (5)
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 converts a virtual hard disk. The referenced utilities can be found in Common Utilities for the Virtualization Samples.
using System;
using System.Management;
namespace HyperVSamples
{
class ConvertVHD
{
enum DiskType
{
Fixed = 2,
Dynamic = 3,
PhysicalDrive = 5
}
static void ConvertVirtualHardDisk(string sourcePath, string destinationPath, string diskTypeName)
{
ManagementScope scope = new ManagementScope(@"root\virtualization", null);
ManagementObject imageService = Utility.GetServiceObject(scope, "Msvm_ImageManagementService");
DiskType diskType = (DiskType)Enum.Parse(typeof(DiskType), diskTypeName, true);
ManagementBaseObject inParams = imageService.GetMethodParameters("ConvertVirtualHardDisk");
inParams["SourcePath"] = sourcePath;
inParams["DestinationPath"] = destinationPath;
inParams["Type"] = diskType;
ManagementBaseObject outParams = imageService.InvokeMethod("ConvertVirtualHardDisk", inParams, null);
if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
{
if (Utility.JobCompleted(outParams, scope))
{
Console.WriteLine("{0} was converted successfully.", inParams["SourcePath"]);
}
else
{
Console.WriteLine("Unable to convert {0}", inParams["SourcePath"]);
}
}
inParams.Dispose();
outParams.Dispose();
imageService.Dispose();
}
static void Main(string[] args)
{
if (args != null && args.Length != 3)
{
Console.WriteLine("Usage: ConvertVHD SourcePath, DestinationPath, Type");
Console.WriteLine("Type:Fixed|Dynamic|PhysicalDrive");
return;
}
ConvertVirtualHardDisk(args[0], args[1], args[2]);
}
}
}
The following VBScript example converts a virtual hard disk.
option explicit
dim objWMIService
dim vhdPath
dim managementService
dim fileSystem
const JobStarting = 3
const JobRunning = 4
const JobCompleted = 7
const wmiStarted = 4096
'targetVHD type
const Fixed = 2
const Dynamic = 3
const PhysicalDrive = 5
Main()
'-----------------------------------------------------------------
' Main
'-----------------------------------------------------------------
Sub Main()
dim computer, objArgs, sourcePath, destinationPath, vhdTypeName
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
sourcePath = objArgs.Unnamed.Item(0)
destinationPath = objArgs.Unnamed.Item(1)
vhdTypeName = objArgs.Unnamed.Item(2)
else
Usage
end if
if ExecuteConvertVHD(sourcePath, destinationPath, vhdTypeName) then
WriteLog "Done"
WScript.Quit(0)
else
WriteLog "ConvertVHD failed"
WScript.Quit(1)
end if
End Sub
'-----------------------------------------------------------------
' Display Script Usage
'-----------------------------------------------------------------
Sub Usage()
WScript.Echo "usage: cscript ConvertVHD.vbs SourcePath DestinationPath Type"
WScript.Echo "Type:Fixed|Dynamic|PhysicalDrive"
WScript.Quit(1)
End Sub
'-----------------------------------------------------------------
' set target disk type
'-----------------------------------------------------------------
Function DiskTypeValue(TypeName)
if LCase(TypeName) = "fixed" then
DiskTypeValue = Fixed
elseif LCase(TypeName) = "dynamic" then
DiskTypeValue = Dynamic
elseif LCase(TypeName) = "physicaldrive" then
DiskTypeValue = PhysicalDrive
else
Usage
end if
End Function
'-----------------------------------------------------------------
' Execute ConvertVirtualHardDisk
'-----------------------------------------------------------------
Function ExecuteConvertVHD(sourcePath, destination, vhdTypeName)
WriteLog Format3("Sub ExecuteConvertVHD({0}, {1}, {2})", sourcePath, destination, vhdTypeName)
dim objInParam, objOutParams
ExecuteConvertVHD = false
set objInParam = managementService.Methods_("ConvertVirtualHardDisk").InParameters.SpawnInstance_()
objInParam.Type = DiskTypeValue(vhdTypeName)
objInParam.SourcePath = sourcePath
objInParam.DestinationPath = destination
set objOutParams = managementService.ExecMethod_("ConvertVirtualHardDisk", objInParam)
if (WMIMethodStarted(objOutParams)) then
if (WMIJobCompleted(objOutParams)) then
WriteLog Format3("VHD {0} was converted to {1} as a {2} VHD successfully.", sourcePath, destination, vhdTypeName)
ExecuteConvertVHD = 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
else
WriteLog Format1("Convert VHD with error {0}", wmiStatus)
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)
WScript.Sleep(1000)
set WMIJob = objWMIService.Get(outParam.Job)
jobState = WMIJob.JobState
wend
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(".\ConvertVHD.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 |
|