简体   繁体   English

找出进程是否在cmd.exe或PowerShell窗口中运行

[英]Find out if process is running in a cmd.exe or PowerShell window

Imagine you're writing a program in a high level language where you are not allowed to do custom system calls (win32 API) because code is cross compiling or the language doesn't support that. 假设您正在以高级语言编写程序,由于代码是交叉编译的,或者该语言不支持该语言,因此您不允许进行自定义系统调用(win32 API)。

When your process is running on windows, is there a way to find out if you're running inside powershell and not cmd.exe? 当您的进程在Windows上运行时,是否有办法找出您是在Powershell而不是cmd.exe内部运行? Or vice versa? 或相反亦然?

I'm trying to print "I'm running in powershell" and "I'm running in cmd.exe" from the program. 我正在尝试从程序中打印“我正在Powershell中运行”和“我正在cmd.exe中运行”。

In this case I'm writing the program in Golang but that shouldn't really matter because I don't want to traverse process tree. 在这种情况下,我正在用Golang编写程序,但这并不重要,因为我不想遍历进程树。

Eryksun might have a point about the usefullness of this but there is a post about getting the parent process . Eryksun可能对此有用,但有一篇关于获取父进程的文章

Putting the following text into a ps1 script: 将以下文本放入ps1脚本中:

$parentpid=(gwmi win32_process -Filter "processid='$pid'").parentprocessid
Write-Host ("I am running in {0}" -f (Get-Process -id $parentpid).Name)

Running it from CMD calling PowerShell: 从CMD调用PowerShell运行它:

I am running in cmd 我在cmd中运行

Running it from PowerShell and ISE: 从PowerShell和ISE运行它:

I am running in explorer 我在资源管理器中运行

So while explorer is not very useful it is not CMD which I think is what you actually wanted to know. 因此,尽管资源管理器不是很有用,但我想并不是您真正想知道的不是CMD。

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

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