Skip to content

Instantly share code, notes, and snippets.

@broestls
Last active July 15, 2020 17:43
Show Gist options
  • Save broestls/0ab21c67f4938297c81fbdca6480d0f2 to your computer and use it in GitHub Desktop.
Save broestls/0ab21c67f4938297c81fbdca6480d0f2 to your computer and use it in GitHub Desktop.
PowerCLI: Tagging vCenter Entities, Creates new Tags if they do not exist
# Target to be tagged
$entity = ""
# Set the tags for your target here
# Empty quotes will not be set on target entity
$desired_tags = @{
class = "";
billing_owner = "";
billing_owner_email = "";
admin_owner = "";
admin_owner_email = ""
}
# Test for existence or create new tag as needed
$tags = @{}
$desired_tags.getEnumerator() | foreach {
if ((Get-Tag -Category $_.key -Name $_.value) -eq $null) {New-Tag -Name $_.value -Category $_.key}
$tags.add($_.key, (Get-Tag -Category $_.key -Name $_.value))
}
# Assign the tags to $entity
foreach ($t in $tags.Values) {
New-TagAssignment -Tag $t -Entity $entity
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment