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
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)
{
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
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.
Author of the upcoming book: Managing VMware Infrastructure with PowerShell
Co-Host, PowerScripting Podcast (http://powerscripting.net)
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
Thanks for your input, i'll test your inputs whan i get home.
Regards
A. Mikkelsen
Thanks LucD
Your solution worked like it should.
Regards
A. Mikkelsen