VMware Cloud Community
A_Mikkelsen
Expert
Expert

Adding info to custom fields in VC

Hi,

I'm trying to convert a VIPerl scrip to VI Toolkit (Powershell) and i'm kind of stuck - so i'm hoping someone can help me Smiley Happy

The basic og the script is to display extra information about each VM in VC, like Disk size, LUNS, vmx path, etc...

I have found a way to create the information in VC using VI Powershell but i keep getting some weird text in the fields.

But the below code adds "@{Filename=[Local] XP01/XP01.vmdk}" to the customfield "HDD0path"

How do i get rid of the "@{Filename={" in the beginning and the "}" in the end

My code:

___________________________________________________________

foreach ($vm in Get-VM XP01)

{

$hddpath = $vm |Get-HardDisk | Select FileName

$counter = 0

foreach ($drive in $hddpath)

{

  1. Write the HDD path to custom VC field

$vm | Set-CustomField -Name "HDD${counter}path" -Value $drive

$counter =+ 1

}

}

___________________________________________________________

Regards

A. Mikkelsen

A real PowerShellnewbie Smiley Sad

If you found this or any other answer useful please consider the use of the Helpful or correct buttons to award points. Regards A. Mikkelsen
0 Kudos
5 Replies
halr9000
Commander
Commander

How do i get rid of the "@{Filename={" in the beginning and the "}" in the end

$hddpath = $vm |Get-HardDisk | Select FileName

Long story short, do it this way:

$hddpath = ($vm |Get-HardDisk)[0].FileName

The @{} stuff is a powershell hashtable. As you probably realize, a VM can have more than one hard disk, hence the array selector. And the reason why the hashtable is because Select-Object when used this way creates a new object containing the properties you specify. Maybe this will help explain:

§ ATLLAPHROTTENBE {~} $a = Get-VM | Get-HardDisk | select filename
§ ATLLAPHROTTENBE {~} $a

Filename
--------
[SAN_VOL1] PHBANDEV-svustika-01/PHBANDEV-svustika-01.vmdk
[SAS_PHATLESX07] phatlvc02/phatlvc02.vmdk
[SAS_PHATLESX07] phatlvc02/phatlvc02_1.vmdk


§ ATLLAPHROTTENBE {~} $a[0]

Filename
--------
[SAN_VOL1] PHBANDEV-svustika-01/PHBANDEV-svustika-01.vmdk


§ ATLLAPHROTTENBE {~} $a[0].Filename
[SAN_VOL1] PHBANDEV-svustika-01/PHBANDEV-svustika-01.vmdk

Or maybe I've made things worse. Smiley Happy






Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
LucD
Leadership
Leadership

Hal's explanation about the hashtable is correct.

But I think you want to see the filename for each hard disk, so instead of showing the hashtable only show the desired property.

So your script should be

foreach ($vm in Get-VM MVV30*){
  $hddpath = $vm |Get-HardDisk | Select FileName
  $counter = 0
  foreach ($drive in $hddpath){
# Write the HDD path to custom VC field
    $vm | Set-CustomField -Name "HDD${counter}path" -Value $drive.Filename
    $counter =+ 1
  }
}


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

A_Mikkelsen
Expert
Expert

Thanks for your input, i'll test your inputs whan i get home.

Regards

A. Mikkelsen

If you found this or any other answer useful please consider the use of the Helpful or correct buttons to award points. Regards A. Mikkelsen
0 Kudos
A_Mikkelsen
Expert
Expert

Thanks LucD

Your solution worked like it should.

Regards

A. Mikkelsen

If you found this or any other answer useful please consider the use of the Helpful or correct buttons to award points. Regards A. Mikkelsen
0 Kudos
A_Mikkelsen
Expert
Expert

If you wan't to catch a preview of my project please visit

Regards

A. Mikkelsen

If you found this or any other answer useful please consider the use of the Helpful or correct buttons to award points. Regards A. Mikkelsen
0 Kudos