VMware Cloud Community
alanrenouf
VMware Employee
VMware Employee
Jump to solution

vmdk size

Hi,

Im trying to get the size of the vmdk added to the following script from www.peetersonline.nl, anyone have any ideas...

$VCserverName = "myVCserver.domain.local"

$outputFile = "D:\scripts\diskfiles.csv"

$VC = Connect-VIServer $VCserverName

$VMsAdv = Get-VM | Sort-Object Name | % { Get-View $_.ID }

$myCol = @()

ForEach ($VMAdv in $VMsAdv)

{

ForEach ($Disk in $VMAdv.Layout.Disk)

{

$myObj = "" | Select-Object Name, Disk

$myObj.Name = $VMAdv.Name

$myObj.Disk = $Disk.DiskFile[0]

$myCol += $myObj

}

}

$myCol | Export-Csv $outputFile -NoTypeInformation

Thanks

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The Get-HardDisk cmdlet returns the disksize so there is no reason to fetch this from the VirtualMachine object.

This should do the trick

$col = @()

Get-VM | %{
  $disk = $_ | Get-HardDisk
  if (($disk.GetType()).Name -eq "Object[]"){
    foreach($d in $disk){
      $row = "" | select VMname, DiskName, DiskSize
      $row.VMname = $_.Name
      $row.DiskName = $d.Filename
      $row.DiskSize = $d.CapacityKb
      $col += $row
	}
  }
  else{
    $row = "" | select VMname, DiskName, DiskSize
    $row.VMname = $_.Name
    $row.DiskName = $disk.Filename
    $row.DiskSize = $disk.CapacityKb
    $Global:col += $row
  }
}
$col | Export-Csv "c:\Temp\diskreport.csv" -noTypeInformation

Note1: a guest can have 1 or more hard disks hence the test for Object[]

Note2: the Get-HardDisk cmdlet returns the disk capacity in Kb. If another unit is required use the builtin constants like 1Kb, 1Mb...


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

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

The Get-HardDisk cmdlet returns the disksize so there is no reason to fetch this from the VirtualMachine object.

This should do the trick

$col = @()

Get-VM | %{
  $disk = $_ | Get-HardDisk
  if (($disk.GetType()).Name -eq "Object[]"){
    foreach($d in $disk){
      $row = "" | select VMname, DiskName, DiskSize
      $row.VMname = $_.Name
      $row.DiskName = $d.Filename
      $row.DiskSize = $d.CapacityKb
      $col += $row
	}
  }
  else{
    $row = "" | select VMname, DiskName, DiskSize
    $row.VMname = $_.Name
    $row.DiskName = $disk.Filename
    $row.DiskSize = $disk.CapacityKb
    $Global:col += $row
  }
}
$col | Export-Csv "c:\Temp\diskreport.csv" -noTypeInformation

Note1: a guest can have 1 or more hard disks hence the test for Object[]

Note2: the Get-HardDisk cmdlet returns the disk capacity in Kb. If another unit is required use the builtin constants like 1Kb, 1Mb...


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

0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Thats great thanks, they should rename this forum to 'Ask LucD' :smileygrin:

Hope you enjoyed VMWorld 2008 !

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
LucD
Leadership
Leadership
Jump to solution

VMWorld Las Vegas was great!

A lot bigger than VMWorld Europe.


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

0 Kudos
fabbio75
Contributor
Contributor
Jump to solution

It works .. but .. if i want to know how many free space is available on each hd ?

I use this :

$reportVM = @()

Get-VM | %{

$vm = $_ | Get-View

foreach($dsImpl in $vm.Datastore){ $row = "" | select-Object NomeVM, DiscoTot, DiscoFree

$row.NomeVM = $_.Name

$row.DiscoTot = $vm.Guest.Disk[0].Capacity

$row.DiscoFree = $vm.Guest.Disk[0].FreeSpace

$reportVM += $row

}

}

$reportVM | export-csv C:\scripts\Report-VMachine.csv -NoTypeInformation

but it takes me only first hd and it doesn't work if there's no vmtools installed!!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You only get the first HD because you only select the first one (Guest.Disk[0]).

If you want all you should use for example a foreach loop.

When VMware tools are not installed or if the machine is offline, and the VMware Tools evidently do not run, this information is not accessible.

For an alternative (using the VDDK) see .


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

0 Kudos
fabbio75
Contributor
Contributor
Jump to solution

Yes i know .. i wrote this :

$reportVM = @()

Get-VM | %{

$vm = $_ | Get-View

foreach($dsImpl in $vm.Datastore){ $row = "" | select-Object NomeVM, DiscoTot, DiscoFree

$row.NomeVM = $_.Name

$count = 0

$Numdisks = ($_.harddisks | Measure-Object).count

while ($count -ne $Numdisks )

{

$row = "" | select VMname, DiskName, DiskSize, Discofree

$row.VMname = $_.Name

$row.DiskName = $_.Guest.Disks[$count].Path

$row.DiskSize = $_.Guest.Disks[$count].Capacity

$row.DiscoFree = $_.Guest.Disks[$count].FreeSpace

$count = $count + 1

$reportVM += $row

}

}

}

$reportVM | export-csv C:\scripts\Report-VMachine.csv -NoTypeInformation

Tell me about this please...

0 Kudos
IT_Guy1
Contributor
Contributor
Jump to solution

For some reason when I run this I get about 66% of the vm's I have, the remaining dont show up in my output file but I do get the following errors.

+ $Global:col += <<<< $row

Method invocation failed because http://System.Management.Automation.PSObject doesn't contain a method named 'op_Addition'.

At C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\scripts\VI-vm-disk-report.ps1:19 char:19

This make sense to anyone?

0 Kudos
breizhdude
Enthusiast
Enthusiast
Jump to solution

<code>Replace "*$Global:col += $row*" by </code><code>"*$col += $row*", the should work fine</code>

0 Kudos
IT_Guy1
Contributor
Contributor
Jump to solution

How could I miss such an easy mistake??????

Thanks a bunch

0 Kudos