VMware Cloud Community
xacolabril
Expert
Expert
Jump to solution

Easy Pseudo-scrip, but in PowerCLI, too?

Hello. I'm testing a personal balancing path script for MRU LUNs. I developed the following pseudo-algorithm and I would turn/convert it into PowerCLI. I would like to do exactly this:

HostName = "Server1"


Cont_Fix = 1
Cont_MRU = 1


For each LUN of HostName


    # Get the number of paths for the LUN:
    NumPaths=LeerNumPaths(Lun)


    # Balancing paths for a Fixed LUN:
    If (Lun = Fixed) Then
        # The technique is configure a preferred path for the Fixed LUNs and playing
        # with a module to determining which path can be preferred in each case.
        Configure_Path -Lun=lun -PreferredPath=(Cont_Fix Mod NumPaths)+1
        Cont_Fix = Cont_Fix + 1
    End If


    # Balancing paths for a MRU LUN:
    If (LUN = MRU) Then
        Path_To_Set_Enabled=(Cont_Mru Mod NumPaths)+1


        # The technique is: 1) Disabling all paths except one, which will have taken the active role:


        For Path_Count=1 To NumPaths Do
            If Path_Count <> Path_To_Set_Enabled Then
                Disable_Path -Lun=lun -Path=Path_Count           
            End If
        End For


        # 2) We again turn on the paths to normalize the status of the LUN:


        For Path_Count=1 To NumPaths Do
            If Path_Count <> Path_To_Set_Enabled Then
                Enable_Path -Lun=lun -Path=Path_Count           
            End If
        End For


        Cont_Mru = Cont_Mru + 1
       
    End If
       
End For


If Get_Paths_Disabled Then
    Say "Oups. You need to review disabled paths"
Else
    Say "Yeah."
End If

It's possible to convert this pseudo-script to Power-CLI?

Thank you in advance!

Regards.

Xavier Colomé Abril. VMware Certified Professional VCP3, VCP4 and VCP5. [Si encuentras que esta o cualquier otra respuesta ha sido de utilidad, vótalas. Gracias.] [If you find this or any other information helpful or correct, please consider awarding points. Thank you.]
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The following PowerCLI script is an implementation of your pseudo code. I did not actually test the script. So it might not work the first time you run it.

$HostName = "Server1"

$Cont_Fix = 1
$Cont_MRU = 1

foreach ($LUN in (Get-VMHost -Name $HostName | Get-ScsiLun))
{
    # Get the number of paths for the LUN:
    $ScsiLunPath = $LUN | Get-ScsiLunPath
    $NumPaths = $ScsiLunPath | Measure-Object | Select-Object -Property Count

    # Balancing paths for a Fixed LUN:
    If ($Lun.MultipathPolicy -eq "Fixed")
    {
        # The technique is configure a preferred path for the Fixed LUNs and playing
        # with a module to determining which path can be preferred in each case.
        Set-ScsiLun -ScsiLun $LUN -PreferredPath $ScsiLunPath[($Cont_Fix % $NumPaths)+1] -Confirm:$false  
        $Cont_Fix += 1
    }

    # Balancing paths for a MRU LUN:
    If ($Lun.MultipathPolicy -eq "MostRecentlyUsed")
    {
        $Path_To_Set_Enabled = ($Cont_Mru % $NumPaths)+1

        # The technique is: 1) Disabling all paths except one, which will have taken the active role:

        for ($Path_Count=1;$Path_Count++;$Path_Count -le $NumPaths)
        {
            if ($Path_Count -ne $Path_To_Set_Enabled)
            {
                Set-ScsiLunPath -Active $false Disable_Path -ScsiLunPath $ScsiLunPath[$Path_Count] -Confirm:$false            
            }
        }

        # 2) We again turn on the paths to normalize the status of the LUN:

        for ($Path_Count=1;$Path_Count++;$PathCount -le $NumPaths)
        {
            if ($Path_Count -ne $Path_To_Set_Enabled)
            {
                Set-ScsiLunPath -Active $true Disable_Path -ScsiLunPath $ScsiLunPath[$Path_Count] -Confirm:$false    
            }
        }

        $Cont_Mru += 1 
    }
}

if ($ScsiLun | Get-ScsiLunPath | Where-Object {$_.State -ne "Active"})
{
    "Oups. You need to review disabled paths"
}
else
{
    "Yeah."
}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
2 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The following PowerCLI script is an implementation of your pseudo code. I did not actually test the script. So it might not work the first time you run it.

$HostName = "Server1"

$Cont_Fix = 1
$Cont_MRU = 1

foreach ($LUN in (Get-VMHost -Name $HostName | Get-ScsiLun))
{
    # Get the number of paths for the LUN:
    $ScsiLunPath = $LUN | Get-ScsiLunPath
    $NumPaths = $ScsiLunPath | Measure-Object | Select-Object -Property Count

    # Balancing paths for a Fixed LUN:
    If ($Lun.MultipathPolicy -eq "Fixed")
    {
        # The technique is configure a preferred path for the Fixed LUNs and playing
        # with a module to determining which path can be preferred in each case.
        Set-ScsiLun -ScsiLun $LUN -PreferredPath $ScsiLunPath[($Cont_Fix % $NumPaths)+1] -Confirm:$false  
        $Cont_Fix += 1
    }

    # Balancing paths for a MRU LUN:
    If ($Lun.MultipathPolicy -eq "MostRecentlyUsed")
    {
        $Path_To_Set_Enabled = ($Cont_Mru % $NumPaths)+1

        # The technique is: 1) Disabling all paths except one, which will have taken the active role:

        for ($Path_Count=1;$Path_Count++;$Path_Count -le $NumPaths)
        {
            if ($Path_Count -ne $Path_To_Set_Enabled)
            {
                Set-ScsiLunPath -Active $false Disable_Path -ScsiLunPath $ScsiLunPath[$Path_Count] -Confirm:$false            
            }
        }

        # 2) We again turn on the paths to normalize the status of the LUN:

        for ($Path_Count=1;$Path_Count++;$PathCount -le $NumPaths)
        {
            if ($Path_Count -ne $Path_To_Set_Enabled)
            {
                Set-ScsiLunPath -Active $true Disable_Path -ScsiLunPath $ScsiLunPath[$Path_Count] -Confirm:$false    
            }
        }

        $Cont_Mru += 1 
    }
}

if ($ScsiLun | Get-ScsiLunPath | Where-Object {$_.State -ne "Active"})
{
    "Oups. You need to review disabled paths"
}
else
{
    "Yeah."
}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
xacolabril
Expert
Expert
Jump to solution

It's ok for me. Thank you so much.

Xavier Colomé Abril. VMware Certified Professional VCP3, VCP4 and VCP5. [Si encuentras que esta o cualquier otra respuesta ha sido de utilidad, vótalas. Gracias.] [If you find this or any other information helpful or correct, please consider awarding points. Thank you.]
0 Kudos