Why is the assignment of a value of $? Powershell crash?

Code execution $? = $true $? = $true in powershell crashes the program with the Windows error message "Operating system shutdown message".

Some built-in functions, such as $true or $host , cause an error: Cannot overwrite variable true because it is read-only or constant. . Others, such as $myinvocation or $null , are simply ignored. Why $? is different? Is there any documentation on which the inline ones have an answer? help about_automatic_variables does not contain any details.

I am currently running version 5.1. I also tried using powershell -version $n '$? = $true' powershell -version $n '$? = $true' and version 3-5 all crashes. Interestingly, versions 1 and 2 do not; they cause a read-only variable error.

+5
source share
1 answer

This seems to be a known bug . A fix has been added to the version that is currently running (6.0). You can find the related traction request here .

My irritants tell me that the reason for the different behavior (compared to another automatic variable) is the fact that both you and the system are trying to write this variable almost simultaneously. And although I do not know why you would like to write (I suspect that it all started with some if ($? = $true) ...), I agree that this is really nasty.

+4
source

Source: https://habr.com/ru/post/1261602/


All Articles