重新启用导入模块Psreadline警告
这个警告在 Visual Code 应用程序的终端中不断弹出
Warning: PowerShell detected that you might be using a screen reader and has
disabled PSReadLine for compatibility purposes.
If you want to re-enable it, run 'Import-Module PSReadLine'.
即使我将值更改为0via regedit,警告仍然显示。
回答
您的症状的影响是:
-
在PowerShell 会话开始时,Windows 处于屏幕阅读器模式(视障人士的 Windows 辅助功能)。
-
您正在使用常规PowerShell 会话,无论是在控制台窗口/Windows 终端中还是在 Visual Studio Code 的集成终端中。
-
相反,如果你使用Visual Studio代码用PowerShell的扩展,这使得更加丰富的PowerShell代码创作经验,不会出现问题,因为扩展的所谓提供的会话PowerShell集成控制台做不执行此检查(从 version 开始
2021.2.2),因此会加载PSReadLine(提供丰富命令行编辑体验的模块)并且不会发出警告。目前尚不清楚这种无条件覆盖是设计还是疏忽。 -
虽然你可以按照错误消息的建议,并添加
Import-Module PSReadLine到您的$PROFILE文件,这样做会重新启用PSReadLine了丰富的命令行编辑的经验,但你仍然会看到警告,在启动时,因为它是由PowerShell的发射前该$PROFILE文件被加载。也就是说,如果您的系统设计启用了屏幕阅读器模式,那么这是正确的解决方案。
-
如果此模式被(不小心)持续打开,通过注册表,您可以按如下方式关闭它:
Set-ItemProperty 'registry::HKEY_CURRENT_USERControl PanelAccessibilityBlind Access' On 0
注意:
- 此更改需要注销或重新启动才能生效。
- 要查询持久模式,请使用:
Get-ItemPropertyValue 'registry::HKEY_CURRENT_USERControl PanelAccessibilityBlind Access' On
如果此模式在 OS-session 中意外打开,由行为不端的应用程序打开,因为它不会再次关闭该模式,或者在能够这样做之前已经崩溃,您可以临时编译 C# 代码以关闭模式(感激地改编自此 GitHub 评论),以便将来在同一操作系统用户会话中的 PowerShell 会话不再看到警告:
(Add-Type -PassThru -Name ScreenReaderUtil -Namespace WinApiHelper -MemberDefinition @'
const int SPIF_SENDCHANGE = 0x0002;
const int SPI_SETSCREENREADER = 0x0047;
[DllImport("user32", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
public static void EnableScreenReader(bool enable)
{
var ok = SystemParametersInfo(SPI_SETSCREENREADER, enable ? 1u : 0u, IntPtr.Zero, SPIF_SENDCHANGE);
if (!ok)
{
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
}
}
'@)::EnableScreenReader($false)