You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PowerShell should be better able to reveal developer information on an unknown object #17701
PowerShell Write-* cmdlets are sending information to the related output streams which (all?) are eventually displaying information in a way the end-user would probably prefer to receive it but as a script developer, the way this is displayed is often misleading.
For a new PowerShell scripter, a known pitfall is the object enrolment were is unclear what type is actually being output from the pipeline in the case of a single item.
And there are a lot of different object types that all show the same "empty" result as e.g. '', $Null, @(), @(''), @($Null),@($Null, ''),
It would be nice if e.g. the Write-Debug cmdlet would an addition -Object parameter that reveals developer information about a specific object.
Currently, any argument other than a string provided to the Write-Object cmdlet results in an error:
$Name='John','Doe'Write-Debug$Name
Write-Debug: Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Message'. Specified method is not supported.
What I would like to see instead (as a developer), is a formatted object that doesn't leave any room for misinterpretation (e.g. the same way as it is required to be formatted for the PowerShell engine)
Wishful thinking:
$Name='John','Doe'Write-Debug$Name'John','Doe'
$Name=$NullWrite-Debug$Name$Null
$Name=''Write-Debug$Name''
$Name=@()
Write-Debug$Name@()
$Name=@('',$Null)
Write-Debug$Name@('',$Null)
This could go as deep as something prototyped here: ConvertTo-Expression which definitely doesn't make it an easy request.
changed the title [-]`Write-Debug` should be able to reveal developer information on a object[/-][+]`Write-Debug` should be able to reveal developer information on a PowerShell object[/+]on Jul 18, 2022
I guess that is indeed close to what I would like to see, but I do not want to predefine the <noun> in the the Format-* cmdlet as I have no clue what is in the object in advance (or what went wrong in my script). Take e.g. the StackOverflow questions concerning "You cannot call a method on a null-valued expression". The developers, get a clear error but are still not able to understand the cause of the issue. This is to my opinion because the standard display outputs are generally designed for the end-users (and not for the developers¹). In these outputs there is no easy way to distinguish between a $Null and objects as '', @(), @(''), @($Null) and @($Null, ''). Besides objects are quite flat in the end-users output where it gets even more vague determine value and the type of a property, e.g. $Object.Property.Method(), where the Property might be unexpectedly $Null or something else then imagined.
1) Also knowing that there aren't always sophisticated source code editors available in environments as production
$Data = ConvertFrom-Csv @'
Id,Name,Note
01,John,
02,Jack,
03,Ryan,Just a note
04,Luke,
05,Noah,
'@
ConvertTo-Expression $Data # Could be any statement/cmdlet or even a kind of developer mode
[pscustomobject]@{
Id = '01'
Name = 'John'
Note = ''
},
[pscustomobject]@{
Id = '02'
Name = 'Jack'
Note = ''
},
[pscustomobject]@{
Id = '03'
Name = 'Ryan'
Note = 'Just a note'
},
[pscustomobject]@{
Id = '04'
Name = 'Luke'
Note = ''
},
[pscustomobject]@{
Id = '05'
Name = 'Noah'
Note = $Null # <-- here is the issue
}
But that method is not easily available, requires knowledge of xmland the specific PowerShell schema (understanding what e.g. the Nil tag means) and quiet difficult to read for a human.
Basically: to talk to the PowerShell engine, I am required to "speak PowerShell".
As a developer, while debugging, I would like to see the engine to be able to respond back in PowerShell language.
As said before, I realize that a PowerShell language serializer is not build without fight (taking issues along with cycled properties and lose of fidelity in account) but at the other hand, I think that I have proven with the ConvertTo-Expression script isn't entirely impossible. Besides there are other leverages apart from debugging, a PowerShell language serializer lets you easily round-trip objects and could therefore be useful in building Pester scripts, DSC and to create a Minimal, Reproducible Examples.
changed the title [-]`Write-Debug` should be able to reveal developer information on a PowerShell object[/-][+]PowerShell should be better able to reveal developer information on an unknown object[/+]on Jul 20, 2022
# Could be any statement/cmdlet or even a kind of developer mode
Second thoughts: I think this should actually controlled from a kind of SupportsDebuggingcmdlet binding attribute which would than also partly cover the pitfall mentioned in #15781Strict Write-Output.
Meaning if the SupportsDebugging attribute is supplied to an advanced function and the concerned function is invoked with the common -Debug parameter, the objects at the end of the success stream will displayed in PowerShell notation rather than end-user strings.
This would imply that most existing cmdlets (that produce objects) might support this SupportsDebugging attribute as well in the future. But write-* cmdlets should specifically be excluded (except for maybe Write-Output which -afaik- in fact eventually handles the display output at the end of the pipeline)
Such a feature (Format-Pretty|Debug|All|???) would be a good candidate for a community-supplied PowerShell module. We're not sure using the -Debug parameter would be appropriate since that simply enables Write-Debug messages to go to the Debug stream. Those do not go to the Output stream.
Activity
[-]`Write-Debug` should be able to reveal developer information on a object[/-][+]`Write-Debug` should be able to reveal developer information on a PowerShell object[/+]fMichaleczek commentedon Jul 20, 2022
I think you are asking for new Format-* cmdlets.
Format-Hex is nice for string, char and valuetype, but not good with null
Format-Custom is nice with object, hashtable and collections.
A command Format-Pretty should be a good feature.
The base serialization is CliXml, if you want to import/export object, you could export and display CliXml.
iRon7 commentedon Jul 20, 2022
Flavien MICHALECZEK (@fMichaleczek),
I guess that is indeed close to what I would like to see, but I do not want to predefine the
<noun>in the theFormat-*cmdlet as I have no clue what is in the object in advance (or what went wrong in my script). Take e.g. the StackOverflow questions concerning "You cannot call a method on a null-valued expression". The developers, get a clear error but are still not able to understand the cause of the issue. This is to my opinion because the standard display outputs are generally designed for the end-users (and not for the developers¹). In these outputs there is no easy way to distinguish between a$Nulland objects as'',@(),@(''),@($Null)and@($Null, ''). Besides objects are quite flat in the end-users output where it gets even more vague determine value and the type of a property, e.g.$Object.Property.Method(), where thePropertymight be unexpectedly$Nullor something else then imagined.1) Also knowing that there aren't always sophisticated source code editors available in environments as production
Take the linked issue
#17683$null should be indicated by a blank line, especially when within an array as a example, as a developer I would like to be able to reveal the cause what happend here:Also take the very specific bug(?), I recently reported here:
#17702The last cell of an empty column read by ConvertFrom-Csv is inconsistently$Null. How would anyone able to easily reveal this?I indeed could also have used
PSSerializer.SerializeMethod for this:But that method is not easily available, requires knowledge of
xmland the specific PowerShell schema (understanding what e.g. theNiltag means) and quiet difficult to read for a human.Basically: to talk to the PowerShell engine, I am required to "speak PowerShell".
As a developer, while debugging, I would like to see the engine to be able to respond back in PowerShell language.
As said before, I realize that a PowerShell language serializer is not build without fight (taking issues along with cycled properties and lose of fidelity in account) but at the other hand, I think that I have proven with the ConvertTo-Expression script isn't entirely impossible. Besides there are other leverages apart from debugging, a PowerShell language serializer lets you easily round-trip objects and could therefore be useful in building Pester scripts, DSC and to create a Minimal, Reproducible Examples.
[-]`Write-Debug` should be able to reveal developer information on a PowerShell object[/-][+]PowerShell should be better able to reveal developer information on an unknown object[/+]iRon7 commentedon Jul 21, 2022
Second thoughts: I think this should actually controlled from a kind of
SupportsDebuggingcmdlet binding attribute which would than also partly cover the pitfall mentioned in #15781 Strict Write-Output.Meaning if the
SupportsDebuggingattribute is supplied to an advanced function and the concerned function is invoked with the common-Debugparameter, the objects at the end of the success stream will displayed in PowerShell notation rather than end-user strings.This would imply that most existing cmdlets (that produce objects) might support this
SupportsDebuggingattribute as well in the future. Butwrite-*cmdlets should specifically be excluded (except for maybeWrite-Outputwhich -afaik- in fact eventually handles the display output at the end of the pipeline)Wishful thinking:
rkeithhill commentedon Aug 23, 2022
WG-Engine discussed this today (8/22/2022).
Such a feature (Format-Pretty|Debug|All|???) would be a good candidate for a community-supplied PowerShell module. We're not sure using the
-Debugparameter would be appropriate since that simply enablesWrite-Debugmessages to go to the Debug stream. Those do not go to the Output stream.