查明进程是否在 cmd.exe 或 PowerShell 窗口中运行 [英] Find out if process is running in a cmd.exe or PowerShell window

查看:45
本文介绍了查明进程是否在 cmd.exe 或 PowerShell 窗口中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,您正在使用高级语言编写程序,您不允许进行自定义系统调用 (win32 API),因为代码是交叉编译的,或者该语言不支持.

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.

当您的进程在 Windows 上运行时,有没有办法确定您是在 powershell 中运行而不是在 cmd.exe 中运行?反之亦然?

When your process is running on windows, is there a way to find out if you're running inside powershell and not cmd.exe? Or vice versa?

我正在尝试从程序中打印我在 powershell 中运行"和我在 cmd.exe 中运行".

I'm trying to print "I'm running in powershell" and "I'm running in cmd.exe" from the program.

在这种情况下,我正在用 Golang 编写程序,但这并不重要,因为我不想遍历进程树.

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.

推荐答案

Eryksun 可能对此的用处有所了解,但有一个 关于获取父进程的帖子.

Eryksun might have a point about the usefullness of this but there is a post about getting the parent process.

将以下文本放入 ps1 脚本中:

Putting the following text into a ps1 script:

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

从 CMD 调用 PowerShell 运行它:

Running it from CMD calling PowerShell:

我在 cmd 中运行

从 PowerShell 和 ISE 运行它:

Running it from PowerShell and ISE:

我在资源管理器中运行

因此,虽然资源管理器不是很有用,但我认为您真正想知道的并不是 CMD.

So while explorer is not very useful it is not CMD which I think is what you actually wanted to know.

这篇关于查明进程是否在 cmd.exe 或 PowerShell 窗口中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆