Msvm_VirtualSystemManagementService 类的 ModifyKvpItems 方法
修改虚拟机上的现有键值对。
语法
uint32 ModifyKvpItems(
[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) 。
重要
若要正常运行,必须在虚拟机主机服务器上运行以下代码,并且必须使用管理员权限运行。
using System;
using System.Management;
namespace HyperVSamples
{
class ModifyKvpItemsClass
{
static void ModifyKvpItems(string vmName, string itemName, string itemValue)
{
ManagementScope scope = new ManagementScope(@"root\virtualization\v2", null);
ManagementObject virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");
ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("ModifyKvpItems");
ManagementClass kvpExchangeDataItem = new ManagementClass(scope, new ManagementPath("Msvm_KvpExchangeDataItem"), null);
ManagementObject dataItem = kvpExchangeDataItem.CreateInstance();
dataItem["Data"] = itemValue;
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("ModifyKvpItems", inParams, null);
if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
{
if (Utility.JobCompleted(outParams, scope))
{
Console.WriteLine("KvpItems were modified successfully.");
}
else
{
Console.WriteLine("Failed to modify KvpItems");
}
}
else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
{
Console.WriteLine("KvpItems were modified successfully.");
}
else
{
Console.WriteLine("Modify virtual system kvp items failed with error {0}", outParams["ReturnValue"]);
}
inParams.Dispose();
outParams.Dispose();
vm.Dispose();
dataItem.Dispose();
virtualSystemService.Dispose();
}
static void Main(string[] args)
{
if (args != null && args.Length != 3)
{
Console.WriteLine("Usage: ModifyKvpItems vmName itemName itemValue");
return;
}
ModifyKvpItems(args[0], args[1], args[2]);
}
}
}
以下 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, itemValue, 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)
itemValue = objArgs.Unnamed.Item(2)
else
WScript.Echo "usage: cscript AddKvpItems.vbs vmName itemName itemValue"
WScript.Quit(1)
end if
set myVm = GetComputerSystem(vmName)
if ModifyKvpItems(myVm, itemName, itemValue) then
WriteLog "Done"
WScript.Quit(0)
else
WriteLog "ModifyKvpItems 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
'-----------------------------------------------------------------
' RemoveVirtualSystemResource
'-----------------------------------------------------------------
Function ModifyKvpItems(computerSystem, itemName, itemValue)
dim dataItem, dataItems, objInParam, objOutParams
ModifyKvpItems = false
set dataItem = objWMIService.Get("Msvm_KvpExchangeDataItem").SpawnInstance_()
dataItem.Data = itemValue
dataItem.Name = itemName
dataItem.Source = 0
dataItems = Array(1)
dataItems(0) = dataItem.GetText_(1)
set objInParam = managementService.Methods_("ModifyKvpItems").InParameters.SpawnInstance_()
objInParam.TargetSystem = computerSystem.Path_.Path
objInParam.dataItems = dataItems
set objOutParams = managementService.ExecMethod_("ModifyKvpItems", objInParam)
if objOutParams.ReturnValue = wmiStarted then
if (WMIJobCompleted(objOutParams)) then
ModifyKvpItems = true
end if
elseif objOutParams.ReturnValue = wmiSuccessful then
ModifyKvpItems = true
else
WriteLog Format1("ModifyKvpItems 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(".\ModifyKvpItems.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 |
|