VMware Cloud Community
eblank
Contributor
Contributor
Jump to solution

PowerCLI Script to change path policy for RDMs

Hi,

I am trying to change the path policy setting for the Raw Device Mappings (RDMs) in our environment from "Round Robin" to "Fixed". We are using RDM LUNs for clustering, but it turns out that VMWare currently doesn't support Round Robin path policy in RDMs. I found many scripts to change the setting for all LUNs, but I just want to change the setting for RDMs and nothing else.

Any ideas on how to write a powercli script to do that would be greatly appreciated!

Thanks,

-Eugenio

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

foreach($vm in (Get-View -ViewType VirtualMachine)){
    $vm.Config.Hardware.Device | where {$_.gettype().Name -eq "VirtualDisk"} | %{
        if("physicalMode","virtualmode" -contains $_.Backing.CompatibilityMode){
            $diskUUID = $_.Backing.LunUuid.Substring(10,32)
            $esx = Get-View $vm.Runtime.Host
            $lun = $esx.Config.StorageDevice.ScsiLun | where {$_.CanonicalName.Split(".")[1] -eq $diskUUID}
            if($lun){
                Get-VMHost $esx.Name | Get-ScsiLun -CanonicalName $lun.CanonicalName |
                where {$_.MultiPathPolicy -eq "RoundRobin"} |
                Set-ScsiLun -MultipathPolicy Fixed
            }         }     } }

It's based on the logic I used in my LUN report – datastore, RDM and node visibility post.

Note that the script runs through all your VM and will check the path policy for all RDM disks. If you want to limit this to a specific set of VMs, then you could use the Filter parameter on the Get-View cmdlet on the first line.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

13 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

foreach($vm in (Get-View -ViewType VirtualMachine)){
    $vm.Config.Hardware.Device | where {$_.gettype().Name -eq "VirtualDisk"} | %{
        if("physicalMode","virtualmode" -contains $_.Backing.CompatibilityMode){
            $diskUUID = $_.Backing.LunUuid.Substring(10,32)
            $esx = Get-View $vm.Runtime.Host
            $lun = $esx.Config.StorageDevice.ScsiLun | where {$_.CanonicalName.Split(".")[1] -eq $diskUUID}
            if($lun){
                Get-VMHost $esx.Name | Get-ScsiLun -CanonicalName $lun.CanonicalName |
                where {$_.MultiPathPolicy -eq "RoundRobin"} |
                Set-ScsiLun -MultipathPolicy Fixed
            }         }     } }

It's based on the logic I used in my LUN report – datastore, RDM and node visibility post.

Note that the script runs through all your VM and will check the path policy for all RDM disks. If you want to limit this to a specific set of VMs, then you could use the Filter parameter on the Get-View cmdlet on the first line.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

eblank
Contributor
Contributor
Jump to solution

Thanks LucD! This was very helpful!!

Reply
0 Kudos
SantoshKumar36
Enthusiast
Enthusiast
Jump to solution

@LUCD : I have small doubt in the below script.

How do we find the attributes supported to so & so commands,for exam. you have mentioned in the below exam. 

$esx.Config.StorageDevice.ScsiLun 

How can we avail these pramateres like .config.storagedevice.etc..

If we keep pressing tab continiously, we would get parameters related to it, but if you can get a list of the all the avialble parameters listout in commands, will help us to filter it out..





Santosh K | If my answer resolved or helped you, please mark it as Correct or Helpful to award points.
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

These vSphere objects are described in the SDK Reference.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
audi50002011101
Contributor
Contributor
Jump to solution

What if you want to do cluster by cluster?

Also, I'm getting..

If the MultipathPolicy parameter is set to 'Fixed', you must
specify the PreferredPath parameter.

Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

Luc, can you please update how to run it for a list of already identified VMs?

$VMlist = Get-Content -path "C:\vmlist.txt"

foreach ($vm in $vmlist)

{

foreach($vm in (Get-View -ViewType VirtualMachine)){
   
$vm.Config.Hardware.Device | where {$_.gettype().Name -eq "VirtualDisk"} | %{
       
if("physicalMode","virtualmode" -contains $_.Backing.CompatibilityMode){
           
$diskUUID = $_.Backing.LunUuid.Substring(10,32)
           
$esx = Get-View $vm.Runtime.Host
           
$lun = $esx.Config.StorageDevice.ScsiLun | where {$_.CanonicalName.Split(".")[1] -eq $diskUUID}
           
if($lun){
               
Get-VMHost $esx.Name | Get-ScsiLun -CanonicalName $lun.CanonicalName |
               
where {$_.MultiPathPolicy -eq "RoundRobin"} |
               
Set-ScsiLun -MultipathPolicy Fixed
            }
        }
    }
}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this.

This version also fixes the issue with the missing PreferredPath parameter.

$filter = [string]::Join('|',(Get-Content -path "C:\vmlist.txt"))

foreach($vm in (Get-View -ViewType VirtualMachine -Filter @{'Name'=$filter})){

    $vm.Config.Hardware.Device | where {$_.gettype().Name -eq "VirtualDisk"} | %{

        if("physicalMode","virtualmode" -contains $_.Backing.CompatibilityMode){

            $diskUUID = $_.Backing.LunUuid.Substring(10,32)

            $esx = Get-View $vm.Runtime.Host

            $lun = $esx.Config.StorageDevice.ScsiLun | where {$_.CanonicalName.Split(".")[1] -eq $diskUUID}

            if($lun){

                $lunObj = Get-VMHost $esx.Name | Get-ScsiLun -CanonicalName $lun.CanonicalName |

                    where {$_.MultiPathPolicy -eq "RoundRobin"}

                if($lunObj){

                    $path = Get-ScsiLunPath -ScsiLun $lunObj | Get-Random

                    Set-ScsiLun -ScsiLun $lunObj -MultipathPolicy Fixed -PreferredPath $path

                }

            }

        }

    }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

Thanks, but i need to set the path policy to MRU since it's an AP array.So I believe I should use only till

$path = Get-ScsiLunPath -ScsiLun $lunObj | Get-Random

                    Set-ScsiLun -ScsiLun $lunObj -MultipathPolicy MostRecentlyUSed


and rest of the script remains the same.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Correct, something like this

$filter = [string]::Join('|',(Get-Content -path "C:\vmlist.txt"))

foreach($vm in (Get-View -ViewType VirtualMachine -Filter @{'Name'=$filter})){

    $vm.Config.Hardware.Device | where {$_.gettype().Name -eq "VirtualDisk"} | %{

        if("physicalMode","virtualmode" -contains $_.Backing.CompatibilityMode){

            $diskUUID = $_.Backing.LunUuid.Substring(10,32)

            $esx = Get-View $vm.Runtime.Host

            $lun = $esx.Config.StorageDevice.ScsiLun | where {$_.CanonicalName.Split(".")[1] -eq $diskUUID}

            if($lun){

                Get-VMHost $esx.Name | Get-ScsiLun -CanonicalName $lun.CanonicalName |

                    where {$_.MultiPathPolicy -ne "RoundRobin"} |

                    Set-ScsiLun -MultipathPolicy RoundRobin

                }

            }

        }

    }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

ajohn24
Contributor
Contributor
Jump to solution

Thanks Luc, will check it out Smiley Happy

Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

One question though. I wanted to make sure if my VM has more than 1RDMs attached the script should be able to change the path policy for all the disks and not stop after one. I did not see the logic to traverse across the disks and do the configurations (like foreach disk or something), hence the question.

Sorry for the ignorance

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The 3th line starts a loop that runs through all the devices connected to the VM.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

Awesome. Thanks again!

Reply
0 Kudos