Hi all ,
I tried A lot of script to get tags , and finally I found good script .
I have 2 issues with this script .
Script:
Get-VM | ForEach-Object {
$VM = $_
$VM | ForEach-Object {
$Report = "" | Select-Object VM,VMHost,Notes,Tag
$Report.VM = $VM.Name
$Report.VMHost = $VM.VMHost
$Report.Notes = $VM.Notes
$VM | Get-TagAssignment
$Report.Tag = $_.Tag
$Report
}
} | Export-Csv -Path C:\temp\VMTags1.csv -NoTypeInformation
My problems with script is :
A. I need create about all tag new Column . like tag1 Column , tag2 another Column .
How I create new Tag if I have more of one tag?
B. After export csv file , I need also import tags, how I take all new tags from file and import it?
Thanks all.
Alex.
Please try this. (It should be better for VM without TAG)
Get-VM | ForEach-Object {
$VM = $_
$Tag = $_ | get-tagassignment
If($Tag){
$Tag.tag| foreach-object{
$Output = New-Object -Type PSObject -Prop ([ordered]@{
'VM' = $VM
'Host' = $VM.VMhost
'Notes' = $VM.Notes
'TagCategory' = $_.Category
'TagName' = $_.Name
})
Return $Output
}
}Else{
$Output = New-Object -Type PSObject -Prop ([ordered]@{
'VM' = $VM
'Host' = $VM.VMhost
'Notes' = $VM.Notes
'TagCategory' = "NO TAG FOR THIS VM"
'TagName' = "NO TAG FOR THIS VM"
})
Return $Output
}
} | ogv
Which version of vCenter are you targeting?
Which version of PowerCLI?
Could you please provide a screenshot of the result?
The following script will get you the CSV file, with a column per TagCategory and the assigned Tag in that column.
Note that we sort the resulting records on the number of properties in each object.
This to avoid the "feature" in Export-Csv that uses the properties in the 1st row to determine how many columns there will be in the CSV
Get-VM | ForEach-Object {
$VM = $_
$VM | ForEach-Object {
$obj = [ordered]@{
VM = $VM.Name
VMHost = $VM.VMHost
Notes = $VM.Notes
}
$_ | Get-TagAssignment | %{
$tCat = $_.Tag.Category
$tAssigned = $_.Tag.Name
$obj.Add($tCat,$tAssigned)
}
New-Object PSObject -Property $obj
}
} | Sort-Object -Property {($_ | Get-Member -MemberType NoteProperty).Count} -Descending |
Export-Csv -Path C:\temp\VMTags1.csv -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
To restore the tag assignments from that CSV file, you could try something like this
foreach($vm in (Import-Csv -Path C:\temp\VMTags1.csv)){
Get-Member -InputObject $vm -MemberType NoteProperty |
where{'VM','VMHost','Notes' -notcontains $_.Name} | %{
$tagCategory = $_.Name
$tag = Invoke-Expression "`$vm.$($_.Name)"
Try{
$tcatObj = Get-TagCategory -Name $tagCategory -ErrorAction Stop | Out-Null
}
Catch{
$tcatObj = New-TagCategory -Name $tagCategory -Cardinality Single -EntityType VirtualMachine
}
Try{
$tagObj = Get-Tag -Name $tag -Category $tcatObj -ErrorAction Stop
}
Catch{
$tagObj = New-Tag -Name $tag -Category $tcatObj
}
$vmObj = Get-VM -Name $vm.VM
$cTags = Get-TagAssignment -Entity $vmObj -Category $tcatObj | where{$_.Tag.Name -eq $tag}
if(!$cTags){
New-TagAssignment -Entity $vmObj -Tag $tagObj
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD ,
Thank you a lot for your help.
I have exceptions with script :
I have more of one :"VM_SystemOwner" ( Tag for this Category) and it's exception .
I added screenshot of my output:
Again , thanks a lot .
Ok, got it, you have more tags in the same tag category.
How do you intend to enter that in the CSV ?
Perhaps a mock layout of the CSV would help me understand what you want to produce ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This is the headers that I want..
IF some server haven't category of tag , or some tag missing I want complete it and import after that..
Let me see if I get that right, you want the Tag in the column header, not the Tag Category ?
And what should go on a line for a specific VM in these Tag columns ? Present and not-present ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No ,
I give you example what I want :
In empty column I want complete after export file and after that import this.
It seems that it will be better for you to modify the format of exported data.
It will make the job easier to export the tags AND import them if it is your final goal.
For example you can target something like:
VM / Category / TAG
VM1 / Storage / SSD
VM1 / Owner / Finance Department
VM1 / Performance / HIGH
VM2 / Owner/ Human ressources
Maybe you will need to export two CSV files.
One is for all VM information with a relation one to one like Name/CPU/Memory etc.
The other will contain all information for tag only which is a one to many relation.
A VM can have one or multiples tags on multiples categories (Or in the same category) or no tags at all.
With information in the format provided above you can reimport tags by working line by line.
The chapter “Data Normalization” in the book “Access 2013 Bible” is providing many concepts similar to the above that can be reused for manipulating data with PowerShell.
Example with 3 test VMs created. The third doesn't have any TAG associated to it.
Get-VM | where {$_.Name -match "TAG*"}| ForEach-Object {
$VM = $_
$Tag = $_ | get-tagassignment
If($Tag){
$Tag.tag| foreach-object{
$Output = New-Object -Type PSObject -Prop ([ordered]@{
'VM' = $VM
'Host' = $VM.VMhost
'Notes' = $VM.Notes
'TagCategory' = $_.Category
'TagName' = $_.Name
})
Return $Output
}
}Else{
$Tag.tag| foreach-object{
$Output = New-Object -Type PSObject -Prop ([ordered]@{
'VM' = $VM
'Host' = $VM.VMhost
'Notes' = $VM.Notes
'TagCategory' = "NO TAG FOR THIS VM"
'TagName' = "NO TAG FOR THIS VM"
})
Return $Output
}
}
} | ogv
And the output looks like:
hi ccalvetTCCchristophe calvet
thank you , but output from this script it's nothing..
Did you remove the following line?
where {$_.Name -match "TAG*"}
This filter is valid only in my test environment.
Hey,
I remove this line and my output it's only tag and tag category without the name host and guest and guest without tags not shown also.
Please try this. (It should be better for VM without TAG)
Get-VM | ForEach-Object {
$VM = $_
$Tag = $_ | get-tagassignment
If($Tag){
$Tag.tag| foreach-object{
$Output = New-Object -Type PSObject -Prop ([ordered]@{
'VM' = $VM
'Host' = $VM.VMhost
'Notes' = $VM.Notes
'TagCategory' = $_.Category
'TagName' = $_.Name
})
Return $Output
}
}Else{
$Output = New-Object -Type PSObject -Prop ([ordered]@{
'VM' = $VM
'Host' = $VM.VMhost
'Notes' = $VM.Notes
'TagCategory' = "NO TAG FOR THIS VM"
'TagName' = "NO TAG FOR THIS VM"
})
Return $Output
}
} | ogv
Which version of vCenter are you targeting?
Which version of PowerCLI?
Could you please provide a screenshot of the result?
Great it's work!
now, after export , I need to import same csv file with new Tag Category and Tag .
Which script I need to use?
Vcenter Version 5.5.0 Build 2175560
PowerCLI : 6.0 release 1 Build 2548067
Could you provide clarification on what you would like to achieve?
There are three areas potentially to cover:
TagCategory
Tag
TagAssignment
If you are thinking of backup and restore it will be necessary to cover these three areas.
The post below looks like promising for this goal, i didn't test it though.
Backup/restore vCenter tags and tag assignments | The Shell Nut
If you are thinking of automatise the creation of TagCategory, Tag and TagAssignment from a CSV it will be better to look in this direction.
http://www.virtu-al.net/2014/11/13/automating-tags-tag-category-creation-assignment-powercli/
Hi again,
I have new problem ,
I need export the same thing but without tag category .
Only :
VM | Host | Notes | Tag 1 | Tag 2 | Tag 3 |
With last script I got :
VM | Host | Notes | Tag Category | Tag
and if server have more of one tag it's going down a row like:
server 1 | host1 | note1 | Tag category 1 | tag1
server 1 | host 1 | note 1| Tag category 2 | tag 2
I need :
Server 1 | Host 1 | note 1 | tag1 | tag 2 | tag 3 etc.
Can its possible ?
I also am trying to restore my tags and not very powershell savvy. I am trying to import the tags from the information I have off a CSV file. Am I supposed to be using the administrator account as the username and login?? Here is the script:
Add-PsSnapin VMware.VimAutomation.Core -ea "SilentlyContinue"
Connect-viserver xxxxxxx.corp.na.xxx.xxx -user xxxx -pass xxxxxxxx
$CMDBInfo = Import-CSV C:\Users\sfields\documents\cmdbinfo.csv
# Get the header names to use as tag category names
$TagCatNames = $cmdbinfo | Get-Member | Where {$_.MemberType -eq "NoteProperty"} | Select -Expand Name
# Create the Tag Category if it doesnt exist
Foreach ($Name in ($TagCatNames | Where {$_ -ne "Name"})) {
if (-Not (Get-TagAssignment $Name -ea SilentlyContinue)) {
Write-Host "Creating Tag Category $Name"
New-TagAssignment -Name $Name -Description "$Name from CMDB" | Out-Null
}
}
# Create Tags under the Tag Categories
$UniqueTags = $cmdbinfo | Select -expand $name | Get-Unique
Foreach ($Tag in $UniqueTags) {
if (-Not (Get-Tag $Tag -ea SilentlyContinue)) {
Write-Host "..Creating Tag under $Name of $Tag"
New-Tag -Name $Tag -Category $name -Description "$Tag from CMDB" | Out-Null
}
# Assign the Tags to the VMs/Hosts
$cmdbinfo | Where {$_.($Name) -eq $Tag} | Foreach {
Write-Host ".... Assigning $Tag in Category of $Name to $($_.Name)"
$TagAssignment = Get-Tag -Category $Name -name $Tag
New-TagAssignment -entity $($_.Name) -Tag $Tagassignment | Out-Null
}
}
I am getting errors back:
Connect-viserver : Method not found: 'Void VMware.Binding.Sts.VmwareSecruityTokenService..ctor(System.Uri)'.
At C:\Users\sfields\Documents\scripts\vmtagimported.ps1:2 char:1
+ Connect-viserver xxxxxxx.corp.xxxx.xxxx -user xxxxxx
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Connect-VIServer], MissingMethodException
+ FullyQualifiedErrorId : System.MissingMethodException,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer
Creating Tag Category Application
New-TagAssignment : A parameter cannot be found that matches parameter name 'Name'.
At C:\Users\sfields\Documents\scripts\vmtagimported.ps1:12 char:27
+ New-TagAssignment -Name $Name -Description "$Name from CMDB" ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-TagAssignment], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
The problem seems to be with the Connect-VIServer cmdlet.
You have to use an account that has correct Role to handle Tags.
You could use the administrator@xxx.yyy account if you have access to that one.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks I will try that for sure