vic*_*woo 59 c# powershell command process
例如:如果我跑notepad.exe c:\autoexec.bat,
我怎样才能c:\autoexec.bat在Get-Process notepadPowerShell中?
或者我怎样才能c:\autoexec.bat在Process.GetProcessesByName("notepad");C#中?
Ans*_*ers 107
在PowerShell中,您可以通过WMI获取进程的命令行:
$process = "notepad.exe"
Get-WmiObject Win32_Process -Filter "name = '$process'" | Select-Object CommandLine
Run Code Online (Sandbox Code Playgroud)
请注意,您需要管理员权限才能访问有关在其他用户的上下文中运行的进程的信息.作为普通用户,只有在您自己的上下文中运行的进程才能看到它.
Psy*_*ata 22
这个答案非常好,但是对于未来的防护和未来的帮助,除非你使用相当旧的powershell(在这种情况下我建议更新!)Get-WMIObject已经被Get-CimInstance取代了Hey Scripting Guy参考
试试这个
$process = "notepad.exe"
Get-CimInstance Win32_Process -Filter "name = '$process'" | select CommandLine
Run Code Online (Sandbox Code Playgroud)
我正在使用 powershell 7.1,这似乎现在作为脚本属性内置到进程对象中:
> (Get-Process notepad)[0].CommandLine
"C:\WINDOWS\system32\notepad.exe"
Run Code Online (Sandbox Code Playgroud)
有趣的是,您可以查看它的实现并看到它部分使用了来自 PsychoData 的答案:
($process | Get-Member -Name CommandLine).Definition
System.Object CommandLine {get=
if ($IsWindows) {
(Get-CimInstance Win32_Process -Filter "ProcessId = $($this.Id)").CommandLine
} elseif ($IsLinux) {
Get-Content -LiteralPath "/proc/$($this.Id)/cmdline"
}
;}
Run Code Online (Sandbox Code Playgroud)
在进程上运行 Get-Member 表明它是System.Diagnostics.Process 的一个实例,但它有几个脚本化的属性。
其他属性是 FileVersion、Path、Product 和 ProductVersion。
如果将以下代码放入 powershell $profile 文件中,则可以永久扩展“process”对象类并使用“CommandLine”属性
例子:
get-process notepad.exe | select-object ProcessName, CommandLine
Run Code Online (Sandbox Code Playgroud)
代码:
$TypeData = @{
TypeName = 'System.Diagnostics.Process'
MemberType = 'ScriptProperty'
MemberName = 'CommandLine'
Value = {(Get-CimInstance Win32_Process -Filter "ProcessId = $($this.Id)").CommandLine}
}
Update-TypeData @TypeData
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77024 次 |
| 最近记录: |