question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Displaying SwitchParameter causes subsequent output to fail...

See original GitHub issue

Put this in a file like testme.ps1:

param(
  [switch]$skip = $false,
  [switch]$weird = $false
)

if (! $skip) {
    $weird  # skip this line and it works!
}

$hash2=@{}

$hash2["aaa"]=1

"hash value is"

$hash2

Without the -skip parameter, nothing gets printed for the hash value…

PS C:\temp> powershell .\testme.ps1

IsPresent
---------
    False
hash value is

PS C:\temp> powershell .\testme.ps1 -skip
hash value is

Name                           Value
----                           -----
aaa                            1

PS C:\temp>

I would have expected the hash value output to be the same in both cases?

I see the same behavior on Core 7.1.3 and Desktop 5.1.19041.906.

Is there a workaround?

Thanks.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
vexx32commented, May 14, 2021

Yeah, I’d echo @jhoneill’s point and say if you need informational / debugging messages, use of Write-Verbose / Write-Debug and/or Write-Host depending on your exact needs is what you really want; those all avoid putting the data in the output stream and causing issues like this.

For errors I’d use throw or Write-Error, or (specifically in a function with [cmdletbinding()]) you can also use $PSCmdlet.ThrowTerminatingError() or $PSCmdlet.WriteError().

That is a bit tangential; this behaviour is still broken, but hopefully using those you should be able to work around it for the current problem you have. 🙂

2reactions
jhoneillcommented, May 14, 2021

I tend to recommend against having more than one type of object be output from a single function

I think I agree – but when you get errors and such and/or start trying to instrument code to see what is going on, that becomes pretty hard to ensure.

It is at this point write-verbose and write-debug become your friends

[cmdletbinding()]
param(
  [switch]$skip ,
  [switch]$weird 
)
if (! $skip) {
    Write-Verbose     $weird  
}
$hash2=@{}
$hash2["aaa"]=1
"hash value is"
$hash2
}

As @vexx32 said

[it] is still output. If you do $data = & ./testme.ps1 then $data will still have all three values in it.

Unless you use verbose/debug/host the instrumentation gets mixed up with the output and you need need to keep enabling and disabling it.

(the behavior after outputting a switch is still broken, but that’s another issue)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass a switch parameter to another PowerShell ...
It appears sourcing another script into the original just makes the parent's variables visible to all children so this will also work:
Read more >
Fun With the PowerShell Switch Parameter
In this call, just including the switch caused the output to display in upper case. Switches Work with Basic Functions Too. In the...
Read more >
about CommonParameters - PowerShell
The common parameters are a set of cmdlet parameters that you can use with any cmdlet. They're implemented by PowerShell, not by the...
Read more >
Understanding Windows PowerShell function parameters
The switch parameter determines whether to display the Parameter 2 value. Splatting. The above example only has three parameters. Passing values ...
Read more >
The PowerShell WhatIf Parameter: Looking Before you Leap
Learn, understand about, know how the PowerShell WhatIf parameter works and know what benefits it can bring. Learn also to leverage it.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found