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

[玩转系统] 星期五的乐趣与跨平台 PowerShell 提示

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

星期五的乐趣与跨平台 PowerShell 提示


今年对我来说是跨平台的一年。今天继续这个讨论,我有一些有趣而简单的事情。一个 PowerShell 提示功能,可以跨平台工作,并以我认为优雅的方式提供一些有意义的信息。您可能不需要该函数,但您可能想了解我如何创建考虑到跨平台兼容性的 PowerShell 函数。

目标

设计目标是创建一个提示,在彩色框中显示用户名和当前位置。框线颜色将指示用户是否在提升的 PowerShell 会话中运行。然后,提示符将显示当前日期和时间以及 PowerShell 版本。但您确实应该将该函数视为概念验证或模板。您可以替换与您相关的任何信息。尽管如此,您需要牢记跨平台兼容性。

开发跨平台功能

我知道我想在提示中显示当前用户名。您可以使用多种技术。我决定测试 $env:Username 是否已定义。该变量未在非 Windows 平台中定义。在 Linux 上,应该有一个 LOGNAME 环境变量。如果由于某种原因我找不到这些值中的任何一个,那么我将使用最后的默认值。

if ($env:userdomain -AND $env:username) {
        $me = "$($env:userdomain)$($env:username)"
    }
elseif ($env:LOGNAME) {
        $me = $env:LOGNAME
    }
else {
        $me = "PSUser"
    }

我应该指出,我在 Linux 上的测试是有限的,而且我没有在 Mac 上测试过任何东西,因为我无法访问 Mac。我在要在框中显示的文本中使用此值。

$text = "[$me] $($executionContext.SessionState.Path.CurrentLocation)"

您可以在此文本字符串中添加其他信息。我正在添加当前位置。您可以添加主机名、IP 地址、可用空间或任何您需要的内容。请记住,您应该编写代码来根据 PowerShell 和平台的版本派生值。

例如,如果用户(我)在提升的 PowerShell 会话中运行,我希望框颜色为红色。否则它应该是绿色的。在 Windows 平台上,我可以使用 .NET 类 System.Security.Principal.WindowsPrincipal。但是,该类在非 Windows 平台上的 PowerShell Core 中不可用。我使用 If/ElseIf/Else 构造来测试 PowerShell Core 或 Windows PowerShell 中的一些新变量。

if ($IsLinux) {
        if ($(id -g) -eq 0 ) {
            #running as SU
            $lineColor = "Red"
        }
        else {
            $lineColor = "Green"
        }
    }
    elseif ($isWindows -or $psEdition -eq 'desktop') {

        $IsAdmin = [System.Security.Principal.WindowsPrincipal]::new([System.Security.Principal.WindowsIdentity]::GetCurrent() ).IsInRole("Administrators")
        if ($IsAdmin) {
            $lineColor = "Red"
        }
        else {
            $lineColor = "Green"
        }
    }
    else {
        #for everything else not tested
        $lineColor = "Yellow"
    }

请注意,我有一个 Else 子句来处理所有其他情况。根据您的项目,您可能想要抛出异常。但由于这是一个提示功能,我不想这样做。

PowerShell提示功能

您可以在 GitHub 上找到完整的功能作为要点。

BoxPrompt.ps1:

#requires -version 5.1



This prompt should run cross platform, although Linux testing has been limited.

To use the prompt dot source the script file in your PowerShell profile script.

This may not display properly in the Visual Studio Code PowerShell integrated console
#>

Function prompt {

    if ($env:userdomain -AND $env:username) {
        $me = "$($env:userdomain)$($env:username)"
    }
    elseif ($env:LOGNAME) {
        $me = $env:LOGNAME
    }
    else {
        $me = "PSUser"
    }
    $text = "[$me] $($executionContext.SessionState.Path.CurrentLocation)"

    if ($IsLinux) {
        if ($(id -g) -eq 0 ) {
            #running as SU
            $lineColor = "Red"
        }
        else {
            $lineColor = "Green"
        }
    }
    elseif ($isWindows -or $psEdition -eq 'desktop') {

        $IsAdmin = [System.Security.Principal.WindowsPrincipal]::new([System.Security.Principal.WindowsIdentity]::GetCurrent() ).IsInRole("Administrators")
        if ($IsAdmin) {
            $lineColor = "Red"
        }
        else {
            $lineColor = "Green"
        }
    }
    else {
        #for everything else not tested
        $lineColor = "Yellow"
    }

    $top = "┌$(("─" * $($text.length+2)))┐"
    Write-Host $top -ForegroundColor $lineColor
    Write-Host "│ " -ForegroundColor $lineColor -NoNewline
    Write-Host $text -NoNewline
    Write-Host " │" -ForegroundColor $lineColor
    $bottom = "└$(("─" * $($text.length+2)))┘"
    Write-Host $bottom -ForegroundColor $lineColor

    #parse out the PSVersionTable to most meaningful values
    $ver = $(([regex]"\d+\.\d+.\d+").match($psversiontable.psversion).value)

    Write-Host "[$(Get-Date)]" -ForegroundColor Yellow -NoNewline
    #the prompt function needs to write something to the pipeline
    " PS v$ver$('>' * ($nestedPromptLevel + 1)) "

}

与其他 PowerShell 提示功能一样,您需要点源脚本。

. C:\scripts\boxprompt.ps1

如果您想一直使用它,请将此行放入您的 PowerShell 配置文件脚本中。

以下是各种 PowerShell 会话中的提示示例。

[玩转系统] 星期五的乐趣与跨平台 PowerShell 提示

[玩转系统] 星期五的乐趣与跨平台 PowerShell 提示

[玩转系统] 星期五的乐趣与跨平台 PowerShell 提示

[玩转系统] 星期五的乐趣与跨平台 PowerShell 提示

最终结果是我有一个简单的 PowerShell 提示函数,它提供了在任何 PowerShell 实例上运行的更多信息。而且还有增长的空间,因为如果需要,我可以将其他信息添加到框中的文本字符串中。

我希望你能解决这个问题并让我知道你的想法。周末愉快。

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

取消回复欢迎 发表评论:

关灯