VMware Cloud Community
AlexPanov
Enthusiast
Enthusiast

Get-VM | Get-TagAssignment

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.

Tags (2)
Reply
0 Kudos
7 Replies
jasonrobinson
Enthusiast
Enthusiast

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

Jason @jrob24
Reply
0 Kudos
AlexPanov
Enthusiast
Enthusiast

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 ??

Reply
0 Kudos
jasonrobinson
Enthusiast
Enthusiast

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

Jason @jrob24
Reply
0 Kudos
AlexPanov
Enthusiast
Enthusiast

still doesn't work Smiley Sad

Reply
0 Kudos
ccalvetTCC
Enthusiast
Enthusiast

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


Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
Reply
0 Kudos
AlexPanov
Enthusiast
Enthusiast

Hi.

Now I receive output in tag row : System.Object[]

why?

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos