1 2 3 Previous Next 36 Replies Latest reply on May 15, 2019 2:49 AM by markshannon Go to original post
      • 30. Re: Exporting VM Tags in readable format
        LucD Guru
        User ModeratorsvExpertCommunity Warriors

        Yes, I restarted from scratch

        • 31. Re: Exporting VM Tags in readable format
          markshannon Novice

          yes that worked   thankyou

           

          i have a tag category named 'machine information' that has multiple tags against it (tag category could be named anything),  in the report it lists all the tags in same excel field like below with pipe splitting the tags:-

           

          Machine Information

          Production|Owner IT|Non SQL|Internal Facing

           

          how easy is it to split these out?

           

          ps.  your 2nd ed book arrived yesterday

          • 32. Re: Exporting VM Tags in readable format
            LucD Guru
            vExpertCommunity WarriorsUser Moderators

            Splitting out is not a problem, but how would you name those columns?

            • 33. Re: Exporting VM Tags in readable format
              markshannon Novice

              good question i dont know!!!

               

              i guess it needs to dynamic if possible in case new categories are created with multiple tags?

               

              to be easier, could just another column be created called the same whatever the category is called so in this example we would just have 4 columns called 'machine information'?

               

              i say easier, thats why im here asking ha!!!

              • 34. Re: Exporting VM Tags in readable format
                LucD Guru
                vExpertCommunity WarriorsUser Moderators

                I'm afraid not, you can't have columns with the same name.
                And Export-Csv has an issue exporting arrays where not all rows have the same number of properties.

                 

                We could first calculate how many columns we would end up with, and then create those columns for all rows.

                • 35. Re: Exporting VM Tags in readable format
                  markshannon Novice

                  that would work

                  • 36. Re: Exporting VM Tags in readable format
                    LucD Guru
                    User ModeratorsCommunity WarriorsvExpert

                    The code becomes a bit more complex, but this should do the trick.

                     

                    $assigned = Get-VM | Get-TagAssignment

                    $tagCat = $assigned.Tag.Category.Name | Sort-Object -Unique


                    $report = $assigned | Group-Object -Property { $_.Entity.Name } |

                    ForEach-Object -Process {

                       $vm = $_

                       $obj = [ordered]@{

                       VM = $vm.Name

                       }

                       $tagCat | ForEach-Object -Process {

                       $cat = $_

                       $tags = ($vm.Group | where { $_.Tag.Category.Name -eq $cat }).Tag.Name -join '|'

                       $obj.Add($cat, $tags)

                       }

                       New-Object PSObject -Property $obj

                    }


                    $colTab = @{ }

                    $report | Get-Member -MemberType NoteProperty | where { $_.Name -ne 'VM' } |

                    ForEach-Object -Process {

                       $column = $report."$($_.Name)"

                       $columnMax = ($column | where { $_ -ne '' } | % {

                       $_.Split('|').Count

                       } | Measure-Object -Maximum).Maximum

                       $colTab.Add($_.Name, $columnMax)

                    }

                    $report | ForEach-Object -Process {

                       $row = $_

                       $obj = [ordered]@{

                       VM = $_.VM

                       }

                       $colTab.GetEnumerator() | Sort-Object -Property Name | ForEach-Object -Process {

                       if ($_.Value -gt 1)

                       {

                       $col = $_

                       $values = ($row."$($_.Name)").Split('|')

                       1..($_.Value) | ForEach-Object -Process {

                       $obj.Add("$($col.Name)-$_", $values[$_ - 1])

                       }

                       }

                       else

                       {

                       $obj.Add($_.Name, $row."$($_.Name)")

                       }

                       }

                       New-Object PSObject -Property $obj

                    }

                    1 person found this helpful
                    • 37. Re: Exporting VM Tags in readable format
                      markshannon Novice

                      Legend works an absolute treat.

                       

                      appreciate your assistance on this - now to decipher your code so i understand it

                      1 2 3 Previous Next