らしいです。
幸いにも、私はあなたのために仕事ができるハックの回避策を持っています!
色を得るために、私たちはWrite-Host
を使用することができます。 prompt
関数から出力された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は完全修飾名を出力します。このプロンプトコードはすべて、リモートボックスで実行されています。リモートボックスでは、選択したオプションを知る方法がありません。 – latkin
これを修正するための方法が見つかりました。私のヘルパー接続関数は、リモートセッションに接続するために使用されたホストを単に渡すことができ、プロンプト関数(定義されている場合)で使用できます。 'Invoke-Command-Session $ session -ScriptBlock {$ connectedHost = $ using:remoteHost} ' –