当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 Windows 终端中测试 PowerShell

作者:精品下载站 日期:2024-12-14 07:56:40 浏览:14 分类:玩电脑

在 Windows 终端中测试 PowerShell


我几乎已经迁移到 Windows Terminal 作为我的主要 PowerShell 界面。尽管我的日常会话是 PowerShell 7,但我喜欢可以在同一应用程序中打开其他会话。是的,我知道仍然存在局限性,而且你们中的许多人更喜欢 ConEmu。那很好。 Windows Terminal 适合我的需求。我正在做的一件有趣的事情就是美化 PowerShell 7 选项卡。我在这里写过这一点。我的 PowerShell 7 配置文件包含使用自定义提示旋转图像的代码。但是,我意识到,如果我的 PowerShell 7 会话在 Windows 终端中运行,则只能运行该代码。我需要一种方法来发现该信息。

在 PowerShell 7 中,实际上从 PowerShell Core 开始,您可以轻松获取父进程。

[玩转系统] 在 Windows 终端中测试 PowerShell

$PID 是一个自动变量,反映当前 PowerShell 进程 ID。我应该指出,这个会话是在 Windows 终端中运行的。棘手的是,现在我正在使用 pwsh-preview 命令启动,该命令实际上是一个启动 pwsh.exe 的批处理文件。我可能可以修改 Windows 终端配置文件以直接启动 v7 可执行文件,但这并不重要。我可以检查祖父母流程。

[玩转系统] 在 Windows 终端中测试 PowerShell

事实上,我正在 Windows 终端中运行。

在 Windows PowerShell 中,情况略有不同,因为在使用 Get-Process 时没有 CodePproperty 可以返回父进程。幸运的是,您可以使用 Get-CimInstance 来检索信息。

[玩转系统] 在 Windows 终端中测试 PowerShell

通过这些代码片段,我构建了一个相对简单的函数,您可以在 GitHub 要点中找到该函数。

Test-IsWindowsTerminal.ps1:


#requires -version 5.1
Function Test-IsWindowsTerminal {
    [cmdletbinding()]
    [Outputtype([Boolean])]

    Param()

    Write-Verbose "Testing processid $pid"

    if ($PSVersionTable.PSVersion.major -ge 6) {
        #PowerShell Core has a Parent property for process objects
        Write-Verbose "Using Get-Process"
        $parent = (Get-Process -id $pid).Parent
        Write-Verbose "Parent process ID is $($parent.id) ($($parent.processname))"
        if ($parent.ProcessName -eq "WindowsTerminal") {
            $True
        }
        Else {
            #check the grandparent process
            $grandparent = (Get-Process -id $parent.id).parent
            Write-Verbose "Grandarent process ID is $($grandparent.id) ($($grandparent.processname))"
            if ($grandparent.processname -eq "WindowsTerminal") {
                $True
            }
            else {
                $False
            }
        }
    } #if Core or later
    else {
        #PowerShell 5.1 needs to use Get-CimInstance
        Write-Verbose "Using Get-CimInstance"

        $current = Get-CimInstance -ClassName win32_process -filter "processid=$pid"
        $parent = Get-Process -id $current.parentprocessID
        Write-Verbose "Parent process ID is $($parent.id) ($($parent.processname))"
        if ($parent.ProcessName -eq "WindowsTerminal") {
            $True
        }
        Else {
            #check the grandparent process
            $cimGrandparent = Get-CimInstance -classname win32_process -filter "Processid=$($parent.id)"
            $grandparent = Get-Process -id $cimGrandparent.parentProcessId
            Write-Verbose "Grandarent process ID is $($grandparent.id) ($($grandparent.processname))"
            if ($grandparent.processname -eq "WindowsTerminal") {
                $True
            }
            else {
                $False
            }
        }
    } #PowerShell 5.1
} #close function

在我的个人资料中,我可以点源该函数并设置一个变量:

$inWindowsTerminal = Test-IsWindowsTerminal

如果我必须运行依赖于 WindowsTerminal 的东西,我现在有办法进行检查。

如果您在 Windows 终端托管的 WSL 实例中运行 PowerShell,则该函数将不起作用。如果您正在运行嵌套的 PowerShell 实例,它也将不起作用,因为我不会检查祖父母进程之外的任何内容。尽管您可以修改代码以循环执行,直到到达最旧的祖先进程。

我还包含了详细的输出。

[玩转系统] 在 Windows 终端中测试 PowerShell

希望这个对你有帮助。享受并度过一个愉快的周末。

更新 :

发布后不久,我收到了一条非常有用的评论,提醒我 Windows Terminal 团队添加了一个环境变量来提供 GUID。这意味着您可以创建一个非常非常简单的函数,如下所示:

Function Test-IsWindowsTerminal { [bool]($env:WT_Session)}

或者,如果您想了解有关流程本身的更多详细信息,可以使用我的功能。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯