Upgrading your VMFS datastores with PowerCLI


The Upgrade-VmfsDatastore function

The Upgrade-VmfsDatastore function is a PowerShell advanced function that will accept input from the pipeline. You can specify the datastores that you want to upgrade as input. If you use the -WhatIf parameter, it will only show what the function would do, but it will not do it. This is a great way to see in advance which datastore can be upgraded if you use the output of the Get-Datastore cmdlet as input.

function Upgrade-Vmfsdatastore {
    Upgrades a VMFS datastore.

    Upgrades a VMFS datastore from VMFS 3 to VMFS 5. 

  .PARAMETER  Datastore
    Specifies the datastores you want to upgrade.
    Shows what would happen if the function runs. The function is not run.

    PS C:\&gt; Upgrade-VmfsDatastore -Datastore (Get-Datastore -Name Datastore1,Datastore2)
    Upgrades VMFS datastores Datastore1 and Datastore2 to VMFS 5.

    PS C:\&gt; Get-Datastore | Upgrade-VmfsDatastore
    Upgrades all VMFS datastores to VMFS5.



    Author:  Robert van den Nieuwendijk
    Date:    11-10-2016
    Version: 1.1


    [parameter(Mandatory = $true,
               ValueFromPipeline = $true,
               ValueFromPipelineByPropertyName = $true)]
    [VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.VmfsDatastoreImpl[]] $Datastore,
    [Switch] $WhatIf

  process {
    foreach ($CurrentDatastore in $Datastore |
      Where-Object {
        $_.GetType().Name -eq 'VmfsDatastoreImpl' -and $_.FileSystemVersion -lt 5
      # If -WhatIf is used, only show what the function would do and don't do it
      if ($WhatIf)
        Write-Output "What if: Performing operation Upgrade-VmfsDatastore on Target $($CurrentDatastore.Name)."
        # Get the HostStorageSystem of the first host that the datastore is connected to
        # The UpgradeVmfs() method of this object is used to upgrade the datastore
        $HostStorageSystem = $CurrentDatastore |
          Get-VMHost | Select-Object -First 1 |
          Get-VMHostStorage | Get-View

        # Construct the path to the volume to upgrade
        # For example: /vmfs/volumes/4e97fa06-7fa61558-937e-984be163eb88
        $Volume = '/' + $CurrentDatastore.ExtensionData.Info.Url.TrimStart('ds:/').TrimEnd('/')
        # Upgrade the datastore
        # Return the datastore that has been upgraded

Listing 1. The Upgrade-VmfsDatastore function

There are some examples in the Upgrade-VmfsDatastore function’s comment-based help. Here are some more:

PowerCLI C:\> Get-Datastore -Name Datastore1 | Upgrade-VmfsDatastore -WhatIf
What if: Performing operation Upgrade-VmfsDatastore on Target Datastore1.

Name FreeSpaceGB CapacityGB
—- ———– ———-
Datastore1 103,861 499,750

I would love to hear it when you use the Upgrade-VmfsDatastore function to upgrade your datastores.


About Robert van den Nieuwendijk

2 Responses to Upgrading your VMFS datastores with PowerCLI

  1. Tony says:


    Excellent script, and I’m looking to use it to upgrade over 200 datastores after a recent upgrade from 4.0 to 5.5.

    Should $datastore on lines 60 & 78 actually be $CurrentDatastore?
    I ran it against 1 datastore with -whatif, and the Target was blank until I changed $Datastore to $CurrentDatastore.

    I then ran it against 2 datastores using Example 1 in the script, and it output both datastores for each upgrade.

    Thanks again for an excellent script, you are definitley one of the PowerCLI deities 🙂

Leave a Reply to Robert van den Nieuwendijk Cancel reply


WordPress.com Logo


Google photo


Twitter picture


Facebook photo


Connecting to %s

%d bloggers like this: