CreateVirtualFloppyDisk method of the Msvm_ImageManagementService class
Creates a virtual floppy disk file.
Syntax
uint32 CreateVirtualFloppyDisk(
[in] string Path,
[out] CIM_ConcreteJob REF Job
);
Parameters
-
Path [in]
-
Type: string
A fully qualified path that specifies the location of the new file.
-
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
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 creates a virtual floppy disk file. The referenced utilities can be found in Common utilities for the virtualization samples (V2).
using System;
using System.Management;
namespace HyperVSamples
{
class CreateVFD
{
static void CreateVirtualFlopy(string vhdPath)
{
ManagementScope scope = new ManagementScope(@"root\virtualization\v2", null);
ManagementObject imageService = Utility.GetServiceObject(scope, "Msvm_ImageManagementService");
ManagementBaseObject inParams = imageService.GetMethodParameters("CreateVirtualFloppyDisk");
inParams["Path"] = vhdPath;
ManagementBaseObject outParams = imageService.InvokeMethod("CreateVirtualFloppyDisk", inParams, null);
if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
{
if (Utility.JobCompleted(outParams, scope))
{
Console.WriteLine("{0} was created successfully.", inParams["Path"]);
}
else
{
Console.WriteLine("Unable to create {0}", inParams["Path"]);
}
}
inParams.Dispose();
outParams.Dispose();
imageService.Dispose();
}
static void Main(string[] args)
{
if (args != null && args.Length != 1)
{
Console.WriteLine("Usage: CreateVFD path");
return;
}
CreateVirtualFlopy(args[0]);
}
}
}
The following Visual Basic Scripting Edition (VBScript) example creates a virtual floppy disk file.
option explicit
dim objWMIService
dim managementService
dim fileSystem
const JobStarting = 3
const JobRunning = 4
const JobCompleted = 7
const wmiStarted = 4096
const method = "CreateVirtualFloppyDisk"
Main()
'-----------------------------------------------------------------
' Main
'-----------------------------------------------------------------
Sub Main()
dim computer, objArgs, vfdPath
set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject")
computer = "."
set objWMIService = GetObject("winmgmts:\\" & computer & "\root\virtualization\v2")
set managementService = objWMIService.ExecQuery("Select * from Msvm_ImageManagementService").ItemIndex(0)
set objArgs = WScript.Arguments
if WScript.Arguments.Count = 1 then
vfdPath = objArgs.Unnamed.Item(0)
else
WScript.Echo "usage: cscript CreateVFD.vbs VFDFilePath "
WScript.Quit(1)
end if
if CreateVirtualFloppyDisk(vfdPath) then
WriteLog "Done"
WScript.Quit(0)
else
WriteLog "CreateVFD failed"
WScript.Quit(1)
end if
End Sub
'-----------------------------------------------------------------
' Execute CreateVirtualFloppyDisk method
'-----------------------------------------------------------------
Function CreateVirtualFloppyDisk(vfdPath)
WriteLog Format1("Function CreateVirtualFloppyDisk({0})", vfdPath)
dim objInParam, objOutParams
CreateVirtualFloppyDisk = false
set objInParam = managementService.Methods_("CreateVirtualFloppyDisk").InParameters.SpawnInstance_()
objInParam.Path = vfdPath
set objOutParams = managementService.ExecMethod_("CreateVirtualFloppyDisk", objInParam)
if (WMIMethodStarted(objOutParams)) then
if (WMIJobCompleted(objOutParams)) then
WriteLog Format1("VHD {0} was created successfully", vfdPath)
CreateVirtualFloppyDisk = 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 Format2("Failed to create VFD {0} ConcreteJob with error {1}", vfdPath, 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(".\CreateVFD.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
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 |
|