首页 > 解决方案 > Powershell - 在 CMD 上运行命令 - 通配符抛出错误

问题描述

我正在尝试通过 CMD 运行以下 Powershell 命令:

powershell -command "Get-WmiObject Win32_Process | Where-Object {$_.CommandLine -like \"*C:\Windows\Test*\" } | Select-Object ProcessName, CommandLine"

上面的命令直接在 Powershell 上运行良好,但只有在我尝试在 CMD 上运行时才会出现问题。在我的测试中,我发现 * 符号无法正确处理,我试图在符号前放一个反斜杠进行测试,但这并没有成功。有没有办法让它与 CMD 中的 * 符号一起使用?

编辑: 此命令用于查看包含命令行 C:\Windows\Test 的进程

标签: windowspowershellcmdcommandwildcard

解决方案


更简单,不需要 PowerShell

wmic process where "commandline like '%c:\\windows\\test%'" get name, commandline

为了完整起见,为了正确地保持主题,使用 cmd.exe 中的 PowerShell,我会更像这样:

powershell -noprofile "get-ciminstance -query \"select * from win32_process where commandline like '%c:\\windows\\test%'\" | select-object -property processname, commandline"

推荐阅读