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

[玩转系统] 谁在驾驶这个外壳?

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

谁在驾驶这个外壳?


Microsoft 一直忙于下一代 PowerShell。您应该已经知道,该版本将跨平台运行。可执行文件或引擎自然与您习惯使用的 Windows PowerShell 不同。当我尝试最新的 PowerShell Beta 版时,我需要确定当前 PowerShell 引擎的路径。然后我认为获取更多详细信息可能会有所帮助,因此我组合了一个名为 Get-PowerShellEngine 的快速 PowerShell 函数。

获取当前的 PowerShell 引擎非常容易,因为我们可以使用内置的 $pid 变量来引用当前 PowerShell 会话的进程 ID。

Get-Process -id $pid | Select -ExpandProperty Path

您可以在任何 PowerShell 会话中尝试该操作,您应该获得类似 C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe 或 C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell_ise.exe 的路径。但由于我总是想要更多,所以我决定从 $PSVersionTable 和 $Host 获取一些附加属性。完整的功能是 GitHub 上的要点。

Get-PowerShellEngine:

#Requires -version 4.0

Function Get-PowerShellEngine {
     Get-PowerShellEngine
    C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe
    .Example
    PS C:\> Get-PowerShellEngine -detail

    Path           : C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe
    FileVersion    : 10.0.15063.0 (WinBuild.160101.0800)
    PSVersion      : 5.1.15063.502
    ProductVersion : 10.0.15063.0
    Edition        : Desktop
    Host           : Visual Studio Code Host
    Culture        : en-US
    Platform       :

    Result from running in the Visual Studio Code integrated PowerShell terminal

    .Example
    PS C:\> Get-PowerShellEngine -detail

    Path           : C:\Program Files\PowerShell.0.0-beta.5\powershell.exe
    FileVersion    :
    PSVersion      : 6.0.0-beta
    ProductVersion :
    Edition        : Core
    Host           : ConsoleHost
    Culture        : en-US
    Platform       : Win32NT

    Result from running in a PowerShell 6 session on Windows 10

    .Example
   PS /home/> get-powershellengine -Detail                           

    Path           : /opt/microsoft/powershell/6.0.0-beta.5/powershell
    FileVersion    : 
    PSVersion      : 6.0.0-beta
    ProductVersion : 
    Edition        : Core
    Host           : ConsoleHost
    Culture        : en-US
    Platform       : Unix

    Result from running in a PowerShell session on Linux

    .Link
    $PSVersionTable
    .Link
    $Host
    .Link
    Get-Process

    .Outputs
    [string]
    [pscustomobject]
    #>
    [CmdletBinding()]
    Param([switch]$Detail)
    
    #get the current PowerShell process and the file that launched it
    $engine =  Get-Process -id $pid | Get-Item
    if ($Detail) {
        [pscustomobject]@{
            Path = $engine.Fullname
            FileVersion = $engine.VersionInfo.FileVersion
            PSVersion = $PSVersionTable.PSVersion.ToString()
            ProductVersion = $engine.VersionInfo.ProductVersion
            Edition = $PSVersionTable.PSEdition
            Host = $host.name
            Culture = $host.CurrentCulture
            Platform = $PSVersionTable.platform
        }
    }
    else {
        $engine.FullName
    }
}

默认行为是返回路径。但是使用 -Detail 参数会将自定义对象写入管道。我在不同的 PowerShell 会话中尝试了很多乐趣。

[玩转系统] 谁在驾驶这个外壳?

[玩转系统] 谁在驾驶这个外壳?

[玩转系统] 谁在驾驶这个外壳?

[玩转系统] 谁在驾驶这个外壳?

正如您从屏幕截图中看到的,并非所有属性都具有值,具体取决于您的操作系统或托管应用程序。但随着 PowerShell 测试版的推进以及我们最终达到 RTM,这种情况可能会发生变化。我希望您能尝试一下并告诉我您的想法。如果您遇到问题,请在要点页面上发布问题。

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

取消回复欢迎 发表评论:

关灯