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.

Tee Object Respects WhatIf Despite Not Impementing WhatIf

See original GitHub issue

Tee should either implement a WhatIf flag that I can set to -WhatIf:$False or completely ignore WhatIf

Steps to reproduce

https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/15374424-tee-object-doesn-t-work-in-scripts-functions-that

function Test-Tee {
    [Cmdletbinding(supportsshouldprocess)]
    Param()
    $true|tee -Variable b
    $b
}

Test-Tee

Test-Tee -WhatIf

Expected behavior

Return true for both

PS H:\> Test-Tee
True
True

PS H:\> Test-Tee -WhatIf
True
True

Actual behavior

PS H:\> Test-Tee
True
True

PS H:\> Test-Tee -WhatIf
True
What if: Performing the operation "Set variable" on target "Name: b Value: True".

Environment data

Name                           Value                                                                      
----                           -----                                                                      
PSVersion                      5.1.17134.48                                                               
PSEdition                      Desktop                                                                    
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                    
BuildVersion                   10.0.17134.48                                                              
CLRVersion                     4.0.30319.42000                                                            
WSManStackVersion              3.0                                                                        
PSRemotingProtocolVersion      2.3                                                                        
SerializationVersion           1.1.0.1 

Name                           Value
----                           -----
PSVersion                      6.0.2
PSEdition                      Core
GitCommitId                    v6.0.2
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ghostcommented, Oct 12, 2020

+1 just encountered this while trying to implement WhatIf on my own cmdlet.

2reactions
Chirishmancommented, Feb 19, 2019

I don’t think you’re getting the point.

With Set-Variable because it does have a settable -WhatIf I can choose to manually exclude it from any wrapping ShouldProcess however because Tee-Object is lacking that parameter I cannot chose to exclude it and thus cannot use it in any function that implements ShouldProcess.

As such the answer by your logic would be “Don’t use Tee-Object”, where I think a better answer is to just fix Tee-Object so that it’s usable, because Tee-Object is a useful tool.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Not seeing a -WhatIf option : r/PowerShell
I'm attempting to troubleshoot a script. I'm trying to use -WhatIf switch with Invoke-Command. It doesn't appear as an option on the server ......
Read more >
The PowerShell WhatIf Parameter: Looking Before you Leap
In this article, we'll demonstrate this parameter using the Get-Service , Stop-Service , and New-Item cmdlets. Checking for Powershell WhatIf ...
Read more >
I need my script to include the "LastWriteTime" property on ...
Caveat: Your script will then not respect the $WhatIfPreference variable, unless you explicitly check for it in case -WhatIf wasn't explicitly ...
Read more >
Implementing ShouldProcess For Your Functions
It indicates to the PowerShell subsystems that your function should be given two additional common parameters: -WhatIf and -Confirm . Uses of ...
Read more >
[SOLVED] Update Manager attribute with powershell
I need to replace the manager attribute for several AD users, but only if the manager attribute matches a certain value. An IF/THEN...
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