OK, today I would like to try making a script to display virtual hard disk information under Powershell.  Here is a simple little script that will do the job:

Param([String]$vhdName)

$vs=new-object –com VirtualServer.Application –Strict

$result = SetSecurity($vs)

$vhd = $vs.GetHardDisk($vhdName)

$result = SetSecurity($vhd)

$vhd

As you can see: it connects to the Virtual Server COM interface, creates a virtual hard disk COM object (using the virtual hard disk file string that is provided as a parameter for the script), and outputs the contents of that object.  This will result in an output something like this:

File                : E:\My Virtual Machines\Amiga Research OS\Amiga Research OS Hard Disk.vhd
Type                : 0
Security            : System.__ComObject
SizeInGuest         : 17179869184
SizeOnHost          : 35328
HostFreeDiskSpace   : 7816654848
Parent              :
HostDriveIdentifier :

Which works, I guess, but there are a number of ugly aspects to this.

The type is displayed as a numeric identifier, not a meaningful type description.  The parent and host drive identifier fields are always displayed, even when they are not appropriate for the current hard disk type.  And the security and parent fields will always report 'System.__ComObject' as they are actually returning secured COM objects.

So for a slightly more complex approach, let's try this:

Param([String]$vhdName)

# Define constants
set-variable -name vmDiskType_Dynamic -value 0 -option constant
set-variable -name vmDiskType_FixedSize -value 1 -option constant
set-variable -name vmDiskType_Differencing -value 2 -option constant
set-variable -name vmDiskType_HostDrive -value 4 -option constant

$vs=new-object –com VirtualServer.Application –Strict

$result = SetSecurity($vs)

$vhd = $vs.GetHardDisk($vhdName)

$result = SetSecurity($vhd)

write-host
write-host "VHD Information:"
write-host 
write-host "File Name           : " $vhd.File
write-host "SizeInGuest         : " $vhd.SizeInGuest
write-host "SizeOnHost          : " $vhd.SizeOnHost
write-host "HostFreeDiskSpace   : " $vhd.HostFreeDiskSpace

switch ($vhd.type)
   {
      $vmDiskType_Dynamic
      {
         write-host "Type                :  Dynamically Expanding VHD"
      }

      $vmDiskType_FixedSize
      {
         write-host "Type                :  Fixed Size VHD"
      }

      $vmDiskType_Differencing
      {
         $parentVhd = $vhd.Parent
         $result = SetSecurity($parentVhd)

         write-host "Type                :  Differencing VHD"
         write-host "Parent              : " $parentVhd.File
      }

      $vmDiskType_HostDrive
      {
         write-host "Type                :  Linked VHD"
         write-host "HostDriveIdentifier : " $vhd.HostDriveIdentifier
      }
   }

write-host

Once again we connect to Virtual Server and create the virtual hard disk COM object using the provided parameter.  However this time we display the common virtual hard disk information manually and then use a switch operation on the virtual hard disk type.  I'm using some defined constants for this switch operation to make it easier to read.  The script then displays only the appropriate information for each virtual hard disk type.  This time the output looks something like this:

VHD Information:

File Name           :  E:\My Virtual Machines\Amiga Research OS\Amiga Research OS Hard Disk.vhd
SizeInGuest         :  17179869184
SizeOnHost          :  35328
HostFreeDiskSpace   :  7816654848
Type                :  Dynamically Expanding VHD

See?  Much nicer! Once again, in order for this to work you need to have your Powershell environment setup as detailed in this post and this post.

Cheers,
Ben