Hi guy's .
I have a problem with my script.
I want export details from my vcenter and I had a problem with export Tag's.
My script :
Get-VM | `
ForEach-Object {
$Tag = $VM | Get-TagAssignment
$VM = $_
$VMview = $VM | Get-View
$VMResourceConfiguration = $VM | Get-VMResourceConfiguration
$VMHardDisks = $VM | Get-HardDisk
$HardDisksSizesGB = @()
$Temp = $VMHardDisks | ForEach-Object { $HardDisksSizesGB += [Math]::Round($_.CapacityKB/1MB) }
$VmdkSizeGB = ""
$Temp = $HardDisksSizesGB | ForEach-Object { $VmdkSizeGB += "$_+" }
$VmdkSizeGB = $VmdkSizeGB.TrimEnd("+")
$TotalHardDisksSizeGB = 0
$Temp = $HardDisksSizesGB | ForEach-Object { $TotalHardDisksSizeGB += $_ }
$Snapshots = $VM | Get-Snapshot
$Report = "" | Select-Object VMname,ESXname,Tag,MemoryGB,vCPUcount,vNICcount,IPaddresses,VmdkSizeGB,TotalVmdkSizeGB,DatastoreName,ToolsVersion,ToolsUpdate,SnapshotCount,GuestOS
$Report.VMName = $VM.name
$Report.ESXname = $VM.Host
$Report.Tag = $_.Tag
$Report.MemoryGB = $VM.MemoryMB/1024
$Report.vCPUcount = $VM.NumCpu
$Report.vNICcount = $VM.Guest.Nics.Count
$Report.IPaddresses = $VM.Guest.IPAddress
$Report.VmdkSizeGB = $VmdkSizeGB
$Report.TotalVmdkSizeGB = $TotalHardDisksSizeGB
$Report.DatastoreName = $VMview.Config.DatastoreUrl
$Report.ToolsVersion = $VMview.Config.Tools.ToolsVersion
$Report.ToolsUpdate = $VMview.Guest.ToolsStatus
$Report.SnapshotCount = (@($VM | Get-Snapshot)).Count
$Report.GuestOS = $VM.Guest.OSFullName
Write-Output $Report | export-csv –append –path c:\temp\AlexTag8.csv
}
I export all without tags and I don't understand why..
Thanks ALL.
Alex.
I believe using the Tag property is specifying the Tag object and not the Name property you are looking for. You need to take it down one more step.
Update the this line:
$Report.Tag = $_.Tag.Name
Hi Jason.
Thank you for help.
It's still doesn't work. The TAG row is empty and I havn't output of tags.
Any Idea's ??
Sorry yes I see the problem, you are setting the tag information to $Tag, but trying to call with it with $_ which is using the current VM object in the foreach loop.
Use this line instead.
$Report.Tag = $Tag.Tag.Name
still doesn't work
Hi,
Move the line
$Tag = $VM | Get-TagAssignment
below the line
$VM = $_
The variable $VM doesn't exist until the line $VM = $_
Then replace
$Report.Tag = $_.Tag
By
$Report.Tag = $Tag.tag
Hi.
Now I receive output in tag row : System.Object[]
why?
That meand you have more than tag assigned to that VM.
The Export-Csv doesn't know how to display this array, hence the System.Object[]
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference