如何在 PowerShell 远程处理会话的提示中为机器名称添加颜色? [英] How can I add color to the machine name in the prompt of a PowerShell Remoting session?

查看:21
本文介绍了如何在 PowerShell 远程处理会话的提示中为机器名称添加颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

To make it more obvious when I'm remoted to a live/production server, I thought it'd be handy to be able to colour the machine name I'm connected to when using remote PowerShell sessions.

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

However, I can't see a way to do this... The server name prefix seems to be independent of the Prompt function, and even if I could use that, I'm not sure how I could define a new Prompt only for the duration of the session.

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

Is there a way to customise this? Note: I don't want to color all server names the same, I'd like a distinction between local/production servers.

推荐答案

经过一番搜索,您似乎是正确的,没有内置钩子来覆盖预提示 [computername]:标签.

After some searching around it seems like you are correct that there is not built-in hook to override the pre-prompt [computername]: tag.

幸运的是,我有一个可行的解决方法,可以为您服务!

Luckily, I have a hacky workaround which could work for you!

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

To get color, we can just use Write-Host. Write-Host output from the prompt function will be fully left-justified, which is what we want. Unfortunately, the default [computername]: tag is inserted directly afterward. That results in the computer name being duplicated in the prompt, once with color and once without.

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

We get around this by returning a string containing backspace characters, so the un-colored [computername]: will be overwritten. This is the normal prompt string, typically the current path.

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

Finally, in case the normal prompt string is short and does not fully overwrite the un-colored [computername]: tag, we need to do some final cleanup by adding dummy space characters. That might push out the caret, though, so we need to add more backspaces to return the caret to the corrent position.

总而言之,在您的远程机器上使用它:

All-up, use this on your remote machine:

# 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}"
}

这篇关于如何在 PowerShell 远程处理会话的提示中为机器名称添加颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆