Saturday, July 24, 2021

PowerCLI script to find the path status and count the number of paths

 PowerCLI script to find the path status and count the number of paths

To get path status of all the LUNs in vcenter:

#Thanks to vnote42.net

# Reference: https://vnote42.net/2018/11/28/powercli-script-to-count-paths-to-vmfs-volumes/

function Get-VMFSPathCount ($DatastorePattern='', $ClusterPattern='', $VMHostPattern='', [Switch]$TextOutput=$false, [int]$ShouldCount=-1)  {

    $Result = @()

    $VmfsDatastores = @{}

    $QueryClusterList = Get-Cluster ("*"+$ClusterPattern+"*")

    ForEach ($DS in (Get-Datastore ("*"+$DatastorePattern+"*") | Where-Object {$_.Type -eq "vmfs"})){

        $DS.Extensiondata.Info.Vmfs.Extent | ForEach-Object {

            $VmfsDatastores[$_.DiskName] = $DS.Name

        }

    }

    if ($VmfsDatastores.Count -eq 0) {break}

     ForEach ($VmHost in (Get-VMHost ("*"+$VMHostPattern+"*") | Where-Object {$_.ConnectionState -eq "Connected" -or $_.ConnectionState -eq "Maintenance"})) {

        $Cluster = Get-Cluster -VMHost $VmHost.Name

        if ($ClusterPattern -ne '' -and $Cluster.Name -notin $QueryClusterList.Name) {continue}

         $Luns = $VmHost | Get-ScsiLun | Where-Object {$_.IsLocal -ne "true" -and $_.CanonicalName -in $VmfsDatastores.Keys}

         if ($TextOutput) {Write-Host $VmHost.name}

        ForEach ($Lun in $Luns) {

            $CountTab = $Lun | Get-ScsiLunPath

            $CountAll = $CountTab.Count

            $CountActive = ($CountTab | Where-Object {$_.state -eq "Active"}).count

            $CountStandby = ($CountTab | Where-Object {$_.state -eq "Standby"}).count

            $CountDead = ($CountTab | Where-Object {$_.state -eq "Dead"}).count

 

            $VmfsName = $Lun.CanonicalName

            if($VmfsDatastores.ContainsKey($Lun.CanonicalName)){

                $Vmfsname = $VmfsDatastores[$Lun.CanonicalName]

            }

             if ($ShouldCount -ne $CountActive -or $CountDead -gt 0) {

                if ($TextOutput) {

                    Write-Host "`t" $VmfsName -NoNewline

                    Write-Host -ForegroundColor Green "`t" "Active:" $CountActive -NoNewline

                    if ($CountDead -ne 0) {Write-Host -ForegroundColor Red "`t" "Dead:" $CountDead} else {Write-Host}

                } else {

                    $NewEntry = [PScustomObject] @{

                        Cluster = $Cluster.Name

                        VMhost = $VmHost.Name

                        Datastore = $Vmfsname

                        AllPathCount = $CountAll

                        ActivePathCount = $CountActive

                        StandbyPathCount = $CountStandby

                        DeadPathCount = $CountDead

                        MultipathPolicy = $lun.MultipathPolicy

                    }

                    $Result += $NewEntry

                }

            }

        }

    }

    if (!$TextOutput) {$Result | Sort-Object Cluster, VMhost, Datastore}

}

Connect-VIServer vcenterserver

Get-VMFSPathCount | select Cluster, VMhost, Datastore, AllPathCount, ActivePathCount, StandbyPathCount, DeadPathCount, MultipathPolicy | Export-CSV C:\Temp\DSPathCount.csv -NoTypeInformation -Force

Disconnect-VIServer vcenterserver -Confirm:$false


To get path status of specific LUNs in vcenter:


#### HTML Output Formatting #######

$a = "<style>"

$a = $a + "BODY{background-color:White ;}"

$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"

$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:Green}"

$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"

$a = $a + "</style>"

################################################################################################

Remove-Item C:\Temp\Output.html


$VCenter= Read-Host "Enter the vCenter Server Name"

#$StorageArray = Read-Host "Enter the Storage Array Name"

#$Cluster = Read-Host "Enter the Cluster Name"

$ToMail = Read-Host "Enter the Mail address"

$input = Import-Csv C:\Temp\input.csv

$report = @()

Connect-VIServer $VCenter


foreach ($row in $input)

{

    $output=Get-VMFSPathCount | 

    Where-Object {$_.Datastore -like $row.ArrayName -and $_.Cluster -like $row.ClusterName}  | 

    select Cluster, VMhost, Datastore, AllPathCount, ActivePathCount, StandbyPathCount, DeadPathCount


    $report+=$output

}


$report | ConvertTo-html -Head $a -Body "<H2> </H2>" >> C:\Temp\Output.html

$body = [System.IO.File]::ReadAllText('C:\Temp\Output.html')

$text = '<b><font=Arial>Datastore Path Status</font></b>'

Send-MailMessage -To $ToMail -From from@mailaddress.com -Subject "Datastore Path Status for $Cluster and $StorageArray" -Body "$text $body" -SmtpServer smtpmail.mailaddress.com -BodyAsHtml

Disconnect-VIServer $VCenter -Confirm:$false


<# Content of input.csv

ClusterName,ArrayName

cluster1,datastore

#>

No comments:

Post a Comment