Creating a VSAN Cluster with PowerCLI

Recently there was a new fling released on the VMware Flings site, this enables us to use VSAN and VMware Flash Read Cache with PowerCLI, check out the site here for more details.

I wrote a post on the PowerCLI Blog which showed the cmdlets and some examples on how you might use these, recently I had the chance to put them to use when I needed to stand up a new VSAN cluster, I was able to easily build myself a test cluster with the help of Williams post here.

Once I had a couple of hosts and my fresh vCenter server, I used the following script to setup the Datacenter, Cluster, add the hosts, enable the VSAN VMKernel port and enable VSAN in automatic mode.  The cmdlets are very easy to use and I was amazed at how easy and quick it is to setup VSAN.



The script

import-module VMware.VimAutomation.Extensions
Connect-VIServer -User root -Password vmware
$Datacenter = "DC01"
$Cluster = "VSAN Cluster"
$ESXHosts = "", ""
$ESXUser = "root"
$ESXPWD = "vmware"
$VMKNetforVSAN = "Management Network"

# If doesnt exist create the datacenter
If (-Not ($NewDatacenter = Get-Datacenter $Datacenter -ErrorAction SilentlyContinue)){ 
	Write-Host "Adding $Datacenter"
	$NewDatacenter = New-Datacenter -Name $Datacenter -Location (Get-Folder Datacenters) 

# Create the initial cluster
if (-Not ($NewCluster = Get-Cluster $Cluster -ErrorAction SilentlyContinue)) { 
	Write-Host "Adding $Cluster"
	$NewCluster = New-Cluster -Name $Cluster -Location $NewDatacenter 

# For each of our hosts
$ESXHosts | Foreach {
	Write-Host "Adding $($_) to $($NewCluster)"
	# Add them to the cluster
	$AddedHost = Add-VMHost -Name $_ -Location $NewCluster -User $ESXUser -Password $ESXPWD -Force
	# Check to see if they have a VSAN enabled VMKernel
	$VMKernel = $AddedHost | Get-VMHostNetworkAdapter -VMKernel | Where {$_.PortGroupName -eq $VMKNetforVSAN }
	$IsVSANEnabled = $VMKernel | Where { $_.VsanTrafficEnabled}
	# If it isnt Enabled then Enable it
	If (-not $IsVSANEnabled) {
		Write-Host "Enabling VSAN Kernel on $VMKernel"
		$VMKernel | Set-VMHostNetworkAdapter -VsanTrafficEnabled $true -Confirm:$false | Out-Null
	} Else {
		Write-Host "VSAN Kernel already enabled on $VmKernel"
		$IsVSANEnabled | Select VMhost, DeviceName, IP, PortGroupName, VSANTrafficEnabled
# Enable VSAN on the cluster and set to Automatic Disk Claim Mode
Write-Host "Enabling VSAN on $NewCluster"
$VSANCluster = $NewCluster | Set-Cluster -VsanEnabled:$true -VsanDiskClaimMode Automatic -Confirm:$false -ErrorAction SilentlyContinue
If ($VSANCluster.VSANEnabled){
	Write-Host "VSAN cluster $($VSANCLuster.Name) created in $($VSANCluster.VSANDiskClaimMode) configuration"
	Write-Host "The following Hosts and Disk Groups now exist:"
	Get-VsanDiskGroup | Select VMHost, Name | FT -AutoSize
	Write-Host "The following VSAN Datastore now exists:"
	Get-Datastore | Where {$_.Type -eq "vsan"} | Select Name, Type, FreeSpaceGB, CapacityGB
} Else {
	Write-Host "Something went wrong, VSAN not enabled"

6 thoughts on “Creating a VSAN Cluster with PowerCLI

  1. Pingback: Consolidated list of all Virtual SAN (VSAN) deep dive resources. |

  2. Pingback: Exploring VSAN APIs Part 1 – Enable VSAN Cluster | virtuallyGhetto

  3. Pingback: Welcome to vSphere-land! » VSAN Links

  4. Pingback: Creating a VSAN Cluster with PowerCLI - VSAN Me!

  5. Pingback: Creating a manual VSAN Cluster with PowerCLI - Virtu-Al.Net

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.