Msvm_VirtualSystemManagementService 類別的 RemoveKvpItems 方法
從虛擬機器中移除現有的機碼/值組。
語法
uint32 RemoveKvpItems(
[in] CIM_ComputerSystem REF TargetSystem,
[in] string DataItems[],
[out] CIM_ConcreteJob REF Job
);
參數
-
TargetSystem [in]
-
將移除索引鍵/值組之虛擬機器的參考。
-
DataItems [in]
-
類型: string[]
要移除的索引鍵/值組陣列, (索引鍵只需要比對) 。 陣列的每個元素都是 Msvm_KvpExchangeDataItem 類別的內嵌實例。 如果目標系統上沒有任何指定的索引鍵/值組,這個方法就會失敗。 此陣列最多可以包含 128 個專案。
-
作業 [out]
-
類型: CIM_ConcreteJob
如果作業是以非同步方式執行,這個方法會傳回 4096,而此參數將包含衍生自 CIM_ConcreteJob之物件的參考。
傳回值
類型: uint32
這個方法會傳回下列其中一個值。
-
已完成,沒有錯誤 (0)
-
已檢查方法參數 - 作業啟動 (4096)
-
失敗 (32768)
-
拒絕存取 (32769)
-
不支援 (32770)
-
狀態未知 , (32771)
-
逾 時 (32772)
-
不正確參數 (32773)
-
系統正在使用 中 (32774)
-
此作業的狀態無效 , (32775)
-
不正確的資料類型 (32776)
-
( 32777) 無法使用系統
-
記憶體不足 (32778)
備註
UAC 篩選可能會限制 對Msvm_VirtualSystemManagementService 類別的存取。 如需詳細資訊,請參閱 使用者帳戶控制和 WMI。
範例
下列 C# 範例會從虛擬機器中移除機碼/值組。 您可以在虛擬 範例的常見公用程式中找到參考的公用程式, (V2) 。
重要
若要正確運作,必須在虛擬機器主機伺服器上執行下列程式碼,而且必須使用系統管理員許可權執行。
public static void RemoveKvpItems(string vmName, string itemName)
{
ManagementScope scope = new ManagementScope(@"root\virtualization\v2", null);
ManagementObject virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");
ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("RemoveKvpItems");
ManagementClass kvpExchangeDataItem = new ManagementClass(scope, new ManagementPath("Msvm_KvpExchangeDataItem"), null);
ManagementObject dataItem = kvpExchangeDataItem.CreateInstance();
dataItem["Data"] = "";
dataItem["Name"] = itemName;
dataItem["Source"] = 0;
string[] dataItems = new string[1];
dataItems[0] = dataItem.GetText(TextFormat.CimDtd20);
ManagementObject vm = Utility.GetTargetComputer(vmName, scope);
inParams["TargetSystem"] = vm.Path.Path;
inParams["DataItems"] = dataItems;
ManagementBaseObject outParams = virtualSystemService.InvokeMethod("RemoveKvpItems", inParams, null);
if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
{
if (Utility.JobCompleted(outParams, scope))
{
Console.WriteLine("KvpItems were removed successfully.");
}
else
{
Console.WriteLine("Failed to remove KvpItems");
}
}
else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
{
Console.WriteLine("KvpItems were removed successfully.");
}
else
{
Console.WriteLine("Remove KvpItems failed with error {0}", outParams["ReturnValue"]);
}
inParams.Dispose();
outParams.Dispose();
dataItem.Dispose();
vm.Dispose();
virtualSystemService.Dispose();
}
下列 Visual Basic Scripting Edition (VBScript) 範例會從虛擬機器中移除機碼/值組。
重要
若要正確運作,必須在虛擬機器主機伺服器上執行下列程式碼,而且必須使用系統管理員許可權執行。
option explicit
dim objWMIService
dim managementService
dim fileSystem
const JobStarting = 3
const JobRunning = 4
const JobCompleted = 7
const wmiStarted = 4096
const wmiSuccessful = 0
Main()
'-----------------------------------------------------------------
' Main
'-----------------------------------------------------------------
Sub Main()
dim computer, vmName, itemName, myVm, objArgs
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 = 3 then
vmName = objArgs.Unnamed.Item(0)
itemName = objArgs.Unnamed.Item(1)
else
WScript.Echo "usage: cscript AddKvpItems.vbs vmName itemName"
WScript.Quit(1)
end if
set myVm = GetComputerSystem(vmName)
if RemoveKvpItems(myVm, itemName) then
WriteLog "Done"
WScript.Quit(0)
else
WriteLog "RemoveKvpItems failed"
WScript.Quit(1)
end if
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
'-----------------------------------------------------------------
' RemoveKvpItems
'-----------------------------------------------------------------
Function RemoveKvpItems(computerSystem, itemName)
dim dataItem, dataItems, objInParam, objOutParams
RemoveKvpItems = false
set dataItem = objWMIService.Get("Msvm_KvpExchangeDataItem").SpawnInstance_()
dataItem.Data = ""
dataItem.Name = itemName
dataItem.Source = 0
dataItems = Array(1)
dataItems(0) = dataItem.GetText_(1)
set objInParam = managementService.Methods_("RemoveKvpItems").InParameters.SpawnInstance_()
objInParam.TargetSystem = computerSystem.Path_.Path
objInParam.dataItems = dataItems
set objOutParams = managementService.ExecMethod_("RemoveKvpItems", objInParam)
if objOutParams.ReturnValue = wmiStarted then
if (WMIJobCompleted(objOutParams)) then
RemoveKvpItems = true
end if
elseif objOutParams.ReturnValue = wmiSuccessful then
RemoveKvpItems = true
else
WriteLog Format1("RemoveKvpItems failed with {0}", objOutParams.ReturnValue )
end if
End Function
'-----------------------------------------------------------------
' Handle wmi Job object
'-----------------------------------------------------------------
Function WMIJobCompleted(outParam)
dim WMIJob, jobState
WMIJobCompleted = true
set WMIJob = objWMIService.Get(outParam.Job)
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 (WMIJob.JobState <> JobCompleted) then
WriteLog Format1("ErrorDescription:{0}", WMIJob.ErrorDescription)
WriteLog Format1("ErrorCode:{0}", WMIJob.ErrorCode)
WMIJobCompleted = false
end if
End Function
'-----------------------------------------------------------------
' Create the console log files.
'-----------------------------------------------------------------
Sub WriteLog(line)
dim fileStream
set fileStream = fileSystem.OpenTextFile(".\RemoveKvpItems.log", 8, true)
WScript.Echo line
fileStream.WriteLine line
fileStream.Close
End Sub
'------------------------------------------------------------------------------
' 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 |
|