mercredi 16 mars 2016

Getting and Setting Multipathing in VMware vCenter using Powershell

Once in a while, when you first install or replace an external storage attached to your ESXi hosts the Multipathing Policy may need to be changed to something like Round Robin. While one may do it manually it can quickly become annoying after the second ESXi!

PowerShell combined with PowerCLI can solve this easily!


Retrieving the Multipathing Policy in a Cluster:


Building a report to retrieve the attached disks information is not that difficult, the Get-ScsiLun commandlet allows us to retrieve the list of all storage attached to the ESXi hosts thanks to the -LunType disk switch.

# Get all the Host present in the VMware Cluster "CL-LAB" and retrieve the attached disks

Get-Cluster -Name "CL-LAB" `

    | Get-VMHost `

    | Get-ScsiLun -LunType disk `

    | Select VMHost, CanonicalName, Vendor, CapacityGB, IsLocal, MultipathPolicy `

    | Sort-Object CanonicalName, VMHost `

    | Format-Table -AutoSize


<#

    VMHost              CanonicalName                        Vendor   CapacityGB IsLocal MultipathPolicy

    ------              -------------                        ------   ---------- ------- ---------------

    esx001.katalykt.lan naa.60002ac0000000000000002600032154 3PARdata        400   False      RoundRobin

    esx002.katalykt.lan naa.60002ac0000000000000002600032154 3PARdata        400   False      RoundRobin

    esx001.katalykt.lan naa.60002ac0000000000000002700032154 3PARdata        800   False           Fixed

    esx002.katalykt.lan naa.60002ac0000000000000002700032154 3PARdata        800   False           Fixed

    esx001.katalykt.lan naa.600507605809207819f3c23e2183f629 IBM         278,875    True           Fixed

    esx002.katalykt.lan naa.60050760408ed348188ad3f7419e64f3 IBM         278,875    True           Fixed

#>



Setting the Multipathing Policy in a Cluster:


Changing the Multipathing Policy is basically the same as the previous example aside from the fact that we do specify that the disk shouldn't be local (i.e SAN-based storage) and that the existing Policy should be different than Round Robin.

Note that it's important to use the -WhatIf switch with the Set-Scsilun commandlet first to measure the impact before executing it!

<#

    Get all the Host present in the VMware Cluster "CL-LAB", retrieve the attached disks and

    set the Multipath Policy on RoundRobin wherever a non-local, non-RoundRobin disk is detected

    WhatIf should be used first to validate the operation

#>

Get-Cluster -Name "CL-LAB" `

    | Get-VMHost `

    | Get-ScsiLun -LunType disk `

    | Where-Object { ($_.MultipathPolicy -notlike "RoundRobin") -and (-not($_.IsLocal)) } `

    | Set-ScsiLun -MultiPathPolicy RoundRobin -WhatIf


<#

    What if: Performing operation 'Updating ScsiLun.' on ScsiLun 'naa.60002ac0000000000000002700032154'

    What if: Performing operation 'Updating ScsiLun.' on ScsiLun 'naa.60002ac0000000000000002700032154'

#>

lundi 7 mars 2016

Using Powershell to backup the Configuration of a Brocade SAN Fibre Channel Switch




Automating the Configuration backup of a Brocade SAN Fibre Channel Switch is not really a difficult task. Indeed, one may export the configuration of a Switch by using the "configUpload" SSH command which allows the device to export parameters such as the Zones along with the Switch core parameters toward a remote FTP/SFTP/SCP server.

This task can be automated by combining both Powershell and SSH.

The following function requires the Posh-SSH module which is available on github (https://github.com/darkoperator/Posh-SSH) and uses the FTP protocol for the backup. A CSV is expected as an input which contains the Host (the Brocade device), the SSH Operator Username and the SSH Operator Password (see the example below)

Host;Username;Password
192.168.16.100;srvsw-backop;Passw0rdFCA
192.168.16.101;srvsw-backop;Passw0rdFCB

The SSH command "configDownload" may be used to restore the device should the need arise.

#Requires -Version 3.0


Function Backup-BrocadeSANSW {

<#

    .SYNOPSIS

            This function allows the backup of one or multiple Brocade SAN FC Switch

    .DESCRIPTION

            This function allows the backup of one or multiple Brocade SAN FC Switch on an FTP Server

    .PARAMETER  CSV

            The CSV file contains the list of all the IBM Brocade devices (Host / Username / Password)

            Backup-BrocadeSANSW.csv is used by default if nothing is specified

    .PARAMETER  ExportWithName

            This switch may be specified to format the output with the device's name rather than the host name

    .EXAMPLE

            Backup-BrocadeSANSW -CSV "e:\scripts\brocade_list.csv" -ExportWithName


            BackedUp Path                                                                        HostName     Device  

            -------- ----                                                                        --------     ------  

                True ftp://repository.katalykt.lan/SYSTEM/BROCADE2498/SW_FCA_20150612_105512.txt 192.168.16.100 SW_FCA

                True ftp://repository.katalykt.lan/SYSTEM/BROCADE2498/SW_FCB_20150612_105516.txt 192.168.16.101 SW_FCB

    .EXAMPLE

            Backup-BrocadeSANSW


            BackedUp Path                                                                                HostName     Device  

            -------- ----                                                                                --------     ------  

                True ftp://repository.katalykt.lan/SYSTEM/BROCADE2498/192.168.16.100_20150612_105532.txt 192.168.16.100 192.168.16.100

                True ftp://repository.katalykt.lan/SYSTEM/BROCADE2498/192.168.16.101_20150612_105536.txt 192.168.16.101 192.168.16.101

    .MODULES

            Posh-SSH

    .LINKS

            https://github.com/darkoperator/Posh-SSH

    .NOTES

            NAME:     Backup-BrocadeSANSW

            AUTHOR:   ROULEAU Benjamin

            LASTEDIT: 2015-06-12

            TESTED:   IBM Brocade 2498

#>


    #Requires -Module Posh-SSH

    [CmdletBinding()]

    PARAM(

        [ValidateScript({Test-Path -Path $_ -PathType Leaf})]

        $CSV=(Join-Path -Path (Split-Path -parent $PSCommandPath) -ChildPath "Backup-BrocadeSANSW.csv"),

        

        [Switch]$ExportWithName

    )


    BEGIN {

        Write-Verbose -Message "[BEGIN Backup-BrocadeSANSW] Attempting to import the Posh-SSH Module..."


        # FTP Properties, our backup files will be stored in there

        $FTP_Host = "repository.katalykt.lan"

        $FTP_Username = "srv-backupop"

        $FTP_Password = "Passwd20160612"

        $FTP_Path = "SYSTEM/BROCADE2498"

    }


    PROCESS {

        # Retrieve the needed parameters for each given devices

        Import-Csv -Path $CSV -Delimiter ';' | ForEach-Object {

            $SSH_Username = $_.Username

            $SSH_Password = $_.Password

            $SSH_Host = $_.Host

            $IsBackedUp = $false


            # Build up the Credentials

            $SSH_Password = ConvertTo-SecureString $SSH_Password -AsPlainText -Force

            $SSH_Credentials =  New-Object System.Management.Automation.PSCredential ($SSH_Username, $SSH_Password)


            Write-Verbose -Message "[PROCESS Backup-BrocadeSANSW] Processing SSH Host: $SSH_Host"


            # Attempt to start an SSH Session

            TRY {

                Write-Verbose -Message "[PROCESS Backup-BrocadeSANSW] Attempting to connect to the SSH Host: $SSH_Host"


                $SSH_Session = New-SSHSession -ComputerName $SSH_Host -Credential $SSH_Credentials -AcceptKey $true -ErrorAction Stop

            } CATCH {

                Write-Warning -Message "[PROCESS Backup-BrocadeSANSW] Unable to start an SSH Session on the given host"


                $SSH_Session = $false

            }


            # Ensure that the SSH Session is alive and kicking

            IF ($SSH_Session) {

                # By default, we use the SSH Host as a label

                $SSH_DeviceName = $SSH_Host


                # Retrieve the SSH Device Name if specified

                IF ($ExportWithName) {

                    Write-Verbose -Message "[PROCESS Backup-BrocadeSANSW] Attempting to retrieve the device name"


                    $SSH_DeviceName = Invoke-SSHCommand -SSHSession $SSH_Session -Command "switchshow | grep switchName"

                    $SSH_DeviceName = $SSH_DeviceName.Output -match "\:\s(.*[a-zA-Z0-9])"

                    $SSH_DeviceName = $matches[1]


                    IF (-not($SSH_DeviceName)) {

                        Write-Warning -Message "[PROCESS Backup-BrocadeSANSW] Unable to retrieve the Device Name, falling back to the Host name"

                        $SSH_DeviceName = $SSH_Host

                    }


                    Write-Verbose -Message "[PROCESS Backup-BrocadeSANSW] Retrieved device name: '$SSH_DeviceName'"

                }


                # Export the Log to the FTP

                $FTP_ExportFullPath = "{0}/{1}_{2}.txt" -f $FTP_Path, $SSH_DeviceName, (Get-Date -Format "yyyyMMdd_HHmmss")

                $SSH_Export = Invoke-SSHCommand -SSHSession $SSH_Session -Command ("configupload -all -p ftp {0},{1},{2},{3}" -f $FTP_Host, $FTP_Username, $FTP_ExportFullPath, $FTP_Password)


                # Make sure that the output match the expected valid return

                IF ($SSH_Export.Output -like "*All selected config parameters are uploaded*") {

                    Write-Verbose -Message "[PROCESS Backup-DevicesConfiguration] Device '$SSH_DeviceName' configuration has been backed up"


                    # Append the FTP host to the path for the output

                    $FTP_ExportFullPath = "ftp://{0}" -f ((Join-Path $FTP_Host -ChildPath $FTP_ExportFullPath) -replace '\\','/')


                    $IsBackedUp = $true

                } ELSE {

                    Write-Warning -Message "[PROCESS Backup-DevicesConfiguration] Failed to back up device '$SSH_DeviceName': $($SSH_Export.Output)"


                    $FTP_ExportFullPath = "Failed"

                }


                # Remove the SSH Session

                $SSH_Session | Remove-SSHSession | Out-Null


                # Return the Output

                New-Object -TypeName PSObject -Property @{

                    Device = $SSH_DeviceName

                    HostName = $SSH_Host

                    BackedUp = $IsBackedUp

                    Path = $FTP_ExportFullPath

                }

            }

        }

    }


    END {


    }

}