powershell - 为什么要为 $ 赋值?崩溃Powershell?

标签 powershell powershell-5.0

这个问题在这里已经有了答案:





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

(1 个回答)


4年前关闭。




运行代码 $? = $true在 powershell 中,程序崩溃并显示“powershell 已停止工作”Windows 错误消息。

一些内置函数,如 $true$host抛出错误:Cannot overwrite variable true because it is read-only or constant. .其他喜欢 $myinvocation$null只是被忽略。为什么是 $?不同的?是否有关于哪些内置函数有哪些响应的文档? help about_automatic_variables 没有任何细节。

我目前运行的是 5.1 版。我也尝试使用 powershell -version $n '$? = $true'和版本 3-5 都崩溃了。有趣的是,版本 1 和 2 没有;他们抛出只读变量错误。

最佳答案

看起来这是一个 known bug .
该修复程序已添加到当前正在开发的版本 (6.0) 中。你可以找到相关的pull request here .

我的胆量告诉我,不同行为(与其他自动变量相比)的原因是您和系统都试图几乎同时写入此变量。即使我不知道你为什么要写信给它(我怀疑这一切都是从一些 if ($? = $true) 开始的……),但我确实同意一个人真的很讨厌。

关于powershell - 为什么要为 $ 赋值?崩溃Powershell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41251513/

相关文章:

PowerShell:在目录中查找相似的文件名

windows - FuncOne() - 和 FuncTwo() 不工作

powershell - 可选字符串参数(应为 NULL)

powershell - 用Powershell add-adgroupmember结束

Powershell - 为什么 System.String 方法调用在字符串文字上失败?

powershell - 将某个目录中的所有函数加载到 PowerShell 中

powershell - Import-Module 仅在从 Get-Module 进行管道传输时才有效

powershell - DSC 配置不断重启

powershell - DSC 自定义资源依赖项

powershell - 检查管理员权限在某些服务器上不起作用的代码