我遇到一个奇怪的问题,在设置“Set-PSDebug -Trace 2”时出现不同的行为。
我追踪到 switch 语句未正确执行,并且能够在 Powershell V3 上重现它,但不能在 Powershell V2 或 Powershell V1 上重现它(按预期工作)
采用以下简单函数:
function DoTest {
$result = "Switch Case Not Executed"
$VendorName = "Microsoft"
switch ($VendorName)
{
"Microsoft" { $result = "Switch Case Executed" }
}
Write-host "Switch: $($VendorName) -> $result"
}
现在运行以下命令:
#Works as expected
Set-PSDebug -Off; DoTest;
#Doesn't work as expected
Set-PSDebug -Trace 2; DoTest;
使用 PSDebug Trace 在 PosH V3 上的结果
DEBUG: 3+ Set-PSDebug -Trace 2; >>>> DoTest;
DEBUG: 1+ function DoTest >>>> {
DEBUG: ! CALL function 'DoTest'
DEBUG: 2+ >>>> $result = "Switch Case Not Executed"
DEBUG: ! SET $result = 'Switch Case Not Executed'.
DEBUG: 3+ >>>> $VendorName = "Microsoft"
DEBUG: ! SET $VendorName = 'Microsoft'.
DEBUG: ! SET $switch = 'Microsoft'.
DEBUG: 4+ switch ( >>>> $VendorName)
DEBUG: ! SET $switch = ''.
DEBUG: 9+ >>>> Write-host "Switch: $($VendorName) -> $result"
DEBUG: 9+ Write-host "Switch: $( >>>> $VendorName) -> $result"
Switch: Microsoft -> Switch Case Not Executed
DEBUG: 11+ >>>> }
在 PoSH 版本 3 中,即使调试跟踪也表明该值已设置,但似乎完全跳过了 switch 语句。我什至尝试过Set-StrictMode
一切都运行良好。仅当我启用 PSDebug 跟踪时才会出现此情况。这种行为是故意的吗?
经过详尽的调查,这似乎是 Powershell 中的一个错误。这$switch
变量在调试模式下被破坏,最终成为某种数组枚举器、空数组或类似的愚蠢的东西。我建议提交问题http://connect.microsoft.com http://connect.microsoft.com
解决方法1:
# Pro: No scoping differences between if statement blocks and function
# Con: big piles of If ... else if ... else if ... can get really annoying to code and maintain
# Con: Statements are limited to single execution, pipeline is lifted to function level instead of statement level
... same code as before but using an if instead of switch ...
if($VendorName = 'Microsoft') { $result = "Switch Case Executed" }
else { $result = "FAIL!" }
解决方法2:
# Pro: Each script block is self-contained and has private scope
# Pro: "cases" can be added without modifying DoIt function, and even passed into the function
# Pro: "cases" can be used in a pipeline
# Con: Indirect script blocks can be difficult to understand at first
function DoTest {
$result = "Switch Case Not Executed"
$VendorName = "Microsoft"
$SwitchHack = @{
Microsoft = { "Microsoft Case Executed" }
Default = { "Default Case Executed" }
}
if($SwitchHack.Keys -contains $VendorName) {
$result = $SwitchHack."$VendorName".InvokeReturnAsIs()
} else {
$result = $SwitchHack.Default.InvokeReturnAsIs()
}
'Switch: {0} -> {1}' -f $VendorName, $result | Write-Host
}
Set-PSDebug -Trace 2
DoTest
因为我很无聊,所以我写了解决方法 3,它只是 2,将 Switch 移到了函数中
function Switch-Object {
param(
[Parameter(Mandatory=$true)]
[String]$In,
[Parameter(Mandatory=$true)]
[hashtable]$Actions
)
try {
$Actions."$In".InvokeReturnAsIs()
}
catch {
throw "Unknown Action Label"
}
}
function DoTest {
$result = "Switch Case Not Executed"
$VendorName = "Microsoft"
$VendorActions = @{
Microsoft = { "Microsoft Case Executed" }
}
$result = Switch-Object -On:$VendorName -Actions:$VendorActions
'Switch: {0} -> {1}' -f $VendorName, $result | Write-Host
}
Set-PSDebug -Trace 2
DoTest
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)