Going through all of your hosts and setting your multipathing policy manually through either the C# or the vSphere Web Client can certainly be a trying task. Sure, if you have 2 or 3 hosts it might be alright, but anything above that and things start to get pretty monotonous really fast! Also we have all known that if you need to perform the same task more than once it’s always better to script it – Firstly it creates consistency across your environment, secondly it saves you time, and thirdly it helps the economy as you are out buying coffee when normally you would be at your desk clicking….
So all that leads me to the following script… For the most part the script does two things – first it gets a list of all of your LUNS associated with a host or group of hosts, second, it sets up your desired multipathing policy on those luns based on the model of array presenting them as well as marks paths as preferred if applicable… That being said, I’ve only included a few different models inside the script below, to see a full list of models you can run “esxcli storage nmp satp rule list” from the esxcli to list all of the models and integrate them into the script as you see fit.
So first off you can see that the script accepts a parameter – if you pass the string fixall with the call of the script it will just continue without prompts, otherwise, it asks you for a y/n answer. Also, when setting a path policy to fixed you might want to specify a preferred path – you can see that I’ve set this up as vmhba1, just be sure to customize the script to your liking for your environment…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#fixall - if equals autoyes then don't prompt for y/n param ($fixall) $vcenter = "vcenterserver" $username = "username" $password = "password" $hostcontainer = "cluster or datacenter containing hosts to check" Write-Host "Connecting to vCenter Server..." $success = connect-viserver $vcenter -username $username -Password $password $vmhosts = get-vmhost -Location $hostcontainer foreach ($vmhost in $vmhosts) { Write-Host "Processing " $vmhost.Name"......" -ForegroundColor white -BackgroundColor Red #get all luns of type 'disk' from host $luns = get-ScsiLun -VMhost $vmhost -LunType disk foreach ($lun in $luns) { $multipath = $lun.MultipathPolicy $canname = $lun.CanonicalName $model = $lun.Model # if lun belongs to EVA if ($model -eq "HSV400") { $defaultpolicy = "roundrobin" if ($multipath -ne $defaultpolicy) { #get matching datastore name of lun $ds = Get-Datastore | Where-Object {$_.Extensiondata.Info.vmfs.Extent.DiskName -eq "$canname"} $dsname = $ds.Name Write-Host "$dsname" -foregroundcolor red -backgroundcolor yellow -nonewline; Write-Host " is on the EVA and is currently set to " -nonewline; Write-Host "$multipath" -ForegroundColor yellow -nonewline; Write-Host " where it should be " -nonewline; Write-Host "Round Robin" -ForegroundColor green -NoNewline; Write-Host " - Fix? " -NoNewline; if ($fixall -eq "autoyes") { $fix = "y" } else { $fix = Read-Host " y/[N]:" } if ($fix -eq "y") { $lun | set-ScsiLun -MultipathPolicy $defaultpolicy Write-Host "***Complete***" -Foregroundcolor green } } } #if lun belongs to an MSA elseif ($model -eq "MSA1000 VOLUME") { $defaultpolicy = "fixed" $prefPathHBA = "vmhba1" if ($multipath -ne $defaultpolicy) { #get corresponding datastore name of lun $ds = Get-Datastore | Where-Object {$_.Extensiondata.Info.vmfs.Extent.DiskName -eq "$canname"} $dsname = $ds.Name Write-Host "$dsname" -foregroundcolor red -backgroundcolor yellow -nonewline; Write-Host " is on the MSA and is currently set to " -nonewline; Write-Host "$multipath" -ForegroundColor yellow -nonewline; Write-Host " where it should be " -nonewline; Write-Host "Fixed" -ForegroundColor green -NoNewline; Write-Host " - Fix? " -NoNewline; if ($fixall -eq "autoyes") { $fix = "y" } else { $fix = Read-Host " y/[N]:" } if ($fix -eq "y") { $lunpath = Get-ScsiLunPath -scsilun $lun | where-object {$_.ExtensionData.Adapter -like "*$prefPathHBA"} $lun | set-ScsiLun -MultipathPolicy $defaultpolicy -PreferredPath $lunpath -confirm:$false } } } } } |
Again I don’t claim to be a Powershell/PowerCLI scriptmaster, so if you see any performance enhancements, problems, suggestions, concerns please use the comments section below to let me know… – Anyone can write a script, but it takes a community to make it a great script.
Hi Mike,
We came across your script which would be very useful for a piece of work that we are going to do. vCenter is v5.0 U1, and the hosts are also v5.0U1. We have small issue when enumerating the datastore and also the lunpath. We attempted to to troubshoot this.
Get-Datastore | select extensiondata.info
returns a blank list
we then inserted some further troubleshooting:
Get-ScsiLunPath -scsilun $luns[0] | select extensiondata.adapter
extensiondata.adapter also return a blank list.
Thanks
C