-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsxv2t-cleanup-nsxt-apply-vmtags.ps1
38 lines (31 loc) · 1.52 KB
/
nsxv2t-cleanup-nsxt-apply-vmtags.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<#
Example Command:
.\nsxv2t-cleanup-nsxt-apply-vmtags.ps1
#>
# Param (
# [Parameter(Mandatory=$True)][String]$ImportFile
# )
$NSXvTagsImport = Get-Content -Raw -Path "nsxv-sectags-audit.json" | ConvertFrom-Json
$vmdata = Get-NsxtPolicyService -Name com.vmware.nsx_policy.infra.realized_state.enforcement_points.virtual_machines
$vmdataResults = $vmdata.list("default").results
foreach($tagImport in $NSXvTagsImport){
if($vmdataResults.where{($_.display_name -eq $tagImport.vm_name)}.count -gt 0) {
Write-Output ($tagImport.vm_name)
$vmdataspec = $vmdata.help.updatetags.virtual_machine_tags_update.create()
$vmexternalid = $vmdata.list("default").results.where{($_.display_name -eq $tagImport.vm_name)}.external_id.value
$vmdataspec.virtual_machine_id = $vmexternalid
$existingTags = @((Get-NsxtService -Name com.vmware.nsx.fabric.virtual_machines).list().results.where{($_.display_name -eq $tagImport.vm_name)}.tags.tag)
$tagImportTags = @($NSXvTagsImport.where{($_.vm_name -eq $tagImport.vm_name)}.tag_name)
if($existingTags.count -gt 0) {
$tagImportTags += $existingTags
}
foreach($tag in $tagImportTags){
$vmdatatagspec = $vmdata.help.updatetags.virtual_machine_tags_update.tags.Element.Create()
$vmdatatagspec.tag = $tag
$vmdatatagspec.scope = " "
$vmdataspec.tags.Add($vmdatatagspec) | Out-Null
}
$vmdata.updatetags("default", $vmdataspec)
Write-Output (" ")
}
}