如何在Powershell中检测Powershell嵌套?

sco*_*obi 5 powershell

是否有可能从Powershell中检测它是否是嵌套的shell?

如果我打开一个Powershell或cmd.exe窗口,然后powershell在那里输入<enter>,是否有一个魔术$ host.somevariable我可以查询它是否是一个"内部"shell?

Rom*_*min 7

没有这样一个神奇的变量,更有可能.但是有可能获得这些信息:

$me = Get-WmiObject -Query "select * from Win32_Process where Handle=$pid"
$parent = Get-Process -Id $me.ParentProcessId
if ($parent.ProcessName -eq 'powershell') {
    'nested, called from powershell'
}
elseif ($parent.ProcessName -eq 'cmd') {
    'nested, called from cmd'
}
else {
    'not nested'
}
Run Code Online (Sandbox Code Playgroud)