# Found https://vmscribble.com/powercli/powercli-nvme-tbw-push-to-vrops-api
# Make sure to run these command in the same folder as the script for the vROps and VCSA login accounts.
# (get-credential).password | ConvertFrom-SecureString | set-content vrops.pass
# (get-credential).password | ConvertFrom-SecureString | set-content vcsa.pass
# Variables to edit
$vROPsServer = "vrops.my.lab"
$vROPSUser = "admin"
$esxihostid = "c08254d8-ca69-490e-8670-c2f696dd1a91"
$esxihostname = "esxi-dell.my.lab"
$vCenterfqdn = "vcsa1.my.lab"
$vCenteraccount = "Administrator@vsphere.local"
# vCenter credentials
$Passworddecodevcsa = Get-Content vcsa.pass | ConvertTo-SecureString
$vcsacredential = New-Object System.Management.Automation.PsCredential("$vCenteraccount",$Passworddecode)
$vcsaPassword = $vcsacredential.GetNetworkCredential().Password
# Connect to your vCenter
Connect-VIServer -Server $vCenterfqdn -User $vCenteraccount -Password $vcsaPassword | out-null
# Pull NVMe model/GBW/TBW - thanks - https://www.virten.net/2020/03/how-to-check-nvme-drives-tbw-in-esxi-with-powercli/
Foreach ($vmhost in Get-VMHost -Name $esxihostname ) {
$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
}
}
# Record NVMe model/GBW/TBW output as Variables for the vROps API POST.
$NVMemodel = $Info.Model
$NVMeGBW = $Info.GBW
$NVMeTBW = $Info.TBW
# Disconnect from the vCenter
Disconnect-VIServer $vCenterfqdn -Confirm:$false
# vROps credentials
$Passworddecode = Get-Content vrops.pass | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential("$vROPSUser",$Passworddecode)
$vROPsPassword = $credential.GetNetworkCredential().Password
# Powershell API connecton - thanks - https://socialvirtualcloud.com/injecting-data-into-vrops-via-api-from-external-system/
# Defining REST API URLS for use
$BaseURL = "https://" + $vROPsServer + "/suite-api/api/"
$BaseAuthURL = "https://" + $vROPsServer + "/suite-api/api/auth/token/acquire"
$Type = "application/json"
# Creating JSON for Auth Body
$AuthJSON =
"{
""username"": ""$vROPSUser"",
""password"": ""$vROPsPassword""
}"
# Authenticating with API
Try
{
$vROPSSessionResponse = Invoke-RestMethod -Method POST -Uri $BaseAuthURL -Body $AuthJSON -ContentType $Type
}
Catch
{
$_.Exception.ToString()
$error[0] | Format-List -Force
}
# Extracting the session ID from the response
$vROPSSessionHeader = @{"Authorization"="vRealizeOpsToken "+$vROPSSessionResponse.'auth-token'.token
"Accept"="application/json"}
# tested via swagger Resource addPropertiesUsingPOST - Add Properties to a Resource along with associating an adapter instance of the specified adapter kind as the source of data
# thanks - https://vman.ch/vrops-custom-network-stats-for-hostsystems/
[xml]$MetricXML = @('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ops:property-contents xmlns:ops="http://webservice.vmware.com/vRealizeOpsMgr/1.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ops:property-content statKey="NVMe|TBW">
<ops:timestamps>1119844280663</ops:timestamps>
<ops:values>'+$NVMeTBW+'</ops:values>
</ops:property-content>
<ops:property-content statKey="NVMe|GBW">
<ops:timestamps>1119844280663</ops:timestamps>
<ops:values>'+$NVMeGBW+'</ops:values>
</ops:property-content>
<ops:property-content statKey="NVMe|Model">
<ops:timestamps>1119844280663</ops:timestamps>
<ops:values>'+$NVMemodel+'</ops:values>
</ops:property-content>
</ops:property-contents>'
)
# vROPs POST URL using the esxi host id found in the variables.
$posturl = "$BaseURL/resources/$esxihostid/properties"
# One liner to POST into vROps
Invoke-RestMethod -Method POST -uri $posturl -Body $MetricXML -Headers $vROPSSessionHeader -ContentType "application/xml;charset=utf-8" -TimeoutSec 120