【问题标题】:How can I add color to the machine name in the prompt of a PowerShell Remoting session?如何在 PowerShell 远程处理会话的提示中为机器名称添加颜色?
【发布时间】:2012-12-03 18:56:00
【问题描述】:

为了让我在远程到实时/生产服务器时更明显,我认为在使用远程 PowerShell 会话时能够为我连接的机器名称着色会很方便。

但是,我看不到这样做的方法...服务器名称前缀似乎独立于 Prompt 函数,即使我可以使用它,我也不确定如何定义一个新的仅在会话期间提示。

有没有办法自定义这个?注意:我不想将所有服务器名称都涂成相同的颜色,我想区分本地/生产服务器。

【问题讨论】:

    标签: powershell powershell-remoting powershell-3.0


    【解决方案1】:

    经过一番搜索,您似乎是正确的,没有内置的钩子可以覆盖预先提示 [computername]: 标记。

    幸运的是,我有一个可以为您工作的 hacky 解决方法!

    要获得颜色,我们可以使用Write-Hostprompt 函数的 Write-Host 输出将完全左对齐,这就是我们想要的。不幸的是,默认的[computername]: 标签是在之后直接插入的。这会导致计算机名称在提示中重复,一次有颜色,一次没有。

    我们通过返回一个包含退格字符的字符串来解决这个问题,因此未着色的[computername]: 将被覆盖。这是正常的提示字符串,通常是当前路径。

    最后,如果正常提示字符串很短并且没有完全覆盖未着色的[computername]: 标签,我们需要通过添加虚拟空格字符来进行最后的清理。不过,这可能会推出插入符号,因此我们需要添加更多的退格来将插入符号返回到正确位置。

    总之,在你的远程机器上使用它:

    # put your logic here for getting prompt color by machine name
    function GetMachineColor($computerName)
    {
       [ConsoleColor]::Green
    }
    
    function GetComputerName
    {
      # if you want FQDN
      $ipProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
      "{0}.{1}" -f $ipProperties.HostName, $ipProperties.DomainName
    
      # if you want host name only
      # $env:computername
    }
    
    function prompt
    {
      $cn = GetComputerName
    
      # write computer name with color
      Write-Host "[${cn}]: " -Fore (GetMachineColor $cn) -NoNew
    
      # generate regular prompt you would be showing
      $defaultPrompt = "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
    
      # generate backspaces to cover [computername]: pre-prompt printed by powershell
      $backspaces = "`b" * ($cn.Length + 4)
    
      # compute how much extra, if any, needs to be cleaned up at the end
      $remainingChars = [Math]::Max(($cn.Length + 4) - $defaultPrompt.Length, 0)
      $tail = (" " * $remainingChars) + ("`b" * $remainingChars)
    
      "${backspaces}${defaultPrompt}${tail}"
    }
    

    【讨论】:

    • 这个有点问题;如果计算机是远程计算机并且具有类似 mycomputer.mydomain.com 的名称,那么它似乎搞砸了;大概是因为 $env:ComputerName 只是本地部分?
    • 是的,$env:ComputerName 只是主机名。我相信 Powershell 添加的标签与您传递给Enter-PsSession -ComputerName 的字符串匹配。也就是说,如果您只提供主机名,它只会显示主机名,即使对于加入域的框也是如此。如果您选择完全限定它,Powershell 将打印完全限定名称。所有这些提示代码都在远程机器上运行,它无法知道您可能选择了哪个选项。
    • 找到了一种新的方法来解决这个问题;我的辅助连接功能可以将用于连接的主机传递到远程会话,然后提示功能(如果已定义)Invoke-Command -Session $session -ScriptBlock { $connectedHost = $using:remoteHost } 可以使用该主机
    【解决方案2】:

    我使用Posh-Git 来完成此操作。见他们的Prompt Customization。我注意到有些文档有点过时了,如果你在 PowerShell 中输入$GitPromptSettings,你会看到所有可用的属性。使用 Posh-Git 的额外好处是可以在提示符上查看 Git 统计信息。

    我用来设置机器名的命令是...

    $GitPromptSettings.DefaultPromptPrefix = '$(get-content env:computername) '

    这里是颜色设置

    $GitPromptSettings.DefaultPromptPath.ForegroundColor = '橙色'

    【讨论】:

      猜你喜欢
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多