简体   繁体   English

如何在PowerShell Remoting会话的提示符下为计算机名称添加颜色?

[英]How can I add color to the machine name in the prompt of a PowerShell Remoting session?

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. 为了使当我远程连接到实时/生产服务器时更加明显,我认为使用远程PowerShell会话时能够为我连接的计算机名称着色会很方便。

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. 但是,我看不到这样做的方法...服务器名称前缀似乎与Prompt函数无关,即使我可以使用它,也不确定如何仅针对以下内容定义新的Prompt会话的持续时间。

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. 注意:我不想为所有服务器名称都涂上相同的颜色,我想区分本地服务器/生产服务器。

After some searching around it seems like you are correct that there is not built-in hook to override the pre-prompt [computername]: tag. 经过一番搜索之后,似乎没有使用内置的钩子来覆盖预提示[computername]:标签是正确的。

Luckily, I have a hacky workaround which could work for you! 幸运的是,我有一个不错的解决方法,可以为您服务!

To get color, we can just use Write-Host . 为了获得颜色,我们可以只使用Write-Host Write-Host output from the prompt function will be fully left-justified, which is what we want. prompt功能的Write-Host输出将完全左对齐,这就是我们想要的。 Unfortunately, the default [computername]: tag is inserted directly afterward. 不幸的是,默认的[computername]:标记是紧随其后插入的。 That results in the computer name being duplicated in the prompt, once with color and once without. 这将导致计算机名称在提示中重复,一次使用颜色,一次不使用。

We get around this by returning a string containing backspace characters, so the un-colored [computername]: will be overwritten. 我们通过返回包含退格字符的字符串来解决此问题,因此无色的[computername]:将被覆盖。 This is the normal prompt string, typically the current path. 这是普通的提示字符串,通常是当前路径。

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. 最后,如果普通提示字符串很短并且没有完全覆盖未上色的[computername]:标记,我们需要通过添加虚拟空格字符进行一些最终的清理。 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}"
}

I use Posh-Git to accomplish this. 我使用Posh-Git完成此操作。 See their Prompt Customization . 请参阅其提示自定义 I noticed that some of the docs are a bit out of date, if you just type $GitPromptSettings in PowerShell you will see all the properties available. 我注意到某些文档有些过时,如果您仅在PowerShell中键入$GitPromptSettings ,您将看到所有可用属性。 Using Posh-Git has the added bonus of seeing Git stats on the prompt. 使用Posh-Git具有在提示上查看Git统计信息的额外好处。

The command I use to set the machine name is ... 我用来设置机器名称的命令是...

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

Here is the color setting 这是颜色设置

$GitPromptSettings.DefaultPromptPath.ForegroundColor = 'Orange' $ GitPromptSettings.DefaultPromptPath.ForegroundColor ='橙色'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM