Skip to content

gwmi backend: Format-Table drops the WorkingSetSize column without a console (width 80), memory becomes NaN #196

Open

Description

@industry-capitan

Thanks for 4.1.0 - detecting WMIC.exe (#195) fixed memory reporting for us under pm2 on Windows Server. Reporting a remaining problem in the gwmi backend, which is still used where WMIC is absent (WMIC is an optional, deprecated feature on Windows 11 24H2+ / Server 2025) or with useGwmi: true.

What happens

lib/gwmi.js runs gwmi win32_process -Filter '...' | select CreationDate,KernelModeTime,ParentProcessId,ProcessId,UserModeTime,WorkingSetSize | format-table and splits each row on whitespace. Format-Table lays out to the host buffer width. When the calling process has no console (a Windows service, session 0, a process created via WMI), PowerShell uses a width of 80. A row needs ~95 characters, so Format-Table silently drops the last column:

CreationDate              KernelModeTime ParentProcessId ProcessId UserModeTime
------------              -------------- --------------- --------- ------------
20261020033638.737592+180       12343750            3340      5248     35781250

line[5] is undefined, so memory is NaN (null after JSON). The row check line.length === 1 lets the 5-column row through.

Reproduce

On Windows Server 2016 with pidusage 4.1.0, start this from a process without a console (for example Invoke-CimMethod -ClassName Win32_Process -MethodName Create, or from a Windows service):

const pidusage = require('pidusage')
pidusage(process.pid, { useGwmi: true }, (err, stats) => console.log(err, stats.memory)) // NaN
pidusage(process.pid, (err, stats) => console.log(err, stats.memory))                   // bytes (wmic)

From an interactive terminal (width 120) both calls return bytes.

Suggestion

Make the output independent of the console width, for example | ConvertTo-Csv -NoTypeInformation (or | Format-Table -AutoSize | Out-String -Width 4096) and parse that. Checking line.length !== 6 as wmic.js does would at least report an error instead of NaN.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions