Tuesday, August 30, 2016

HBA Storage path redundancy status using PowerCLI

HBA Storage path redundancy status


$views = Get-View -ViewType "HostSystem" -Property Name,Config.StorageDevice


$result = @()

foreach ($view in $views | Sort-Object -Property Name) {
    Write-Host "Checking" $view.Name

    $view.Config.StorageDevice.ScsiTopology.Adapter |?{ $_.Adapter -like "*FibreChannelHba*" } | %{
        $hba = $_.Adapter.Split("-")[2]

        $active = 0
        $standby = 0
        $dead = 0

        $_.Target | %{ 
            $_.Lun | %{
                $id = $_.ScsiLun

                $multipathInfo = $view.Config.StorageDevice.MultipathInfo.Lun | ?{ $_.Lun -eq $id }

                $a = [ARRAY]($multipathInfo.Path | ?{ $_.PathState -like "active" })
                $s = [ARRAY]($multipathInfo.Path | ?{ $_.PathState -like "standby" })
                $d = [ARRAY]($multipathInfo.Path | ?{ $_.PathState -like "dead" })

                $active += $a.Count
                $standby += $s.Count
                $dead += $d.Count
            }
        }

        $result += "{0},{1},{2},{3},{4}" -f $view.Name.Split(".")[0], $hba, $active, $dead, $standby
    }
}

ConvertFrom-Csv -Header "VMHost", "HBA", "Active", "Dead", "Standby" -InputObject $result | ft -AutoSize > "d:\temp\HBA\storagepaths $(get-date -f yyyy-MM-dd-hhmmss).txt"

How to get NIC speed using PowerCLI

Get NIC speed using PowerCLI script

Connect-VIServer vcenterservername

$VMhosts = Get-VMHost | Get-View
Foreach ($vmhost in $vmhosts){
   Write-Output $vmhost.Name
   $pnic = 0
   Do {    
      $Speed = $VMhost.Config.Network.Pnic[$pnic].LinkSpeed.SpeedMb    
      Write "Pnic$pnic $Speed"   
      $pnic ++
   } Until ($pnic -eq ($VMhost.Config.Network.Pnic.Length))}

Configuring NTP server using PowerCLI

Below scripts executes against all the ESXi hosts under the vCenter Server. 

1) Remove old NTP server settings & add new NTP server settings:

connect-viserver vcenterservername
$oldntpservers='192.168.1.254'
$newntpservers='192.168.2.254'
foreach($vmhost in get-vmhost){
#stop ntpd service
$vmhost|Get-VMHostService |?{$_.key -eq 'ntpd'}|Stop-VMHostService -Confirm:$false
#remove ntpservers 
$vmhost|Remove-VMHostNtpServer -NtpServer $oldntpservers -Confirm:$false
#add new ntpservers
$vmhost|Add-VmHostNtpServer -NtpServer $newntpservers
#start ntpd service
$vmhost|Get-VMHostService |?{$_.key -eq 'ntpd'}|Start-VMHostService
}

2) Add new NTP server settings:

Connect-VIServer vcenterservername
Get-VMHost | Add-VMHostNtpServer 192.168.1.253
Get-VMHost | Add-VMHostNtpServer 192.168.1.254
Get-VMHost | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled:$true
Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService

Get-VMhost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "automatic"