= Script: PowerCLI: Deploy Storage to vSphere =
**Summary**: A script to configure storage in vSphere. \\
**Date**: 29 December 2011 \\
**Refactor**: 21 February 2025: Checked links and formatting. \\
{{tag>vmware powershell netapp}}
I'm actually quite proud on this script. It will create a volume, LUNs, connects the LUNs to the required esx hosts and creates a datastore. On top of that it also sets the MPIO settings correct on the esx host:
########################################################################################################################
# Author : Sjoerd Hooft
# Date Initial Version: 29 December 2011
# Comments: sjoerd_warmetal_nl
#
# Description:
# This script creates a volume per vSphere cluster, then creates LUNs, then connects the newly created lun to the esx hosts inside the cluster and creates the datastores on them.
# Make sure to modify the variables for your environment
#
# Credits:
# The idea for this script is partly from https://communities.netapp.com/docs/DOC-6181, which roughly does the same for NFS volumes.
#
# Recommendations:
# This script must run from the PowerCLI.
#
# Changes:
# Please comment on your changes to the script (your name and email address, line number, description):
# DATE - USERNAME - EMAILADDRESS - CHANGE DESCRIPTION
########################################################################################################################
# Script Variables
$timestamp = Get-Date -format "yyyyMMdd-HH.mm"
# Getting credentials for vCenter
while ($vcentercredentials -eq $null) {
Write-Host (get-date -uformat %I:%M:%S) "Please provide authentication credentials for vCenter" -ForegroundColor Green;
$vcentercredentials = $Host.UI.PromptForCredential("Please enter credentials", "Enter vCenter credentials", "intranet\adminshf", "")
}
# Getting credentials for NetApp filer
while ($filercredentials -eq $null) {
Write-Host (get-date -uformat %I:%M:%S) "Please provide authentication credentials for NetApp filer" -ForegroundColor Green;
$filercredentials = $Host.UI.PromptForCredential("Please enter credentials", "Enter NetApp filer credentials", "root", "")
}
# Setting variables vSphere
$vCenter = "vcenter"
$cluster = "BCP Test"
# Setting variables NetApp
$filer = "netappfiler01"
$aggr = "aggr1"
$newvol = "ESX_BCPTEST"
$numberofluns = 2
$lunname = "BCP_"
$lunsize = 4
$size = "g"
$igroup = "ESX_BCP_Hosts"
$startlunid = 201
# Import DATA ONTAP Module
Import-module D:\sjoerd\DataONTAP
# Connect to vCenter and NetApp Filer
Connect-VIServer $vCenter -Credential $vcentercredentials | Out-Null
Connect-NaController $Filer -Credential $filercredentials | Out-Null
# Create a new volume
$tmpvolsize = (($numberofluns * $lunsize) + 1)
$volsize = [string]$tmpvolsize += $size
Write-Host (get-date -uformat %I:%M:%S) "Creating volume $newvol in aggregate $aggr with size $volsize." -ForegroundColor Green;
New-NaVol $newvol $aggr $volsize | Out-Null
# Set options for the new volume
Set-NaVolOption $newvol no_atime_update yes | Out-Null
Set-NaVolOption $newvol fractional_reserve 0 | Out-Null
Set-NaSnapshotreserve $newvol 0 | Out-Null
Set-NaSnapshotSchedule $newvol -Weeks 0 -Days 0 -Hours 0 -WhichHours 0 | Out-Null
# Creating LUNs
$luncounter = 0
$lunid = $startlunid
$fulllunsize = [string]$lunsize += $size
$esxhost = Get-Cluster $cluster | Get-VMHost | sort | Select-Object -First 1
while ($luncounter -le $numberofluns-1) {
$lunname | foreach{
Write-Host (get-date -uformat %I:%M:%S) "Creating lun /vol/$newvol/$_$lunid with size $fulllunsize. " -ForegroundColor Green;
New-NaLun /vol/"$newvol"/"$_$lunid" -size $fulllunsize -Type vmware | Out-Null
Set-NaLunComment /vol/"$newvol"/"$_$lunid" "LUN for ESX Cluster $cluster" | Out-Null
Add-NaLunMap /vol/"$newvol"/"$_$lunid" -InitiatorGroup $igroup -ID $lunid | Out-Null
Get-VMHostStorage $esxhost -RescanAllHba -RescanVmfs | Out-Null
$path = Get-ScsiLun -vmhost $esxhost -luntype disk | where { $_.runtimename -match $lunid }
Write-Host (get-date -uformat %I:%M:%S) "Creating datastore $lunname$lunid on path $path on host $esxhost." -ForegroundColor Green;
New-Datastore -vmhost $esxhost -Name "$lunname$lunid" -path $path -vmfs | Out-Null
}
$luncounter++
$lunid++
}
# Rescan storage and datastores on all hosts in the cluster and set MPIO settings for each host.
$esxhosts = Get-Cluster $cluster | Get-VMHost
$rootpasswordsecure = Read-Host -assecurestring "Please enter the password of the root user to configure the MPIO settings on each host"
Function ConvertToPlainText( [security.securestring]$rootpasswordsecure ) {
$marshal = [Runtime.InteropServices.Marshal]
$marshal::PtrToStringAuto( $marshal::SecureStringToBSTR($rootpasswordsecure) ) }
$rootpassword = ConvertToPlainText $rootpasswordsecure
ForEach ($esxhost in $esxhosts) {
Write-Host (get-date -uformat %I:%M:%S) "Scanning storage on host $esxhost" -ForegroundColor Green;
Get-VMHostStorage $esxhost -RescanAllHba -RescanVmfs | Out-Null
Write-Host (get-date -uformat %I:%M:%S) "Setting MPIO on host $esxhost. Logging data to D:\adminshf\mpath_$esxhost" -ForegroundColor Green;
# Checking for the rsa key to be in cache
D:\adminshf\plink -ssh $esxhost -l root -pw $rootpassword echo "Connection to $esxhost succesful"
D:\adminshf\plink -ssh $esxhost -l root -pw $rootpassword /opt/ontap/santools/config_mpath --primary --loadbalance --policy fixed > D:\adminshf\mpath_"$esxhost"_"$timestamp"
Write-Host (get-date -uformat %I:%M:%S) "Done on host $esxhost" -ForegroundColor Green;
}
//This wiki has been made possible by://