我在理解SupportsShouldProcess的文档时遇到了一些麻烦,我想看看是否有人可以把它分解得更清楚一些让我理解。
使用的链接
当前代码
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())"
}
}
}
}
问题
在我的Process{}
块中,我有一个if()
, 和elseif()
条件来检查是否-Whatif
已指定。
Test-ShouldProcess -WhatIf
我无法理解为什么我的elseif()
语句在指定时运行,但是,我在第一个条件下-WhatIf
收到消息:if()
如果:对目标“DESKTOP-OEREJ77”执行“删除:C:\Users\Abraham\Desktop\FileToDelete.txt.FullName”操作。
..然而,它不会仅在我的elseif
. 不是反过来吗?
我知道我明确告诉它在-WhatIf
指定时运行该部分(我知道,有点回答我的问题)但是,我想看看它是否还有其他内容。似乎情况会反过来。
这对我来说只是一个额外的选择吗?
我只是想太多了吗?