如何在 PowerShell 或 C# 中获取进程的命令行信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17563411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:50:38  来源:igfitidea点击:

How to get Command Line info for a process in PowerShell or C#

c#powershellprocesscommand

提问by victorwoo

e.g: if I run notepad.exe c:\autoexec.bat,

例如:如果我跑notepad.exe c:\autoexec.bat

How can I get c:\autoexec.batin Get-Process notepadin PowerShell?

我怎样才能c:\autoexec.batGet-Process notepadPowerShell中?

Or how can I get c:\autoexec.batin Process.GetProcessesByName("notepad");in C#?

或者我怎样才能c:\autoexec.batProcess.GetProcessesByName("notepad");C#中?

采纳答案by Ansgar Wiechers

In PowerShell you can get the command line of a process via WMI:

在 PowerShell 中,您可以通过 WMI 获取进程的命令行:

$process = "notepad.exe"
Get-WmiObject Win32_Process -Filter "name = '$process'" | Select-Object CommandLine

Note that you need admin privileges to be able to access that information about processes running in the context of another user. As a normal user it's only visible to you for processes running in your own context.

请注意,您需要管理员权限才能访问有关在另一个用户的上下文中运行的进程的信息。作为普通用户,您只能看到在您自己的上下文中运行的进程。

回答by PsychoData

This answer is excellent, however for futureproofing and to do future you a favor, Unless you're using pretty old powershell (in which case I recommend an update!) Get-WMIObject has been superseded by Get-CimInstance Hey Scripting Guy reference

这个答案非常好,但是对于未来证明并为您提供帮助,除非您使用相当旧的 powershell(在这种情况下我建议更新!)Get-WMIObject 已被 Get-CimInstance 取代嘿脚本专家参考

Try this

尝试这个

$process = "notepad.exe"
Get-CimInstance Win32_Process -Filter "name = '$process'" | select CommandLine