Merge pull request #305 from thebillness/patch-1
Add Tag Assigments option
This commit is contained in:
@@ -5,15 +5,23 @@ function Export-Tag {
|
|||||||
[VMware.VimAutomation.ViCore.Types.V1.VIServer]$Server,
|
[VMware.VimAutomation.ViCore.Types.V1.VIServer]$Server,
|
||||||
|
|
||||||
[Parameter(Mandatory = $True, Position = 2)]
|
[Parameter(Mandatory = $True, Position = 2)]
|
||||||
[string]$Destination
|
[string]$Destination,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $False, Position = 3)]
|
||||||
|
[boolean]$ExportAssignments
|
||||||
)
|
)
|
||||||
|
|
||||||
# Retrieve all categories
|
# Retrieve all categories
|
||||||
$categoryList = Get-TagCategory -Server $server
|
$categoryList = Get-TagCategory -Server $server
|
||||||
# Retrieve all tags
|
# Retrieve all tags
|
||||||
$tagList = Get-Tag -Server $server
|
$tagList = Get-Tag -Server $server
|
||||||
# Store the tags and categories in a list to export them at once
|
# Store the tags, categories and assignments (if selected) in a list to export them at once
|
||||||
|
If ($ExportAssignments) {
|
||||||
|
$tagAssignments = Get-TagAssignment -Server $server
|
||||||
|
$export = @($categoryList, $tagList, $tagAssignments)
|
||||||
|
} else {
|
||||||
$export = @($categoryList, $tagList)
|
$export = @($categoryList, $tagList)
|
||||||
|
}
|
||||||
# Export the tags and categories to the specified destination
|
# Export the tags and categories to the specified destination
|
||||||
Export-Clixml -InputObject $export -Path $destination
|
Export-Clixml -InputObject $export -Path $destination
|
||||||
}
|
}
|
||||||
@@ -25,7 +33,10 @@ function Import-Tag {
|
|||||||
[VMware.VimAutomation.ViCore.Types.V1.VIServer]$Server,
|
[VMware.VimAutomation.ViCore.Types.V1.VIServer]$Server,
|
||||||
|
|
||||||
[Parameter(Mandatory = $True, Position = 2)]
|
[Parameter(Mandatory = $True, Position = 2)]
|
||||||
[string]$Source
|
[string]$Source,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $False, Position = 3)]
|
||||||
|
[boolean]$ImportAssignments
|
||||||
)
|
)
|
||||||
|
|
||||||
# Import the tags and categories from the specified source
|
# Import the tags and categories from the specified source
|
||||||
@@ -62,4 +73,23 @@ function Import-Tag {
|
|||||||
-Server $server `
|
-Server $server `
|
||||||
| Out-Null
|
| Out-Null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Restore the assignments if selected
|
||||||
|
If ($ImportAssignments) {
|
||||||
|
# Check for assignments in the file
|
||||||
|
If ($import[2]) {
|
||||||
|
# If tags were found, assign them
|
||||||
|
$tagAssignments = $import[2]
|
||||||
|
ForEach ($assignment in $tagAssignments) {
|
||||||
|
New-TagAssignment `
|
||||||
|
-Tag (Get-Tag -Server $server -Name $assignment.Tag.Name -Category $assignment.Tag.Category) `
|
||||||
|
-Entity (Get-VIObjectByVIView -MORef $assignment.Entity.id) `
|
||||||
|
-Server $server `
|
||||||
|
| Out-Null
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
# If no assignments were found, output warning
|
||||||
|
Write-Warning "Source file does not contain tag assignments."
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user