VMware Cloud Community
VJ_VMware_111
Enthusiast
Enthusiast
Jump to solution

PowerCLI VMware Host Tag help Needed

Hi Guys,

I have a script for collecting the necessary information that i need about ESX hosts. The script was very fast, and runs in few minutes to get data from many vCenters. Now i wanted to include Tag information of ESX host in my output. This tag is to indicate ownership of the host. I added 'Get-TagAssignment -Entity $_.Name' to my script and hoped it will do the job, But all of a sudden the script became extremely slow, and takes hours to pull just from one VC. We have a mix of 5.5 and 6.0 VCs and hosts. I know that in 5.5, tags have to be retrieved from inventory service., which might be slowing this down. Also i am not able to find a hostsystem property for Tag, that i can include with get-view. I guess this is again due to the fact that it is stored in inventory service and not in vcenter server. Can you please help me here, and find a faster way to get the info. Thanks in advance!

$vcenter = vcenter1

connect-viserver $vcenter

Get-View -ViewType HostSystem -Property Name,Parent,Runtime.ConnectionState,Config.Product,Hardware,VM -Server $vcenter | %{

        $obj = [ordered]@{

            vCenter = $vcenter

            Hostname = $_.Name

            Ownership = Get-TagAssignment -Entity $_.Name

            Cluster = &{

                $parent = Get-View -Id $_.Parent -Property Name,Parent

                while($parent -isnot [VMware.Vim.ClusterComputeResource] -and $parent -isnot [VMware.Vim.Datacenter]){

                    $parent = Get-View -Id $parent.Parent -Property Name,Parent

                }

                if($parent -is [VMware.Vim.Datacenter]){

                    ''

                    $script:sa = 'Yes'

                }

                else{

                    $parent.Name

                    $script:sa = 'No'

                }

            }

            Stand_Alone = $script:sa

            State = $_.runtime.connectionState

            ESX_Version =$_.config.product.version

            Build =$_.config.product.build

            Model =$_.hardware.systemInfo.model

            CPU_Sockets =$_.hardware.CpuInfo.numCpuPackages

            CPU_Cores =$_.hardware.CpuInfo.numCpuCores

            RAM_GB = [math]::Round((($_.hardware.memorySize)/(1GB)),1)

            VM = $_.vm.Count

        }

        $HostReport += New-Object PSObject -Property $obj

    }

Thanks,

VJ

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There is not really a faster way to retrieve the tags besides the cmdlet.
One improvement could be to retrieve all tag assignments for all ESXi nodes in one call to Get-TagAssignment.

Then store the results for example in a hash table. You can for example use the ESXi node's name as key.

That would definitely be faster than calling Get-TagAssignment for each ESXi node.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Did you create a TagCategory and did you assign Tags to the ESXi nodes?
Do you see the tags you assigned in the Web Client?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
VJ_VMware_111
Enthusiast
Enthusiast
Jump to solution

Yes, I created a new Category with Cardinality as single and EntityType as VMhost. Then created few Tags under this tag category and assigned these tags to ESXi hosts respectively, based on their ownership. All the hosts in our vcenters has a single tag attached to them.

Yes i see them in web client, as well as i can retrieve the tag with get-tagassignment  as well as vmhost -tag.

Thanks,

VJ

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There is not really a faster way to retrieve the tags besides the cmdlet.
One improvement could be to retrieve all tag assignments for all ESXi nodes in one call to Get-TagAssignment.

Then store the results for example in a hash table. You can for example use the ESXi node's name as key.

That would definitely be faster than calling Get-TagAssignment for each ESXi node.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
VJ_VMware_111
Enthusiast
Enthusiast
Jump to solution

This worked.

PowerCLI C:\Scripts\VMware_Inventory>$owner =@{}

PowerCLI C:\Scripts\VMware_Inventory>$owner = get-tagassignment

PowerCLI C:\Scripts\VMware_Inventory> $owner

Tag                                                           Entity

---                                                             ------

Sub_organization_OCIO/CORP               esx1

Sub_organization_OCIO/CORP               esx2

Sub_organization_OCIO/CORP               esx3

PowerCLI C:\Scripts\VMware_Inventory>$owner.GetEnumerator() | ?{ $_.entity.name -eq "esx1" } | Select -Expandproperty tag | Select -ExpandProperty Name

CORP

Reply
0 Kudos
VJ_VMware_111
Enthusiast
Enthusiast
Jump to solution

LucD,

If i try to integrate this with the main host script, i am not getting anything for ownership. Even without creating a separate function is not working. Can you please help here?

$HostReport = @()

$owner = @{}

function getowner {$owner.GetEnumerator() | ?{ $_.entity.name -eq $args } | Select -Expandproperty tag | Select -ExpandProperty Name}

Connect-VIServer vcenter1

   $owner = get-tagassignment

    Get-View -ViewType HostSystem -Property Name,Parent,Runtime.ConnectionState,Config.Product,Hardware,VM -Server $vcenter | %{

        $obj = [ordered]@{

            vCenter = $vcenter

            Hostname = $_.Name

            Ownership = getowner $_.Name

            IP = get-vmhostnetworkadapter -vmhost $_.name -name vmk0 | select -Expandproperty IP

            Cluster = &{

                $parent = Get-View -Id $_.Parent -Property Name,Parent

                while($parent -isnot [VMware.Vim.ClusterComputeResource] -and $parent -isnot [VMware.Vim.Datacenter]){

                    $parent = Get-View -Id $parent.Parent -Property Name,Parent

                }

                if($parent -is [VMware.Vim.Datacenter]){

                    ''

                    $script:sa = 'Yes'

                }

                else{

                    $parent.Name

                    $script:sa = 'No'

                }

            }

            Stand_Alone = $script:sa

            State = $_.runtime.connectionState

            ESX_Version =$_.config.product.version

            Build =$_.config.product.build

            Model =$_.hardware.systemInfo.model

            CPU_Sockets =$_.hardware.CpuInfo.numCpuPackages

            CPU_Cores =$_.hardware.CpuInfo.numCpuCores

            RAM_GB = [math]::Round((($_.hardware.memorySize)/(1GB)),1)

            VM = $_.vm.Count

        }

        $HostReport += New-Object PSObject -Property $obj

    }

    $owner = @{}

$HostReport | Export-Csv "C:\Results\HostReport.csv" -NoTypeInformation -UseCulture

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$HostReport = @()

Connect-VIServer -Server $vcenter

Get-View -ViewType HostSystem -Property Name,Parent,Runtime.ConnectionState,Config.Product,Hardware,VM -Server $vcenter | %{

    $obj = [ordered]@{

        vCenter = $vcenter

        Hostname = $_.Name

        Ownership = Get-TagAssignment -Entity $_.Name -Category 'Sub_Organization_OCIO' | select -ExpandProperty Tag | select -ExpandProperty Name

        IP = get-vmhostnetworkadapter -vmhost $_.name -name vmk0 | select -Expandproperty IP

        Cluster = &{

            $parent = Get-View -Id $_.Parent -Property Name,Parent

            while($parent -isnot [VMware.Vim.ClusterComputeResource] -and $parent -isnot [VMware.Vim.Datacenter]){

                $parent = Get-View -Id $parent.Parent -Property Name,Parent

            }

            if($parent -is [VMware.Vim.Datacenter]){

                ''

                $script:sa = 'Yes'

            }

            else{

                $parent.Name

                $script:sa = 'No'

            }

        }

        Stand_Alone = $script:sa

        State = $_.runtime.connectionState

        ESX_Version =$_.config.product.version

        Build =$_.config.product.build

        Model =$_.hardware.systemInfo.model

        CPU_Sockets =$_.hardware.CpuInfo.numCpuPackages

        CPU_Cores =$_.hardware.CpuInfo.numCpuCores

        RAM_GB = [math]::Round((($_.hardware.memorySize)/(1GB)),1)

        VM = $_.vm.Count

    }

    $HostReport += New-Object PSObject -Property $obj

}

$HostReport | Export-Csv "C:\Results\HostReport.csv" -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
VJ_VMware_111
Enthusiast
Enthusiast
Jump to solution

Thanks LucD. This time the script ran in 4 hours 46 min for ~15 VCs, which is very fine with us, as compared to 15 hours like in the beginning of the thread. We are good now. Thanks.

Reply
0 Kudos