Displaying SwitchParameter causes subsequent output to fail...
See original GitHub issuePut 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:
- Created 2 years ago
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yeah, I’d echo @jhoneill’s point and say if you need informational / debugging messages, use of
Write-Verbose
/Write-Debug
and/orWrite-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
orWrite-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. 🙂
It is at this point
write-verbose
andwrite-debug
become your friendsAs @vexx32 said
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)