Hi,
i need a little help with my script. The goal is, to retrieve all vms from a vcenter, with Name, CPUCount; Memory, .. AND the TAG within a Tag Category.
Every VM has a Tag like "John Doe" and the Tag-Category is always "customer".
My script, so far:
$report = @()
foreach($vm in Get-View -ViewType Virtualmachine){
$vms = "" | Select-Object VMName,VMState, TotalCPU, CPUShare, TotalMemory, Datastore, UsedSpaceGB, ProvisionedSpaceGB, Tags
$vms.VMName = $vm.Name
$vms.VMState = $vm.summary.runtime.powerState
$vms.TotalCPU = $vm.summary.config.numcpu
$vms.CPUShare = $vm.Config.CpuAllocation.Shares.Level
$vms.TotalMemory = $vm.summary.config.memorysizemb
$vms.Datastore = $vm.Config.DatastoreUrl[0].Name
$vms.UsedSpaceGB = [math]::Round($vm.Summary.Storage.Committed/1GB,2)
$vms.ProvisionedSpaceGB = [math]::Round($vm.Summary.Storage.UnCommitted/1GB,2)
$vms = @{Name="Tags";Expression={(Get-TagAssignment -Category "customer" $_).Tag.Name}} | Where {$_.Tags}
$Report += $vms
}
$report
I know that the little one-liner with 'get-vm' worked:
> get-VM | where {$_.Powerstate -eq "poweredOn" } |Select Name,PowerState, NumCpu, MemoryMB, @{Name= "Tags" ;Expression={(Get-TagAssignment -Category "Customer" $_).Tag.Name}}| Where {$_.Tags} |
But how can i get the Tag into the script above?
Try like this
foreach($vm in Get-View -ViewType Virtualmachine){
$vms = "" | Select-Object VMName,VMState, TotalCPU, CPUShare, TotalMemory, Datastore, UsedSpaceGB, ProvisionedSpaceGB, Tags
$vms.VMName = $vm.Name
$vms.VMState = $vm.summary.runtime.powerState
$vms.TotalCPU = $vm.summary.config.numcpu
$vms.CPUShare = $vm.Config.CpuAllocation.Shares.Level
$vms.TotalMemory = $vm.summary.config.memorysizemb
$vms.Datastore = $vm.Config.DatastoreUrl[0].Name
$vms.UsedSpaceGB = [math]::Round($vm.Summary.Storage.Committed/1GB,2)
$vms.ProvisionedSpaceGB = [math]::Round($vm.Summary.Storage.UnCommitted/1GB,2)
$vms.Tags = (Get-TagAssignment -Entity (Get-VIObjectByVIView -VIView $vm) -Category "customer").Tag.Name
$report += $vms
}
$report
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this
foreach($vm in Get-View -ViewType Virtualmachine){
$vms = "" | Select-Object VMName,VMState, TotalCPU, CPUShare, TotalMemory, Datastore, UsedSpaceGB, ProvisionedSpaceGB, Tags
$vms.VMName = $vm.Name
$vms.VMState = $vm.summary.runtime.powerState
$vms.TotalCPU = $vm.summary.config.numcpu
$vms.CPUShare = $vm.Config.CpuAllocation.Shares.Level
$vms.TotalMemory = $vm.summary.config.memorysizemb
$vms.Datastore = $vm.Config.DatastoreUrl[0].Name
$vms.UsedSpaceGB = [math]::Round($vm.Summary.Storage.Committed/1GB,2)
$vms.ProvisionedSpaceGB = [math]::Round($vm.Summary.Storage.UnCommitted/1GB,2)
$vms.Tags = (Get-TagAssignment -Entity (Get-VIObjectByVIView -VIView $vm) -Category "customer").Tag.Name
$report += $vms
}
$report
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you for your answer. But after 6 Minutes waiting, nothing happens in the PowerCLI. I'm not getting any output, not even a error..
Which PowerCLI version are you using?
In older PowerCLI versions the Tag cmdlets were noticably slow.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi,
the PowerCLI Version is 6.3 Release 1. Our vCenter Version is a 6.0 with ~ 1500 VMs.
I would seriously advise to upgrade your PowerCLI version.
The Tag related cmdlets switched to REST API calls, and are noticably faster than the older versions.
And yes, vSphere 6 is supported.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Uninstalled all PowerCLI Tools, fresh install via Win PowerShell and ran the script again.
Now, it takes a few minutes (not measured, i guess ~8 .. 10 mins) but the script is working. I recieve all the informations that i wanted.
Many thanks for your help!
Hi Luc,
I am try below code to get the detail in csv as we have multiple category. But its not working.
Also Is it possible to get details in csv as below?
VMName VMState Customer Datacenter Product Enviroment
VM1 Poweredon VMware Europe VSAN TEST
Currently i am getting all category under tag
VMName VMState Tags
VM1 PowerOn {VMware,Europe,VSAN,TEST}
&{$report = @()
foreach($vm in Get-View -ViewType Virtualmachine){
$vms = "" | Select-Object VMName,VMState,Tags
$vms.VMName = $vm.Name
$vms.VMState = $vm.summary.runtime.powerState
$vms.Tags = (Get-TagAssignment -Entity (Get-VIObjectByVIView -VIView $vm) -Category "Customer","Datacenter","Product","Environment").Tag.Name
$report += $vms
}
$report
} | export-csv .\VMTag.csv -NoTypeInformation -UseCulture
Thanks
Bikash
I did something like that in Solved: Re: Exporting VM Tags in readable format - VMware Technology Network VMTN
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc, i used the below code. But in my env we have 4205 VMs but i got report of only 3809 VMs.
And i got around 141 error as below. Am i missing something ?
MethodInvocationException:
Line |
19 | $val.Add($tag.Tag.Category.Name,$tag.Tag.Name)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictionary: 'Customer' Key being added: 'Customer'"
MethodInvocationException:
$tagCat = @()
$tagTab = @{}
foreach($tag in (Get-TagAssignment)){
$tagCat += $tag.Tag.Category.Name
$key = $tag.Entity.Name
if($tagTab.ContainsKey($key)){
` $val = $tagTab.Item($key)
}
else{
$val = @{}
}
$val.Add($tag.Tag.Category.Name,$tag.Tag.Name)
$tagTab[$key] = $val
}
$tagCat = $tagCat | Sort-Object -Unique
$tags = foreach($row in ($tagTab.GetEnumerator() | Sort-Object -Property Key)){
$obj = New-Object PSObject -Property @{
VM = $row.Key
}
$tagCat | %{
$obj | Add-Member -Name $_ -Value $row.Value[$_] -MemberType NoteProperty
}
$obj
}
$tags | Export-Csv tags1.csv -NoTypeInformation -UseCulture
Thanks
Bikash
That looks as if you have Tag categories with multiple Tags.
In that case, you will have to make the "key" ($tag.Tag.Category.Name) unique.
And how would you store that in the CSV?
One option would be to add those different Tags, separated with a comma, under the same column (Category).
That requires testing if a "key" is already present or not in the hash table.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes we have multiple tag categories and under each category we have different tags as below
Customer Datacenter Product Enviroment
VMware Europe VSAN TEST
So you mean the existing script cannot be updated to be adapted to work without testing?
Can you please let me know which part of the script needs to be updated as to what , so that I ll test n get back to you.