Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell crashes on `if ( $? = $false )`

Tags:

powershell

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

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.

Is there anything I'm missing?

I'm running Windows 10 Pro and PowerShell 5.1.14393.206.

Update 1

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

Update 2

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

Update 3

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: 
like image 562
bahrep Avatar asked Nov 06 '16 11:11

bahrep


Video Answer


1 Answers

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"
}
like image 155
Martin Brandl Avatar answered Sep 28 '22 09:09

Martin Brandl