PowerShell 在执行命令时询问“您想如何打开此文件”

问题描述 投票:0回答:2

从 Windows 11 升级后,Select-String cmdlet 停止工作,每次我使用它时,它都会尝试“打开未知文件”,如下图所示:

Example of strange Select-String behaviour

当我尝试从批处理文件运行我的 ps 脚本之一时,我发现了这一点 - 在 Win 10 中它运行良好,但在 Win 11 中则不然。当我在 VSCode 中运行代码时,它工作正常,我不知道为什么。

Example of VSCode's Select-String correct behaviour

我尝试将文本从 VSCode(如果编码不同)复制到新文件并直接复制到 PowerShell 终端,但这些都不起作用。

我还尝试了新的 Windows 11 终端并将 PowerShell 更新到最新版本 (7.2.2 x64),但没有任何效果。

禁用/启用 Windows PowerShell 2.0 的系统功能在这种情况下也没有效果。

编辑:
我发现,如果我在脚本开头运行命令

Update-Script
(不带任何参数),
Select-String
会按预期工作(在此之前 cmdlet 似乎根本不存在 -
Get-Help Select-String
没有找到任何内容) ,但使用
Update-Script
后它会找到适当的帮助)。其他一些命令也有同样的作用(例如
Trace-Command
)。查看输出:

PS C:\> Get-Command Select-String
CommandType  Name           Version    Source
-----------  ----           -------    ------
Application  Select-String  0.0.0.0    C:\WINDOWS\system32\Select-String

PS C:\> Update-Script
PS C:\> Get-Command Select-String

CommandType  Name           Version    Source
-----------  ----           -------    ------
Cmdlet       Select-String  3.1.0.0    Microsoft.PowerShell.Utility

Trace-Command
“使”选择字符串像
Update-Script
一样正常工作并给出以下输出:

DEBUG: CommandDiscovery Information: 0 : Looking up command: Select-String
DEBUG: CommandDiscovery Information: 0 : Cmdlet found: Select-String  Microsoft.PowerShell.Commands.SelectStringCommand 
hello everybody!

为什么我必须这样做(称为

Update-Script
),而
Select-String
应该在没有它的情况下工作?

powershell windows-11
2个回答
4
投票

嗯,有很多关于 powershell“你想如何打开这个文件”的讨论(没有真正解决),但最后,这是我的问题:

https://serverfault.com/questions/1038546/powershell-start-job-returns-a-how-do-you-want-to-open-this-file-with-some-cm

解决方案:

删除文件c:\Windows\System32\Select-string(0字节,无文件扩展名)。我不知道它是怎么到那里的...


0
投票

感谢@Ravikiran R,我找到了一个名为 python 的文件,大小为 0kb。我删除了它,现在它工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.