function Get-TagAssignmentDTI {<#
.SYNOPSIS
This function retrieves the tag assignments of objects
.DESCRIPTION
This function wraps the VMware.VimAutomation.Core cmdlet, Get-TagAssignment.
The VMware cmdlet has an intermittent and random error that causes it to not
return tag assignments. This wrapper detects those occurrences and retries
up to the number of times specified in Retries, waiting DelayMilliSeconds
before trying each time.
.NOTES
Author: Paul Knight
Works only with vCenter Server 5.1 or later.
.LINK
Online version: https://code.vmware.com/doc/preview?id=6330#/doc/Get-TagAssignment.html
Get-Tag
Get-TagAssignment
New-TagAssignment
Remove-TagAssignment
.PARAMETER DelayMilliSeconds
Number of milliseconds between retries if unable to retrieve the tag
assignment. Default is 250ms.
.PARAMETER Entity
Retrieves the tags associated with the specified items.
.PARAMETER Category
Returns the tags that belong to the specified categories.
.PARAMETER Retries
The number of retries to attempt before giving up. Failure is quiet and
returns null. Future revisions may want to make the nature of the failure
an option.
.PARAMETER Server
Specifies the vCenter Server systems on which you want to run the cmdlet. If
no value is passed to this parameter, the command runs on the default
servers. For more information about default servers, see the description of
Connect-VIServer.
.INPUTS
.OUTPUTS
Zero or more TagAssignment objects
.EXAMPLE
PS> $datastore = Get-DataStore MyDatastore
PS> Get-TagAssignment -Entity $datastore -Category MyCategory
Retrieves all tag assignments for the $datastore entity that have tags from
the "MyCategory" category.
#>
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline=$true)]
[PSObject]$Entity=@($null),
[PSObject[]]$Category,
[PSObject[]]$Server,
[int]$DelayMilliSeconds=250,
[int]$Retries=500
)
begin {
"Started execution" | Out-Verbose -Caller Get-TagAssignmentDTI
}
process {
foreach ($obj in $Entity) {
$tries = 0
$bRetry = $true
while ($bRetry -and $tries -le $Retries) {
$tries++
try {
VMware.VimAutomation.Core\Get-TagAssignment -Entity $obj -Category $Category -Server $Server -ErrorAction Stop -Verbose:$false
$bRetry = $false
} catch [VMware.VimAutomation.Cis.Core.Types.V1.CisException] {
# Resolve-Error($_)
if ($obj) {
("Retrying {0} {1}" -f $obj.Name,$tries) |
Out-Verbose -Caller Get-TagAssignmentDTI -Verbosity "High"
} else {
("Retrying {0}" -f $tries) |
Out-Verbose -Caller Get-TagAssignmentDTI -Verbosity "High"
}
Start-Sleep -m $DelayMilliSeconds
}
}
}
}
end {
"Finished execution" | Out-Verbose -Caller Get-TagAssignmentDTI
}
}