QZLin

oh-my-posh导致的Win32-OpenSSH主机能用ssh连上但scp用不了

Powershell7可比cmd好用太多了,很自然的将Powershell配置成了DefaultShell,但突然有一天scp连不上去了,但ssh还能连。scp报C:\WINDOWS\System32\OpenSSH\scp.exe: Connection closed
最后定位到oh-my-posh

https://github.com/PowerShell/Win32-OpenSSH/issues/2402

解决方法:在自己的Profile里加入对环境的判断

function IsVirtualTerminalProcessingEnabled {
  $MethodDefinitions = @'
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
'@
  $Kernel32 = Add-Type -MemberDefinition $MethodDefinitions -Name 'Kernel32' -Namespace 'Win32' -PassThru
  $hConsoleHandle = $Kernel32::GetStdHandle(-11) # STD_OUTPUT_HANDLE
  $mode = 0
  $Kernel32::GetConsoleMode($hConsoleHandle, [ref]$mode) >$null
  if ($mode -band 0x0004) {
    # 0x0004 ENABLE_VIRTUAL_TERMINAL_PROCESSING
    return $true
  }
  return $false
}
$CanUsePredictionSource = (! [System.Console]::IsOutputRedirected) -and (IsVirtualTerminalProcessingEnabled)

如果并非$CanUsePredictionSource就不启动oh-my-posh

posted on 2025-10-16 21:14  QZLin  阅读(18)  评论(0)    收藏  举报

导航