PowerCLI

  • 1.  Pulling Tag Category info into inventory script?

    Posted Feb 06, 2025 02:27 PM

    We have an inventory script that I have been able to modify that gives a good overall view of our environment, that I can then filter in the spreadsheet to show whichever subset we want.  However, I need to add a few specific categories of tags (Application, Criticality, Test/Dev, etc.) in their own specific columns so we can asses who's responsible for a given VM and how important it is.  There is a section in ExtensionData that has a Tag category, but it seems to be empty with no data, and I know that with rare exception, every VM should have at least one tag.  How can I get specific Tag categories into my inventory?


    Clear-Host
    # Where you want to save the CSV file
    $logFile = "h:\scripts\Inventory\InventoryAllVMs2.csv" 
    # Filters which cluster you want to pull VM stats from.
    $vcsCluster = "*" 

    $vms = Get-Cluster -Name $vcsCluster | Get-VM
    $count = 0
    $results = @()
    $Script:ProgressPreferenceOriginal = $ProgressPreference
    Clear-Host
    foreach($vm in $vms){
    # Progress Bar setup
    $count++
    $percentComplete = [math]::Round(($count / $vms.Count) * 100,1)
    Write-Progress -Activity "Collecting info on $($vm.Name)" -Status "$percentComplete% Complete" -PercentComplete $percentComplete

    # Store VM stat info in PSObject
    $object = New-Object PSObject
    Add-Member -InputObject $object -MemberType NoteProperty -Name Name -Value $vm.Name
    Add-Member -InputObject $object -MemberType NoteProperty -Name PowerState -Value $vm.PowerState
    Add-Member -InputObject $object -MemberType NoteProperty -Name Cluster -Value ($vm | Get-Cluster).Name
    Add-Member -InputObject $object -MemberType NoteProperty -Name Host -Value $vm.VMHost
    Add-Member -InputObject $object -MemberType NoteProperty -Name CPU -Value $vm.NumCpu
    Add-Member -InputObject $object -MemberType NoteProperty -Name Sockets -Value ($vm.NumCpu / $vm.CoresPerSocket)
    Add-Member -InputObject $object -MemberType NoteProperty -Name CoresPerSocket -Value $vm.CoresPerSocket
    Add-Member -InputObject $object -MemberType NoteProperty -Name MemoryGB -Value $vm.MemoryGB
    Add-Member -InputObject $object -MemberType NoteProperty -Name OperatingSystem -Value $vm.Guest.OSFullName
    Add-Member -InputObject $object -MemberType NoteProperty -Name IPAddress -Value ($vm.Guest.IPAddress -join ", ")
    Add-Member -InputObject $object -MemberType NoteProperty -Name UsedSpaceGB -Value ([math]::Round($vm.UsedSpaceGB, 0))
    Add-Member -InputObject $object -MemberType NoteProperty -Name ProvisionedSpaceGB -Value ([math]::Round($vm.ProvisionedSpaceGB,0))
    Add-Member -InputObject $object -MemberType NoteProperty -Name Notes -Value $vm.Notes
    Add-Member -InputObject $object -MemberType NoteProperty -Name HardwareVersion -Value $vm.HardwareVersion
    Add-Member -InputObject $object -MemberType NoteProperty -Name ToolsStatus -Value $vm.ExtensionData.Guest.ToolsStatus
    Add-Member -InputObject $object -MemberType NoteProperty -Name ToolsUpgrade -Value $vm.ExtensionData.Guest.ToolsVersionStatus
    Add-Member -InputObject $object -MemberType NoteProperty -Name ToolsVersion -Value $vm.ExtensionData.Guest.ToolsVersion
    Add-Member -InputObject $object -MemberType NoteProperty -Name Template -Value $vm.ExtensionData.Config.Template
    # below don't work yet or like I would prefer
    Add-Member -InputObject $object -MemberType NoteProperty -Name Snapshot -Value $vm.ExtensionData.RootSnapshot
    Add-Member -InputObject $object -MemberType NoteProperty -Name DVPortgroup -Value $vm.ExtensionData.Network
    # add Tags, make Snapshot more useful, PortGroup with useful detail?

    # Stores the PSObject containing VM stats in to an PSObject array
    $results += $object
    }

    # The Sort-Object is specifically set up so that the Export-Csv and Out-GridView do not truncate the properties of the individual PSObjects in
    # the array.
    $results | Out-GridView
    $results | Export-Csv -Path $logFile -NoTypeInformation



  • 2.  RE: Pulling Tag Category info into inventory script?

    Posted Feb 06, 2025 02:29 PM

    Did you check with the Get-TagAssignment cmdlet?



    ------------------------------


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


    ------------------------------



  • 3.  RE: Pulling Tag Category info into inventory script?

    Posted Feb 06, 2025 02:35 PM

    I assumed it would be involved, but I didn't have any idea how to integrate it into what I had there.  It's borrowed from somewhere else, and I did figure out how to add other columns (about half of those I derived).




  • 4.  RE: Pulling Tag Category info into inventory script?
    Best Answer

    Posted Feb 06, 2025 05:08 PM

    You could do something like this for example

    Add-Member -InputObject $object -MemberType NoteProperty -Name 'ApplicationTag' -Value (Get-TagAssignment -Entity $vm -Category 'Application').Tag.Name



    ------------------------------


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


    ------------------------------



  • 5.  RE: Pulling Tag Category info into inventory script?

    Posted Feb 06, 2025 05:27 PM

    Thanks so much.  I was thinking it had to be done somehow more complicated, as I was just pulling config data, and the thought of just plopping a one-liner in there didn't even register.  Ironically, I can figure out one liners, and that's about all so far, it's just integrating them into anything else throws me.

    You have really taken a weight off my shoulders, since previously we had to do some really unwieldy stuff to get that information.  Getting tags as a randomly ordered lump of data was a headache!