How To Increase VMware Datastore Using Powershell or Powercli – VirtuallyThatGuy

This is a quick blog post on how to increase VMFS datastore size using powercli. I recently installed workstation on my desktop to run nested vSAN for testing. I found out that using the native UI took a while so took the challenge to use cli instead.

Script used below:

$ESXiServerName = "HP-ELITEDESK-ESXi.lab.local"                                                                 
$cred = Get-Credential

## I will then pass the $cred to connect to host                                                                
Connect-VIServer $ESXiServerName -Credential $cred

## Once connected I will get the datastores available to the host                                               
Get-Datastore

## we will be increasing the 80gb to additonal 5gb                                                              
## I will rescan hba and vmfs etc  first                                                                        
Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVmfs -Refresh

## Now the magic part                                                                                           
$datastore = get-datastore ELITEDESK-80GB                                                                       
$esxi = Get-View -Id ($Datastore.ExtensionData.Host |Select-Object -last 1 | Select -ExpandProperty Key)        
$datastoreSystem = Get-View -Id $esxi.ConfigManager.DatastoreSystem                                            
$expandOptions = $datastoreSystem.QueryVmfsDatastoreExpandOptions($datastore.ExtensionData.MoRef)              
$datastoreSystem.ExpandVmfsDatastore($datastore.ExtensionData.MoRef,$expandOptions.spec)

## This completes the increase and I will rename to reflect the size                                           

Get-Datastore -Name $datastore | Set-Datastore -name "ELITEDESK-85GB" -Confirm:$false

You May Also Like

About the Author: VirtuallyThatGuy

2 Comments

  1. updated version for you select the datastore

    $ESXiServerName = “HP-ELITEDESK-ESXi.lab.local”
    $cred = Get-Credential

    ## I will then pass the $cred to connect to host
    Connect-VIServer $ESXiServerName -Credential $cred

    ## Once connected I will get the datastores available to the host
    Get-Datastore

    $selecteddatastore = Get-Datastore | Out-GridView -Title ‘Choose one or more datastore’ -PassThru

    ## we will be increasing the 80gb to additonal 5gb
    ## I will rescan hba and vmfs etc first
    Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVmfs -Refresh

    foreach($datastore1 in $selecteddatastore) {
    ## Now the magic part
    $datastore = get-datastore $datastore1.Name
    $esxi = Get-View -Id ($Datastore.ExtensionData.Host |Select-Object -last 1 | Select -ExpandProperty Key)
    $datastoreSystem = Get-View -Id $esxi.ConfigManager.DatastoreSystem
    $expandOptions = $datastoreSystem.QueryVmfsDatastoreExpandOptions($datastore.ExtensionData.MoRef)
    $datastoreSystem.ExpandVmfsDatastore($datastore.ExtensionData.MoRef,$expandOptions.spec)

    ## This completes the increase and I will rename to reflect the size

    Get-Datastore -Name $datastore | Set-Datastore -name $datastore1.Name -Confirm:$false

    }

Leave a Reply

Your email address will not be published. Required fields are marked *