Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VMWARE rest api标记关联_Rest_Vmware_Vsphere - Fatal编程技术网

VMWARE rest api标记关联

VMWARE rest api标记关联,rest,vmware,vsphere,Rest,Vmware,Vsphere,我正在尝试通过vsphere 6.5的rest api将vmware标记与虚拟机关联。缺少该方法的api文档: 特别是,我正在努力寻找关于对象id类型的任何信息,以及应该作为两个id传递什么(不同的来源给出不同的答案)。请提供有关这些字段格式和内容的任何信息 这是我目前的职位。在创建虚拟机期间,将从api中提取ID POST /rest/com/vmware/cis/tagging/tag-association/id:urn:vmomi:InventoryServiceTag:<hex

我正在尝试通过vsphere 6.5的rest api将vmware标记与虚拟机关联。缺少该方法的api文档:

特别是,我正在努力寻找关于对象id类型的任何信息,以及应该作为两个id传递什么(不同的来源给出不同的答案)。请提供有关这些字段格式和内容的任何信息

这是我目前的职位。在创建虚拟机期间,将从api中提取ID

POST /rest/com/vmware/cis/tagging/tag-association/id:urn:vmomi:InventoryServiceTag:<hex number>:GLOBAL?~action=attach
POST/rest/com/vmware/cis/tagging/tag关联/id:urn:vmomi:InventoryServiceTag::GLOBAL?~action=attach
Json编码体:

{"object_id":{"id":"Elstree_vm-<4 digit number>","type":"VirtualMachine"}}
{“object_id”:{“id”:“Elstree_vm-”,“type”:“VirtualMachine”}
api返回404,错误标记对象未找到,类型为cis.Tagging.objectNotFound.error。

解决了这个问题

正确的帖子格式:

POST /rest/com/vmware/cis/tagging/tag-association/id:urn:vmomi:InventoryServiceTag:<hex number>:GLOBAL?~action=attach
POST/rest/com/vmware/cis/tagging/tag关联/id:urn:vmomi:InventoryServiceTag::GLOBAL?~action=attach
Json编码体

{"object_id":{"id":"vm-<4 digit number>","type":"VirtualMachine"}}
{“object_id”:{“id”:“vm-”,“type”:“VirtualMachine”}

我从powershell中的VMware(带有powercli模块)获得了此工作代码:


您在哪里找到要获取虚拟机的类型列表?我找了几个小时试图找到这个,你的碎片帮了我。。。我一直在使用
string
,就像API文档的例子一样——谢谢
com.vmware.vapi.rest.unsupportedMediaType
$creds = Get-Credential
$vCenter = 'vcenter.fqdn'
$tagName = "WebServer"
$vmName = "web01"

Connect-CisServer -Server $vCenter -Credential $creds
Connect-ViServer -Server $vCenter -Credential $creds

$tagSvc = Get-CisService -Name com.vmware.cis.tagging.tag
$tagList = $tagSvc.list()
$tags = @()
foreach ($t in $tagList) {
    $tags += $tagSvc.Get($t)
}
$tag = $tags | where {$_.Name -eq $tagName}
$vm = Get-VM -Name $vmName
$tagAssoc = Get-CisService -Name com.vmware.cis.tagging.tag_association
$objId = $tagAssoc.Help.attach.object_id.Create()
$objId.type = $vm.ExtensionData.MoRef.Type
$objId.id = $vm.ExtensionData.MoRef.Value
$tagAssoc.attach($tag.id.Value, $objId)

$attachedTags = $tagAssoc.list_attached_tags($objId)
foreach ($at in $attachedTags) {
    $tagSvc.Get($at)
}