Tee should either implement a WhatIf flag that I can set to -WhatIf:$False or completely ignore WhatIf
function Test-Tee {
[Cmdletbinding(supportsshouldprocess)]
Param()
$true|tee -Variable b
$b
}
Test-Tee
Test-Tee -WhatIf
Return true for both
PS H:\> Test-Tee
True
True
PS H:\> Test-Tee -WhatIf
True
True
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".
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 is with ShouldProcessParameters in common and is repro in latest nightly build,
Is this not simply expected behaviour? If I implement a function that doesn't have ShouldProcess support, which then calls a function that does... I should be able to wrap my original in a function that does support ShouldProcess in order for PS to echo that call down to the function that actually supports it:
function Test-NotSP {
Set-Variable -Name "Test" -Value "Testing..."
$Test
}
function Wrap-WithSP {
[CmdletBinding(SupportsShouldProcess)]
param()
Test-NotSP
}
PS> Wrap-WithSP -WhatIf
What if: Performing the operation "Set variable" on target "Name: Test Value: Testing...".
@vexx32 No. It makes ShouldProcess nearly useless for end-to-end testing. Set-Variable is actually an excellent example of why you _wouldn't_ want this to happen.
If I am using Set-Variable in a function to say, define a file name that I want to create with New-Item, the whole rest of the script will fail because the variable is undefined. Below is an example of how it can be worked around to explicitly exclude certain things from the ShouldProcess scenario. The problem comes when there is a function like Tee-Object where it cannot be explicitly excluded because it doesn't support ShouldProcess.
#Excluded in Inner Function
function Test-NotSP {
Set-Variable -Name "Test" -Value "Testing..." -WhatIf:$false
Set-Variable -Name 'Test2' -Value 'Testing2'
-join($Test,$Test2)
}
function Wrap-WithSP {
[CmdletBinding(SupportsShouldProcess)]
param()
Test-NotSP
}
#Support added, Excluded in outer function
function Test-WithSP {
[CmdletBinding(SupportsShouldProcess)]
Param()
Set-Variable -Name "Test" -Value "Testing..."
$Test
}
function Wrap-WithSPAndExclusion {
[CmdletBinding(SupportsShouldProcess)]
param()
Test-WithSP -WhatIf:$False
}
So... Don't use Set-Variable where you don't need it, then? Just do regular variable assignment.
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.
Seems fair. Shouldn't be overly difficult to have it implement -WhatIf properly by the looks of it, then; most likely it would simply be adding the attribute parameter.
+1 just encountered this while trying to implement WhatIf on my own cmdlet.