Msvm_VirtualSystemManagementService 類別的 GetVirtualSystemThumbnailImage 方法
擷取現有虛擬機器的縮圖影像。
語法
uint32 GetVirtualSystemThumbnailImage(
[in] CIM_VirtualSystemSettingData REF TargetSystem,
[in] uint16 WidthPixels,
[in] uint16 HeightPixels,
[out] uint8 ImageData[]
);
參數
-
TargetSystem [in]
-
類型: CIM_VirtualSystemSettingData
要擷取縮圖影像 之CIM_VirtualSystemSettingData 實例的參考。 這個實例可能代表虛擬機器的目前具現化,或虛擬機器快照集的實例。
-
WidthPixels [in]
-
類型: uint16
所需影像的寬度,以圖元為單位。
-
HeightPixels [in]
-
類型: uint16
所需影像的高度,以圖元為單位。
-
ImageData [out]
-
類型: uint8[]
所要求的影像資料,格式為原始 RGB 565。
傳回值
類型: uint32
這個方法會傳回下列其中一個值。
-
已完成 , (0)
-
已檢查方法參數 - 作業啟動 (4096)
-
失敗 (32768)
-
拒絕存取 (32769)
-
不支援 (32770)
-
狀態未知 (32771)
-
逾 時 (32772)
-
不正確參數 (32773)
-
系統正在使用 (32774)
-
此作業的狀態無效 , (32775)
-
不正確的資料類型 (32776)
-
系統無法使用 (32777)
-
記憶體不足 (32778)
備註
Msvm_VirtualSystemManagementService 類別的 存取可能會受到 UAC 篩選的限制。 如需詳細資訊,請參閱 使用者帳戶控制和 WMI。
範例
下列 C# 範例會擷取虛擬機器的縮圖影像。 您可以在 虛擬範例的通用公用程式中找到參考的公用程式, (V2) 。
重要
若要正確運作,必須在虛擬機器主機伺服器上執行下列程式碼,而且必須使用系統管理員許可權執行。
static void SaveImageData(byte[] imageData)
{
FileStream fs = new FileStream(@"c:\test.bin", FileMode.Create, FileAccess.Write);
fs.Write(imageData, 0, imageData.Length);
fs.Flush();
fs.Close();
}
public static void GetVirtualSystemThumbnailImage(string vmName)
{
ManagementScope scope = new ManagementScope(@"root\virtualization\v2", null);
ManagementObject virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");
ManagementObject vm = Utility.GetTargetComputer(vmName, scope);
ManagementObjectCollection vmsettingDatas = vm.GetRelated(
"Msvm_VirtualSystemsettingData",
"Msvm_SettingsDefineState",
null,
null,
"SettingData",
"ManagedElement",
false,
null);
ManagementObject settingData = null;
foreach (ManagementObject data in vmsettingDatas)
{
settingData = data;
break;
}
ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("GetVirtualSystemThumbnailImage");
inParams["HeightPixels"] = 16;
inParams["WidthPixels"] = 16;
inParams["TargetSystem"] = settingData.Path.Path;
ManagementBaseObject outParams = virtualSystemService.InvokeMethod("GetVirtualSystemThumbnailImage", inParams, null);
if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
{
if (Utility.JobCompleted(outParams, scope))
{
SaveImageData((byte[])outParams["ImageData"]);
Console.WriteLine("VM '{0}' thumbnail were retrieved successfully.", vm["ElementName"]);
}
else
{
Console.WriteLine("Failed to retrieve VM thumbnail");
}
}
else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
{
SaveImageData((byte[])outParams["ImageData"]);
Console.WriteLine("VM '{0}' thumbnail were retrieved successfully.", vm["ElementName"]);
}
else
{
Console.WriteLine("Failed to retrieve VM thumbnail with error {0}", (UInt32)outParams["ReturnValue"]);
}
inParams.Dispose();
outParams.Dispose();
settingData.Dispose();
vm.Dispose();
virtualSystemService.Dispose();
}
下列 Visual Basic Scripting Edition (VBScript) 範例會擷取虛擬機器的縮圖影像。
重要
若要正確運作,必須在虛擬機器主機伺服器上執行下列程式碼,而且必須使用系統管理員許可權執行。
option explicit
dim objWMIService
dim managementService
dim fileSystem
const wmiStarted = 4096
const wmiSuccessful = 0
Main()
'-----------------------------------------------------------------
' Main
'-----------------------------------------------------------------
Sub Main()
dim computer, objArgs, vmName, vm
set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject")
computer = "."
set objWMIService = GetObject("winmgmts:\\" & computer & "\root\virtualization\v2")
set managementService = objWMIService.ExecQuery("select * from Msvm_VirtualSystemManagementService").ItemIndex(0)
set objArgs = WScript.Arguments
if WScript.Arguments.Count = 1 then
vmName = objArgs.Unnamed.Item(0)
else
WScript.Echo "usage: cscript GetVirtualSystemThumbnailImage.vbs vmName"
WScript.Quit(1)
end if
set vm = GetComputerSystem(vmName)
if StartVm(vm) then
if GetVirtualSystemThumbnailImage(vm) then
WriteLog "Done"
WScript.Quit(0)
End if
end if
WriteLog "GetVirtualSystemThumbnailImage Failed."
WScript.Quit(1)
End Sub
'-----------------------------------------------------------------
' Retrieve Msvm_VirtualComputerSystem from base on its ElementName
'-----------------------------------------------------------------
Function GetComputerSystem(vmElementName)
On Error Resume Next
dim query
query = Format1("select * from Msvm_ComputerSystem where ElementName = '{0}'", vmElementName)
set GetComputerSystem = objWMIService.ExecQuery(query).ItemIndex(0)
if (Err.Number <> 0) then
WriteLog Format1("Err.Number: {0}", Err.Number)
WriteLog Format1("Err.Description:{0}",Err.Description)
WScript.Quit(1)
end if
End Function
'-----------------------------------------------------------------
' Save the thumbnail
'-----------------------------------------------------------------
Sub SaveThumbnailImage(thumbnailBytes)
dim stream
Const adTypeText = 2
Const adSaveCreateOverWrite = 2
set stream = CreateObject("ADODB.Stream")
stream.Type = adTypeText
stream.Open
Redim text(ubound(thumbnailBytes) \ 2)
for i = lbound(thumbnailBytes) to ubound(thumbnailBytes) step 2
text(i\2) = ChrW(thumbnailBytes(i + 1) * &HFF + thumbnailBytes(i))
next
stream.WriteText text
stream.SaveToFile ".\thumbnail.bin", adSaveCreateOverWrite
stream.Close
End Sub
'-----------------------------------------------------------------
' Start the virtual machine
'-----------------------------------------------------------------
Function StartVm(computerSystem)
dim objInParam, objOutParams
StartVm = false
if computerSystem.OperationalStatus(0) = 2 then
StartVm = true
Exit Function
end if
set objInParam = computerSystem.Methods_("RequestStateChange").InParameters.SpawnInstance_()
objInParam.RequestedState = 2
set objOutParams = computerSystem.ExecMethod_("RequestStateChange", objInParam)
if objOutParams.ReturnValue = wmiStarted then
if (WMIJobCompleted(objOutParams)) then
StartVm = true
end if
elseif objOutParams.ReturnValue = wmiSuccessful then
StartVm = true
else
WriteLog Format1("StartVM failed with ReturnValue {0}", wmiStatus)
end if
End Function
'-----------------------------------------------------------------
' Print the thumbnail data
'-----------------------------------------------------------------
Sub PrintThumbnailImage(thumbnailBytes)
dim index
for index = lbound(thumbnailBytes) to ubound(thumbnailBytes)
WriteLog Format2("{0}:{1} ", index, thumbnailBytes(i))
next
End Sub
'-----------------------------------------------------------------
' Define a virtual system
'-----------------------------------------------------------------
Function GetVirtualSystemThumbnailImage(computerSystem)
dim query, objInParam, objOutParams, virtualSystemsetting
GetVirtualSystemThumbnailImage = false
query = Format1("ASSOCIATORS OF {{0}} WHERE resultClass = Msvm_VirtualSystemsettingData", computerSystem.Path_.Path)
set virtualSystemsetting = objWMIService.ExecQuery(query).ItemIndex(0)
set objInParam = managementService.Methods_("GetVirtualSystemThumbnailImage").InParameters.SpawnInstance_()
objInParam.HeightPixels = 16
objInParam.WidthPixels = 16
objInParam.TargetSystem = virtualSystemsetting.Path_.Path
set objOutParams = managementService.ExecMethod_("GetVirtualSystemThumbnailImage", objInParam)
if objOutParams.ReturnValue = wmiStarted then
if (WMIJobCompleted(objOutParams)) then
GetVirtualSystemThumbnailImage = true
end if
elseif objOutParams.ReturnValue = wmiSuccessful then
GetVirtualSystemThumbnailImage = true
else
WriteLog Format1("GetVirtualSystemThumbnailImage failed with ReturnValue {0}", wmiStatus)
end if
End Function
'-----------------------------------------------------------------
' Handle wmi Job object
'-----------------------------------------------------------------
Function WMIJobCompleted(outParam)
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(".\GetVirtualSystemThumbnailImage.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
規格需求
需求 | 值 |
---|---|
最低支援的用戶端 |
Windows 8 [僅限傳統型應用程式] |
最低支援的伺服器 |
Windows Server 2012 [僅限傳統型應用程式] |
命名空間 |
Root\Virtualization\V2 |
MOF |
|
DLL |
|