Skip to content

New-PSFormatXML - handle empty property values #81

@scriptingstudio

Description

@scriptingstudio

This is "unusual" way of object prototype creating which I often exploit.

$obj = '' | select-object <properties>
$obj.PSOBject.TypeNames.Insert(0,<FormatName>)
$obj | New-PSFormatXML -FormatType table -path <path>

You cannot call a method on a null-valued expression.
+ $Member.value.tostring().length
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

It is method .tostring() that throws error in line 195.
This happens because of property values are NULL. To avoid this a property value should be tested for $null.
It can be done like this (quick fix):

$len = if (-not $Member.value) {0} else {$Member.value.tostring().length}
$longest = $len, $member.name.length | Sort-Object | Select-Object -last 1

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions