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?