+ #
+ # Two of those are on the box, and the one beside rsync.exe is preferred.
+ # It is the same client from the same source, with the same ~/.ssh, agent
+ # and known_hosts, built with a pump on its stdin: the in-box one reads
+ # stdin 3KB at a time, which holds anything git PUSHES to ~17MB/s however
+ # fast the link is. It only exists once the elevated half has run, so the
+ # in-box client stays the fallback - and on a first provisioning run from
+ # setup-windows.bat it is the elevated half that runs first, so the fast one
+ # is normally already there.
+ $sshCandidates = @(
+ (Join-Path $RsyncDir 'ssh.exe'),
+ (Join-Path $env:WINDIR 'System32\OpenSSH\ssh.exe')
+ )
+ $winSsh = $null
+ foreach ($cand in $sshCandidates) {
+ if (-not (Test-Path $cand)) { continue }
+ # Run it, rather than just believing the file is there: the build beside
+ # rsync.exe links against the libcrypto.dll the OpenSSH Client capability
+ # puts in System32, and without that capability it is a binary that does
+ # not start. Better to find that out here than on the next `git push`.
+ #
+ # EAP back to Continue for the call: ssh -V writes its version to
+ # STDERR, and with $ErrorActionPreference = 'Stop' a native command's
+ # stderr becomes a terminating RemoteException - so the working client
+ # would look like the broken one.
+ $prevEap = $ErrorActionPreference
+ $ErrorActionPreference = 'Continue'
+ $version = $null
+ # Clear the exit code first, explicitly at global scope. An exe that
+ # cannot start at all - the missing-libcrypto case - throws here without
+ # ever setting one, and the stale 0 from the last native command that DID
+ # run would otherwise read as success. $global: because a bare assignment
+ # would make a local copy that the native call then does not update.
+ $global:LASTEXITCODE = $null
+ try { $version = (& $cand -V 2>&1 | Select-Object -First 1) } catch { }
+ finally { $ErrorActionPreference = $prevEap }
+ if ($LASTEXITCODE -eq 0) { $winSsh = $cand; break }
+ $why = if ($null -eq $LASTEXITCODE) { 'it would not start' } else { "exit $LASTEXITCODE" }
+ Write-Warning "$cand did not run ($why)$(if ($version) { ": $version" })"
+ }
+ if (-not $winSsh) {
+ Write-Warning "No working Win32-OpenSSH client found (looked in $($sshCandidates -join ', ')). Add the 'OpenSSH Client' optional feature and re-run; until then git uses its own bundled ssh.exe, which cannot see keys held by the Windows ssh-agent service."