Difference between revisions of "Script Extracts and Examples (PowerCLI)"

Jump to navigation Jump to search
→‎Script Extracts: Added "ESX BIOS, NIC and HBA Driver Versions"
(→‎Script Extracts: Added "ESX Storage Events")
(→‎Script Extracts: Added "ESX BIOS, NIC and HBA Driver Versions")
Line 915: Line 915:
</source>
</source>
   
   
=== ESX BIOS, NIC and HBA Driver Versions ===
Getting BIOS, NIC and HBA driver versions is dependant on what's reported to the ESX by the hardware vendor's CIM provider.  This script was written using various HP servers and may well only work for them.
<source lang="Powershell">
# ===============================================================
# ESX Inventory Getter
# ===============================================================
# Simon Strutt        November 2010
# ===============================================================
#
# Version 1
# - Initial Creation
#
# Version 2 - Dec 2010
# - Added BiosVer, CpuCores, CpuModel, HbaModel, NicModel
# - Bugfix: Corrected VC connection handling
# - Removed dependancy on obseleted properties (from upgrade to PowerCLI v4.1.1)
#
# Limitations
# - Tested on HP DL380 (G7), BL465 (G7), BL495 (G6), BL685 (G5)
# - Supports 1 distinct HBA model, 2 distinct NIC models
#
# ================================================================
$start = Get-Date
$OutputFile = "ESXs.csv"
$VC_List = "ESX-Check.csv"
$UserFile = "User.fil"
$PassFile = "Pass.fil"                          # Encrypted file to store password in
$Results = @()
# Include library files
. .\lib\Standard.ps1
Start-Transcript -Path ESX-Inventory.log
Log "Started script run at $start"
# Function-U-like ===================================================================================
Function Get-NicDriverVersion ($view, $driver) {
    # Gets the CIM provided driver version (tallies up driver name with CIM software component)
    $result = ($view.Runtime.HealthSystemRuntime.SystemHealthInfo.NumericSensorInfo | Where {$_.Name -like $driver + " driver*"} | Get-Unique).Name
    $result = ([regex]::Matches($result, "(\b\d)(.*)(?=\s)"))[0].Value        # HP regex to extract "2.102.440.0" for example
    $result
}
# ===================================================================================================
# Load list of VC's
try {
    $VCs = Import-CSV $VC_List
} catch {
    Log "ERROR: Failed to load list of vC's"
    Exit
}
# Load password credential from encrypted file
$pass = Get-Content $PassFile | ConvertTo-SecureString
$user = Get-Content $UserFile
$cred = New-Object System.Management.Automation.PsCredential($user, $pass)
foreach ($vc in $VCs) {
   
    # Connect to VC
    try {
        Log("Connecting to " + $vc.vc)
        $VCconn = Connect-VIServer -Server $vc.vc -Credential $cred -errorAction "Stop"
    } catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidLogin] {
        Log("Unable to connect to vCentre, invalid logon error !!")
        Log("Abandoning further script processing in order to prevent potential account lockout.")
        Break
    } catch {
        Log("Unable to connect to vCentre - " + $_)
        Continue
    }
   
    # Get ESX objects
    Log("Getting list of ESXs to check on " + ($vc.vc) + "...")
    $ESXs = Get-VMHost -Server $vc.vc | Sort -property Name
    Log("Got list of " + ($ESXs.Count) + " ESXs to check")
   
    ForEach ($ESX in $ESXs) {
        $row = "" | Select VC, Cluster, Name, IP, Version, Make, Model, BiosVer, CpuCores, CpuModel, HbaModel, HbaDriver, HbaDriverVer, Nic1Model, Nic1Driver, Nic1DriverVer, Nic2Model, Nic2Driver, Nic2DriverVer
        Log($ESX.Name)
       
        # Store objects which get re-used for efficiency
        $ESXview = Get-View -VIObject $ESX
        $VMHostNetworkAdapter = Get-VMHostNetworkAdapter -VMHost $esx
       
        # Get the basics
        $row.VC = $vc.vc
        $row.Cluster = $ESX.Parent
        $row.Name = $ESX.Name.Split(".")[0]
        $row.IP =  ($VMHostNetworkAdapter | Where {$_.ManagementTrafficEnabled -eq "True" -or $_.DeviceName -like "vswif*" -or $_.Name -eq "vmk0"}).IP
        $row.Version = $ESX.Version + " (" + $ESX.Build + ")"
        $row.Make = $ESX.Manufacturer
        $row.Model = $ESX.Model
       
        # Now onto the more ESX version / hardware specfic stuff (new versions of hardware will require further work below)
       
        # BIOS
        if ($ESXView.Hardware.BiosInfo) {    # Works on some systems
            $row.BiosVer = $ESXview.Hardware.BiosInfo.BiosVersion + " " + $ESXview.Hardware.BiosInfo.ReleaseDate.ToString("yyyy-MM-dd")  # Need date for HP servers as they use same version no for diff versions!
        } else {
            $row.BiosVer = ($ESXview.Runtime.HealthSystemRuntime.SystemHealthInfo.NumericSensorInfo | Where {$_.Name -like "*BIOS*"}).Name
            $row.BiosVer = ([regex]::Matches($row.BiosVer, "[A-Z]\d{2} 20\d{2}-\d{2}-\d{2}"))[0].Value          # HP regex to extract "A19 2010-09-30" for example
        }
       
        # CPU info
        $row.CpuCores = $ESXview.Hardware.CpuInfo.NumCpuCores.ToString() + " (" + $ESXview.Hardware.CpuPkg.count + "x" + $ESXview.Hardware.CpuPkg[0].ThreadId.count + ")"
        $row.CpuModel = $ESX.ExtensionData.Summary.Hardware.CpuModel
       
        # HBA info (script assumes only one HBA model in use)
        $row.HbaModel = ($ESX.ExtensionData.Config.StorageDevice.HostBusAdapter | Where {$_.Key -like "*FibreChannel*"})[0].Model
        $row.HbaDriver = ($ESX.ExtensionData.Config.StorageDevice.HostBusAdapter | Where {$_.Key -like "*FibreChannel*"})[0].Driver    # Includes version for ESX3
        $row.HbaDriverVer = ($ESXview.Runtime.HealthSystemRuntime.SystemHealthInfo.NumericSensorInfo | Where {$_.Name -like "*" + $row.HbaDriver + "*"}).Name
        $row.HbaDriverVer = ([regex]::Matches($row.HbaDriverVer, "(\b\d)(.*?)(?=\s)"))[0].Value
       
        # NIC info (script only supports two distinct NIC types)
        $nics = $ESXview.Hardware.PciDevice | Where {$_.ClassId -eq 512} | Select VendorName, DeviceName, Id | Sort-Object -Property DeviceName -Unique
        if ($nics.count) {
            $row.Nic1Model = $nics[0].VendorName + " " + $nics[0].Devicename
            $row.Nic2Model = $nics[1].VendorName + " " + $nics[1].Devicename
           
            # Use PCI ID to match up NIC hardware type with driver name
            $row.Nic1Driver = ($VMHostNetworkAdapter | Where {$_.ExtensionData.Pci -eq $nics[0].Id}).ExtensionData.Driver
            $row.Nic2Driver = ($VMHostNetworkAdapter | Where {$_.ExtensionData.Pci -eq $nics[1].Id}).ExtensionData.Driver
           
            $row.Nic1DriverVer = Get-NicDriverVersion $ESXview $row.Nic1Driver
            $row.Nic2DriverVer = Get-NicDriverVersion $ESXview $row.Nic2Driver
        } else {
            # Annoyingly, $nics is not an array if there's only one NIC type, hence seperate handling
            $row.Nic1Model = $nics.VendorName + " " + $nics.Devicename
            $row.Nic1Driver = ($VMHostNetworkAdapter | Where {$_.ExtensionData.Pci -eq $nics.Id}).ExtensionData.Driver
            $row.Nic1DriverVer = Get-NicDriverVersion $ESXview $row.Nic1Driver
        }
       
        $Results = $Results + $row
    }
    Disconnect-VIServer -Server $VCconn -Confirm:$false
}
$Results | Format-Table *
Log("Writing results to $OutputFile...")
$Results | Export-Csv -path $OutputFile -NoTypeInformation
Log("All completed")
Stop-Transcript
       
</source>


[[Category:VMware]]
[[Category:VMware]]
[[Category:PowerShell]]
[[Category:PowerShell]]

Navigation menu