VMware: Set Perennial Reservations to True on RDM Luns in a cluster (The Quick Way)

Well, the time has come to revist some code.  New year, I’ve learned some more about coding, and need to stretch the fingers a little.

The previous article detailed how to do this, and sooo much more. This article will be a bit more compact, use “off the shelf” components and is compatiable with VMware 5 – 6.5.  You will however need to update your computer and PowerCLI if you don’t meet those requirements.

Those requirements happen to be:

(1) Windows Management Framework 5.1 (aka PowerShell)
(2) dot.NET 4.5.2 (recomend 4.7.1 or higher)
(3) PowerCLI 6.5 (easy install method)

 

 

1) Open a PowerShell command line using an account that has approporiate rights to your vCenter server.

2) Run the following script to automatically set the “IsPerenniallyReserved” flag to “true” on new LUNS.

 

Note: To use a CSV file as the NAA source, comment the second RDMNAAs and uncomment the first RDMNAAs (line 40 and 41).
CSV format is one NAA per line.

 

Function Get-RequiredModule
{
Param([string]$name)
if(!(Get-Module $name)){if(!(Get-Module -ListAvailable | Where-Object {$_.name -eq $name})){write-host "Required module $($name) missing."} else { Import-Module $name }} else { $true }
}

# Load the required modules
Get-RequiredModule -name "VMware.PowerCLI"

# Check if required modules are loaded, if not, break out of script.
if(!(Get-RequiredModule -name "VMware.PowerCLI")){Break}

# Clearing variables
$vCenter=@()
$vFarm=@()
$vCluster=@()

# Change these defaults to suit your environment
$dft_vCenter = "vCenter.domain.com"
$dft_vFarm = "Farm Name"
$dft_vCluster = "Cluster Name"

# Get inputs from user, with default values
$vCenter = Read-Host -Prompt "Input your vCenter Server here (Press enter for: $($dft_vCenter))"
$vFarm = Read-Host -Prompt "Input your Farm here (Press enter for: $($dft_vFarm))"
$vCluster = Read-Host -Prompt "Input your Cluster here (Press enter for: $($dft_vCluster))"

# Assign default values to the field if no user input was given
If(!($vCenter)){$vCenter=$dft_vCenter}
If(!($vFarm)){$vFarm=$dft_vFarm}
If(!($vCluster)){$vCluster=$dft_vCluster}

# Host Connection and Enumeration of NAAs
$connected = Connect-VIServer -Server $vCenter | Out-Null

$clusterInfo = Get-Datacenter -Name $vFarm | get-cluster $vCluster
$vmHosts = $clusterInfo | get-vmhost | select -ExpandProperty Name

# To use a CSV file as the NAA source, comment the second RDMNAAs and uncomment the first RDMNAAs. CSV format is one NAA per line.
#$RDMNAAs = (Get-Content C:\Scripts\RDMNAAs.csv)
$RDMNAAs = $clusterInfo | Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select -ExpandProperty ScsiCanonicalName -Unique

foreach ($vmhost in $vmHosts) {
$myesxcli = Get-EsxCli -VMHost $vmhost -V2

	foreach ($naa in $RDMNAAs) {
	$diskinfo = $myesxcli.storage.core.device.list.Invoke(@{device=$naa})
	$vmhost + " " + $naa + " " + "IsPerenniallyReserved = " + $diskinfo.IsPerenniallyReserved

	if($diskinfo.IsPerenniallyReserved -eq "false") {
		write-host "Configuring Perennial Reservation for LUN $naa..."
		$myesxcli.storage.core.device.setconfig.Invoke(@{device=$naa;perenniallyreserved=$true})
		$diskinfo = $myesxcli.storage.core.device.list.Invoke(@{device=$naa})
		$vmhost + " " + $naa + " " + "IsPerenniallyReserved = " + $diskinfo.IsPerenniallyReserved
		}
	write-host "---------------------------------------------------------------------"
	}
}

Disconnect-VIServer $vcenter -confirm:$false | Out-Null