封面《ノラと皇女と野良猫ハート》

问题描述

打开cmd的时候直接出错,提示“此时不应有&”,英文版应该是“& was unexpected at this time”。问题的实际情况见下图。

问题解决

幸运的是在stack overflow上能够找到解决方案。首先经过测试cmd.exe /d能够正常运行。cmd.exe /d命令可以阻止注册表中的AutoRun运行。这一点可以在cmd.exe /?中查看有哪些注册表需要检查。

需要查阅的注册表一共有两处

1
2
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor

使用Win+R并输入regedit打开注册表。查询结果如下

可以看到HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command ProcessorAutoRun中有一个奇怪的值。这里直接删除即可,或者运行下面的指令。

1
2
3
4
5
6
7
8
# 查询所有定义了AutoRun的位置
Get-ItemProperty -ea Ignore ('HKCU:', 'HKLM:' -replace '$', '\Software\Microsoft\Command Processor') AutoRun
# 查看如果执行了命令会发生什么
Get-ItemProperty -ea Ignore ('HKCU:', 'HKLM:' -replace '$', '\Software\Microsoft\Command Processor') AutoRun | Remove-
ItemProperty -Name AutoRun -WhatIf
# 删除 -WhatIf即可执行命令
Get-ItemProperty -ea Ignore ('HKCU:', 'HKLM:' -replace '$', '\Software\Microsoft\Command Processor') AutoRun | Remove-
ItemProperty -Name AutoRun

运行结果

运行后的注册表

运行后正常的CMD

后记

这个奇葩的小问题有点难受,不知道是哪个软件干的。所幸stack overflow上有问题的定位方法和解决过程。

参考资料

cmd.exe throws error “& was unexpected at this time.”