Skip to content

New-PSFormatXML miscalculates widths when using scriptblocks. #94

@jdhitsolutions

Description

@jdhitsolutions

When using scriptblocks, to define custom properties, the resulting width is way to large.

$props = @(
    "Handles",
    @{Name = "NPM(K)"; Expression = { [int]($_.npm / 1kb) } },
    @{Name = "PM(K)"; Expression = { [int]($_.pm / 1kb) } },
    @{Name = "WS(M)"; Expression = {
            if ($_.ws -ge 500MB) {
                "$([char]0x1b)[91m$([int]($_.ws/1mb))$([char]0x1b)[0m"
            }
            elseif ($_.ws -ge 250MB) {
                "$([char]0x1b)[93m$([int]($_.ws/1mb))$([char]0x1b)[0m"
            }
            else {
                [int]($_.ws / 1mb)
            }
        }
    },
    @{Name = "CPU"; Expression = { New-TimeSpan -Seconds $_.cpu } },
    "ID",
    @{Name = "ProcessName"; Expression = {
            if ($_.ws -ge 500MB) {
                "$([char]0x1b)[1;91m$($_.processname)$([char]0x1b)[0m"
            }
            elseif ($_.ws -ge 250MB) {
                "$([char]0x1b)[1;93m$($_.processname)$([char]0x1b)[0m"
            }
            else {
                $_.processname
            }
        }
    }
)
Get-Process -Id $pid | New-PSFormatXML -Properties $props -ViewName WS -FormatType Table -Path c:\scripts\wsprocess.format.ps1xml -Passthru

The function makes a best guess on column widths but something looks wrong.

  <TableColumnHeader>
            <Label>ProcessName</Label>
            <Width>210</Width>
            <Alignment>left</Alignment>
          </TableColumnHeader>

Suspect the function is using the length of the scriptblock and not the value.

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions