Upgrading your VMFS datastores with PowerCLI

PowerCLI logoWith the release of VMware vSphere 5.0 a new version of the VMFS datastores was released also: VMFS-5. This new VMFS-5 version has a lot of advantages over the old VMFS-3 version (see for information about the new features Cormac Hogan’s post vSphere 5.0 Storage Features Part 1 – VMFS-5). You can upgrade your VMFS-3 datastores to VMFS-5 while VM’s on the datastores keep running. In this post I will show you the vSphere PowerCLI Upgrade-VmfsDatastore function that will do the upgrade for you.

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 {
<#
  .SYNOPSIS
    Upgrades a VMFS datastore.

  .DESCRIPTION
    Upgrades a VMFS datastore from VMFS 3 to VMFS 5. 

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

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

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

  .INPUTS
    VmfsDatastoreImpl

  .OUTPUTS
    VmfsDatastoreImpl

  .NOTES
    Author:  Robert van den Nieuwendijk
    Date:    11-10-2016
    Version: 1.1
 
  .LINK
    https://rvdnieuwendijk.com/
    

#>

  [CmdletBinding()]
  [OutputType([VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.VmfsDatastoreImpl])]
  param(
    [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)."
      }
      else
      {
        # 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
        $HostStorageSystem.UpgradeVmfs($Volume)
        
        # Return the datastore that has been upgraded
        $CurrentDatastore
      }
    }
  }
}

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.
PowerCLI C:\> Get-Datastore -Name Datastore1 | Upgrade-VmfsDatastore

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.

Enjoy!

Advertisements

About Robert van den Nieuwendijk
Robert van den Nieuwendijk is a freelance senior systems engineer with over 30 years of experience in the IT industry. He focusses on VMware vCloud Suite and Microsoft Windows Server. He tries to automate as much of his work as possible using Microsoft PowerShell. Robert is the author of the books “Learning PowerCLI” and “Learning PowerCLI – Second Edition.” Robert is a frequent contributor and moderator at the VMware VMTN Communities. He has a bachelor degree in software engineering and holds the following IT certifications and accreditations: VSP 2016, VTSP 2016, VCP4-DCV, VCP5-DCV, VCP6-Cloud, VCP6-CMA, VCA-Cloud, VCA-WM, VCA-NV, VMSP, VMTSP, ZCS, ZCP, ZCP-Cloud, MCSE, MCSA, MCP, MCP+I, PRINCE2 Foundation and ITIL Foundation. In 2012, 2013, 2014, 2015, 2016 and 2017 Robert received the VMware vExpert award for his contribution to the community of VMware users over the past year. PernixData made him in 2015 a member of the PernixPro.

2 Responses to Upgrading your VMFS datastores with PowerCLI

  1. Tony says:

    Hi,

    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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: