Automation

 View Only
  • 1.  Removing character strings within a hashtable

    Posted Jul 10, 2013 06:42 PM

    Hello all PowerCLI'ers out there.  Here's what I hope will be an easy one for you guys.

    I have built a hashtable named $hostInfo that contains information about our VMHosts.  The information when displayed in a table format shows Name, UsedMemoryGB, TotalMemoryGB, FreeMemoryPct.

    The 'Format-Table' output looks like this:

    Name                                                               UsedMemoryGB             TotalMemoryGB                        FreeMemoryPct
    ---------                                                               -----------------------             ------------------------                        -----------------------
    us1esx0201.company.local                              89.07421875                  95.989414215087890625           92.79587700202785441462201182
    us1esx0209.company.local                              84.8193359375              95.989414215087890625            88.36321862267168020854706439
    us1esx0211.company.local                              83.681640625                95.989414215087890625            87.17798864517570982753322143
    us1esx0205.company.local                              83.3310546875              95.989414215087890625            86.81275468644519191613067240
    us1esx0403.company.local                              96.3408203125              111.989383697509765625           86.02674390344222204017427325
    us1esx0401.company.local                              95.689453125                111.989383697509765625          85.44511092539192491691602221
    us1-vmhesx-p0515.company.local                     81.6171875                   95.989383697509765625            85.02730651672824441211219264
    us1esx0303.company.local                              95.1884765625               111.989383697509765625          84.99776802023479984310570319
    us1-vmhesx-p0512.company.local                    81.4462890625               95.9893798828125                    84.84927099428367955541142883
    us1esx0113.company.local                              81.3212890625               95.98944091796875                  84.71899438605549341860543489

    Here is the code snippet that generates this hashtable:

    $hostInfo = Get-VMHost |

        Select Name, `

        @{N="UsedMemoryGB";E={$_.MemoryUsageGB}}, `

        @{N="TotalMemoryGB";E={$_.MemoryTotalGB}}, `

        @{N="FreeMemoryPct";E={(($_.MemoryUsageGB / $_.MemoryTotalGB) * 100)}} |

        Sort-Object -Descending -Property "FreeMemoryPct" | Select -First 10

    Here is my challenge...I wish to remove the ".company.local" from all the entries in the 'Name' column and only leave the actual hostname.  How would I go about iterating through the 'Name' key and removing only this part of the string?

    Any help is appreciated as always!

    -jSun311



  • 2.  RE: Removing character strings within a hashtable
    Best Answer

    Posted Jul 10, 2013 07:04 PM

    You can create the table in the right format with just the name of the hosts if you modify the second line of your script into:

    Select @{N="Name";E={$_.Name.Split('.')[0]}},

    If you want to modify the $hostinfo variable then you can use:

    $NewHostinfo = $hostinfo | Select-Object -Property @{N="Name";E={$_.Name.Split('.')[0]}},UsedMemoryGB,TotalMemoryGB,FreeMemoryPct