本文介绍了PowerShell 在 `if ( $? = $false )` 上崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PowerShell,但我遇到了可以在计算机上轻松重现的崩溃.

I'm playing with PowerShell and I've encountered a crash that I can easily reproduce on my computer.

我不确定代码是否正确.但是,运行以下代码会使 powershell.exepowershell_ise.exe 崩溃.我想我对 if ( $? = $false ) 的使用是错误的,但在这种情况下不应该发生崩溃.删除 If 语句有助于避免崩溃.

I'm not sure if the code is correct. However, running the following piece makes powershell.exe and powershell_ise.exe crash. I guess that my use of if ( $? = $false ) is wrong, but crash should not happen in such case. Removing If statement helps to avoid the crash.

有什么我遗漏的吗?

我运行的是 Windows 10 专业版和 PowerShell 5.1.14393.206.

I'm running Windows 10 Pro and PowerShell 5.1.14393.206.

更新 1

好的,感谢@Martin,我知道我错误地使用了 = 而不是 -eq.但是为什么会发生这种崩溃?

OK, thanks to @Martin I know that I mistakenly used = instead of -eq. But why this crash happens?

更新 2

向 PowerShell UserVoice 提交错误报告:https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/16977433-assigning-a-value-to-false-crashes

Filed a bug report to PowerShell UserVoice: https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/16977433-assigning-a-value-to-false-crashes

更新 3

这似乎是一个已知错误:https://github.com/PowerShell/PowerShell/issues/2243 应该很快修复 https://github.com/PowerShell/PowerShell/pull/2320

It seems to be a known bug: https://github.com/PowerShell/PowerShell/issues/2243 that should be fixed soon https://github.com/PowerShell/PowerShell/pull/2320

Test-Path "C:\test"
if ( $? = $false ) {
    Out-Host "Hello World"
}
Fault bucket 127386360339, type 5
Event Name: PowerShell
Response: Not available
Cab Id: 0

Problem signature:
P1: powershell.exe
P2: 10.0.14393.206
P3: stem.Management.Automation.PSInvalidCast
P4: stem.Management.Automation.PSInvalidCast
P5: ation.LanguagePrimitives.ThrowInvalidCastException
P6: ation.LanguagePrimitives.ThrowInvalidCastException
P7: Pipeli..ution Thread
P8:
P9:
P10:

推荐答案

您的代码错误.您正在分配 $false问号变量,这是一个只读 变量.您可能想将 = 替换为 -eq:

Your code is wrong. You are assigning $false to the question mark variable which is a read-only variable. You probably want to replace the = with -eq:

Test-Path "C:\test"
if ( $? -eq $false ) {
    Out-Host "Hello World"
}

这篇关于PowerShell 在 `if ( $? = $false )` 上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:16