powershell - 如何检测 Powershell 中的 Powershell 嵌套?

标签 powershell

是否可以从 Powershell 中检测它是否是嵌套 shell?

如果我打开 Powershell 或 cmd.exe 窗口,然后输入 powershell 在那里,是否有一个神奇的 $host.somevariable 我可以查询它是否是一个“内部”shell?

最佳答案

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

$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'
}

关于powershell - 如何检测 Powershell 中的 Powershell 嵌套?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4380531/

相关文章:

从 Powershell 输出生成的 Python 转义 JSON 文件

powershell - 区分注册表项和值路径

powershell - 使用 Powershell 将 csv 导入到 SharePoint 列表

PowerShell:无法捕获 get-content 抛出的 UnauthorizedAccessException

regex - 使用 RegEx 提取逗号分隔值

powershell - 使用 PowerShell 获取电子邮件

powershell - 获取所有 xml 属性的列表

arrays - 将目录文件夹名称存储到阵列 Powershell

Powershell随机选择命令执行

windows - ng serve --open 不能从 Powershell 和 VS 终端(Powershell)工作