How to check NVMe Drives TBW in ESXi with PowerCLI

When working with SSDs, you have to keep an eye on its TBW ("Total Bytes Written" or "Terabytes Written").
A maximum TBW guarantee is typically provided by the vendor in their specifications.
This value describes how data can be written to the entire device until the warranty expires.
The current value can be checked with S.M.A.R.T.
This article explains how to check the TBW value on NVMe based drives running in an ESXi host with PowerShell or from the command line.
If you have a SATA based SSD drive, check this article.

In this example, I'm using the Samsung 950 PRO NVMe based flash drive. The endurance for this device is rated at 400 TBW.
Just mentioning that this still is a consumer-grade SSD. The lowest endurance Class for Virtual SAN, for example, starts at 365TBW.
Connect to the ESXi Host with SSH

1. Identify NVMe drives
>> esxcli nvme device list
HBA Name Status Signature
-------- ------ ---------------------
vmhba0 Online nvmeMgmt-nvme00610000

2. Get the "Data Units Written" counter. Replace vmhbaX id with the adapter identifies in step 2.
>> esxcli nvme device log smart get -A vmhba0 |grep Written
Data Units Written: 0xe1c78d

3. The counter is hexadecimal. Convert it to decimal. This can be done also from the command line, or the Windows Calculator in Programmer mode.
>>  printf "%d\n" 0xe1c78d
14796685
The counter is a little bit unusual as it refers to 1000 of 512-byte units. Multiply the result with 512000 and divide it by 1TB (1.099.511.627.776) for TBW or 1GB (1.073.741.824) for GBW.
14796685 * 512000 / 1099511627776 = 6,9 TBW
14796685 * 512000 / 1073741824 = 7055 GBW
(Please note that some devices have 4k block sizes. In that case, multiply with 4096000 instead of 512000).

You can also use this one-liner to do the calculation for GBW:
>>  i=$(esxcli nvme device log smart get -A vmhba0 |grep Written | cut -d ":" -f 2); echo $(($(printf "%d\n"
$i)*512000/1073741824)) GBW
7055 GBW
or TBW:
>>  i=$(esxcli nvme device log smart get -A vmhba0 |grep Written | cut -d ":" -f 2); echo $(($(printf "%d\n" $i)*512000/1099511627776)) TBW
6 TBW

PowerShell / PowerCLI Script
The following PowerShell script displays a report about all NVMe devices in a vCenter Server instance.
$AllInfo = @()
Foreach ($vmhost in Get-VMHost |Where-object {$_.powerstate -eq "PoweredOn"}) {
$esxcli = get-esxcli -V2 -vmhost $vmhost
$devices = $esxcli.nvme.device.list.Invoke()
Foreach ($device in $devices) {
$nvme = $esxcli.nvme.device.get.CreateArgs()
$nvme.adapter = $device.HBAName
$Info = "" | Select VMHost, HBA, Model, DataUnitsWritten, GBW, TBW
$Info.VMHost = $vmhost.Name
$Info.HBA = $device.HBAName
$Info.Model = ($esxcli.nvme.device.get.invoke($nvme) | Select-Object ModelNumber).ModelNumber
$Info.DataUnitsWritten = [int]($esxcli.nvme.device.log.smart.get.Invoke($nvme) |Select-Object DataUnitsWritten).DataUnitsWritten
$Info.GBW = [math]::round($Info.DataUnitsWritten*512000/1Gb)
$Info.TBW = [math]::round($Info.DataUnitsWritten*512000/1Tb, 1)
$AllInfo += $Info
}
}

$AllInfo
Output:
VMHost : esx4.virten.lab
HBA : vmhba0
Model : Samsung SSD 950 PRO 256GB
DataUnitsWritten : 14797188
GBW : 7056
TBW : 6,9