1

I wrote a powershell script to find the NVME driver version. It kind of works well, but need help to tweak a bit.

If there are multiple NVME storage adapters, my code works good. If there is only 1 NVME storage adapter - it's not picking up the version correctly. (Refer the attached screenshot: on right side, if multiple NVME under storage controller - my code works good, if only 1 NVME under storage controller, my below code does not show full version)

If there are multiple NVME storage adapters - below code shows output as 1.4.1.21 (which is expected)

$NVME = Get-WmiObject win32_PNPSignedDriver | select devicename,driverversion | where {_.devicename -like "*AWS NVMe"}
$NVMEVersion = $NVME.driverversion[0]

If there is 1 NVME storage adapter, same above code shows output as 1

Can anyone help me with code to get the full version of NVME, no matter is it 1 NVME storage adapter or multiple?

screenshot

phuclv
  • 37,963
  • 15
  • 156
  • 475
prof x
  • 21
  • 2
  • 1
    You can force `$NVME` to be an array, which should fix this by doing `[array]$NVME = Get-WmiObject win32_PNPSignedDriver | select devicename,driverversion | where {_.devicename -like "*AWS NVMe"}` or you can change how you're getting the first driverversion by doing `$NVMEVersion = $NVME.driverversion | Select -first 1` – TheMadTechnician Jun 08 '23 at 20:54
  • hey perfect !!! .. tried your second recommendation" -Select -first 1 " and worked so perfect. thank you. – prof x Jun 08 '23 at 21:34
  • 1
    Please [format your post properly](https://stackoverflow.com/help/formatting). – mklement0 Jun 08 '23 at 22:18
  • In short: PowerShell commands situationally output zero, one, or more objects. Only 2+ objects, when captured, become an _array_.A _single_ output object is captured _as itself_. If it happens to be a _string_, applying index `[0]` returns that string's _first character_. The same logic applies to values returned via [member-access enumeration](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Member-Access_Enumeration). One option is to enclose the command in `@(...)` before applying `[0]`, to ensure that the output is treated as an _array_. – mklement0 Jun 08 '23 at 22:28

0 Answers0