简体   繁体   English

PowerShell 获取当前控制台进程

[英]PowerShell Get Current Console Process

I would like to get the process ID or handle of the current powershell script when I double click and run a PS1 file from Windows Explorer.当我双击并从 Windows Explorer 运行 PS1 文件时,我想获取当前 powershell 脚本的进程 ID 或句柄。

This works:这有效:

$hwndPowerShell = Get-Process -Name "PowerShell"

But if I have multiple PowerShell console open, how do I get the handle of the current script?但是如果我打开了多个 PowerShell 控制台,如何获取当前脚本的句柄?

I know one other way is to use the Win32 API FindWindow to find the latest instance of PowerShell console.我知道另一种方法是使用 Win32 API FindWindow 来查找 PowerShell 控制台的最新实例。

Try this command,in commandline property it will list the script path which we can use to identity the script and PID.试试这个命令,在命令行属性中它会列出我们可以用来识别脚本和 PID 的脚本路径。

Get-CimInstance -ClassName Win32_Process -filter "name = 'powershell.exe'" | Get-CimInstance -ClassName Win32_Process -filter "name = 'powershell.exe'" | Select-Object -property Name,CommandLine,HandleCount,ProcessID,WorkingSetSize,CreationDate Select-Object - 属性名称、命令行、HandleCount、ProcessID、WorkingSetSize、CreationDate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM