Automation

 View Only
  • 1.  Get-VM | Get-TagAssignment

    Posted Oct 13, 2015 02:46 PM

    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.



  • 2.  RE: Get-VM | Get-TagAssignment

    Posted Oct 15, 2015 07:50 PM

    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



  • 3.  RE: Get-VM | Get-TagAssignment

    Posted Oct 18, 2015 07:18 AM

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



  • 4.  RE: Get-VM | Get-TagAssignment

    Posted Oct 19, 2015 06:52 PM

    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



  • 5.  RE: Get-VM | Get-TagAssignment

    Posted Oct 20, 2015 05:41 AM

    still doesn't work :smileysad:



  • 6.  RE: Get-VM | Get-TagAssignment

    Posted Oct 20, 2015 07:40 PM

    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




  • 7.  RE: Get-VM | Get-TagAssignment

    Posted Oct 21, 2015 05:21 AM

    Hi.

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

    why?



  • 8.  RE: Get-VM | Get-TagAssignment

    Posted Oct 21, 2015 10:22 AM

    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[]