3

I am having a bit of trouble understanding the documentation for SupportsShouldProcess, and was looking to see if someone can break it down a little more for me to understand.

Links used


Current Code

Function Test-ShouldProcess{
[CmdletBinding(SupportsShouldProcess)]
    Param(
        [Parameter(Mandatory=$false,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [String[]]$Path = "C:\Users\Abraham\Desktop\FileToDelete.txt",
        [String[]]$ComputerName =  $env:COMPUTERNAME
    )
Begin{
$File = Get-Item -Path $Path 
}
Process{
    foreach($Computer in $ComputerName){
        try {
        #$PSSession = New-PSSession -ComputerName $Computer -ErrorAction Stop

        if($PSCmdlet.ShouldProcess($Computer, "Removing: $File")){
            "$($file.Name) deleted"
    }
        elseif ($WhatIfPreference.IsPresent) {
            "Why does my what if switch go here?"
                }
            } catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
            "Unable to connect to $computer"
            "Error: $($error[0].Exception.Message.Split(':')[1].Trim())"
            }
        }
    }
}

Question

In my Process{} block, I have an if(), and elseif() condition to check if -Whatif was specified.

  • Test-ShouldProcess -WhatIf

I am having trouble understanding why my elseif() statement runs when specifying -WhatIf but, I get the message in my first if() condition:

What if: Performing the operation "Removing: C:\Users\Abraham\Desktop\FileToDelete.txt.FullName" on target "DESKTOP-OEREJ77".

..yet, it doesn't run the code in that block only in my elseif. Would it not be the other way around?

I'm aware im explicitly telling it to run that section if -WhatIf is specified (I know, kind of answers my question) but, im looking to see if there's something else to it. Seems like it would be the other way around.

Is this just an extra option for me?

Am I just thinking too much into this?

Dale K
  • 25,246
  • 15
  • 42
  • 71
Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24
  • 3
    `ShouldProcess` _always_ returns `$false` when `-WhatIf` is present - that's sort of the whole point, `-WhatIf` should _prevent_ execution of branches with irreversible side effects. The message is a side-effect of evaluating the _condition_ - something that _always_ happens (otherwise how would PowerShell know to enter the `if` block or not?) – Mathias R. Jessen May 15 '21 at 13:54
  • 1
    The presence of `$PSCmdlet.ShouldProcess` ensures that you get the message from `-WhatIf`, but the `if` block never evaluates to true itself and doesn't execute. The only true condition is the `elseif` where you have added a check for whether the `$WhatIfPreference.IsPresent`. This gives it the illusion that both blocks run, but that is not the case as it never is with `if/elseif/else`. See `$a = 1; if ($a -eq 1) { "Y" } elseif ($a -eq 1) { "ELSEIF" } else { "Nothing to see here" }` – Ash May 15 '21 at 13:56
  • Ohhhhhhh, thats odd. Makes sense though! If one of you would like to post as an answer, I'd be glad to accept. Thank you guys, I can now die in peace. lol:) – Abraham Zinala May 15 '21 at 14:00

0 Answers0