VMware Cloud Community
A_S
Enthusiast
Enthusiast

howto change Hard disk(s) size(s) (the SDK way?) in one go?

Hi,

I would like to change the Hard disk sizes (VMDK files) in one go (the SDK way?)

# I'd like to put all required Hard Disk # and SizeInGB in one string array seperated by "|"

syntax: <Hard disk #>;<newSizeinGB>|<Hard disk #>;<newSizeinGB>

sample: $VMDKParams=1;30|2;50

the pseudocode should be like this...

split string

For each hard disk ($vm.config.hardware.virtualdevice.virtualdisk().Info.label?) of VM

     if hard disk # = <givenparam> and currentsizeof hardDiskinGB -lt <newSizeInGB>

          if datastore where this hard disk reside has enough space left to acommodate desired increase

            increase the size of VMDK to <newSizeinGB>

          else

            return "no space left on datastore"

          end if

     end if

next for

anyone putting this in working powershell code...

thanks in advance

0 Kudos
5 Replies
LucD
Leadership
Leadership

If there is no specific reason to do this the SDK way, I would suggest to use the Set-Harddisk cmdlet.

Your pseudo-code converted to PowerCLI could look something like this

$hdSizes = @{
    1 = 30;
    2 = 50}

Get-VM  | Get-HardDisk | %{
    $CapacityGB = $_.CapacityKB/1MB
    $hdId = [int]($_.Name.Replace("Hard disk ",""))
    if($CapacityGB -lt $hdSizes[$hdId]){
        $ds = Get-View $_.Extensiondata.Backing.Datastore
        if($ds.Summary.FreeSpace/1GB -gt ($hdSizes[$hdId] - $CapacityGB)){
            Set-HardDisk -CapacityKB ($hdSizes[$hdId] * 1MB) -HardDisk $_ -Confirm:$false
        }        
else{             Write-Host "Insufficient free space on" $ds.Name "to extend Hard disk" $hdId "on" $_.Parent.Name         }     } }

The script uses a hash table to store the hard disk Id and the required size in GB.

That's easier to work with than a string that needs to be split several ways.


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

0 Kudos
A_S
Enthusiast
Enthusiast

Great,

I wil (try) combine this script with diskpart 'extend volume' command.

Thank you

A.S.

0 Kudos
AlbertWT
Virtuoso
Virtuoso

Yes, that sounds like a good idea. Please share it here when you're done with the script.

/* Please feel free to provide any comments or input you may have. */
0 Kudos
AureusStone
Expert
Expert

For some reason my post didn't go through.

I would recommend using VIX to copy your scripts and ExtPart to extend as it can extend system drives on XP and 2003

You can use Set-Harddisk and it will use Invoke-VMscript on the diskpart scripts in \VMware\Infrastructure\vSphere PowerCLI\Scripts but I think ExtPart is better.

Edit: This link should hopefully link you to a chapter from VMware VSphere PowerCLI Reference: Automating VSphere Administration that describes how to use Set-Harddisk to extend a volume.  Thanks to LucD and all the other guys involved in this book.

http://books.google.com.au/books?id=w-rBbtkYYW0C&pg=PT230&lpg=PT230&dq=powercli+extend+disk&source=b...

0 Kudos
A_S
Enthusiast
Enthusiast

I bought this book immediately. Great companion.

But let's extend this function first before we do the ext-part.

$hdSizes = @{
    1 = 30;
    2 = 50}

Get-VM | Get-HardDisk | %{
    $CapacityGB = $_.CapacityKB/1MB
    $hdId = [int]($_.Name.Replace("Hard disk ",""))
    if($CapacityGB -lt $hdSizes[$hdId]){
        $ds = Get-View $_.Extensiondata.Backing.Datastore
        if($ds.Summary.FreeSpace/1GB -gt ($hdSizes[$hdId] - $CapacityGB)){
            Set-HardDisk -CapacityKB ($hdSizes[$hdId] * 1MB) -HardDisk $_ -Confirm:$false
        }
        else{
            # Write-Host "Insufficient free space on" $ds.Name "to extend Hard disk" $hdId "on" $_.Parent.Name

            GetDatastore_with_most_freespace_connectedto_VMhost_ofThis_VM

            # since VMDKs can be hosted on different datastores (except Hard Disk1 because we want to keep it in same folder as .vmx)

            if ($hdId -gt 1) {

                    SVMotion_this_VMDK_to_that_Datastore_with_most_freespace_connectedto_VMhost_ofThis_VM

                    Call this method again but at this time the VMDK is moved to another datastore with enough space to accomodate the size increase.

            }
        }
    }

0 Kudos