From b9660fe61e616e6e4d445af81bc599b698a7df8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D0=BD=D1=81=D1=82=D0=B0=D0=BD=D1=82=D0=B8?= =?UTF-8?q?=D0=BD?= Date: Wed, 30 Nov 2016 16:43:05 +0600 Subject: [PATCH] Fix process of obtaining pageant PID. --- GitUtils.ps1 | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/GitUtils.ps1 b/GitUtils.ps1 index 29f0778ee..c073b8674 100644 --- a/GitUtils.ps1 +++ b/GitUtils.ps1 @@ -254,10 +254,31 @@ function Get-TempEnvPath($key){ # Retrieve the current SSH agent PID (or zero). Can be used to determine if there # is a running agent. -function Get-SshAgent() { +function Get-SshAgent(){ if ($env:GIT_SSH -imatch 'plink') { - $pageantPid = Get-Process | Where-Object { $_.Name -eq 'pageant' } | Select -ExpandProperty Id -First 1 - if ($null -ne $pageantPid) { return $pageantPid } + $sig=@' + using System; + using System.Runtime.InteropServices; + + public static class Win32Api + { + [System.Runtime.InteropServices.DllImportAttribute("User32.dll", EntryPoint = "GetWindowThreadProcessId")] + public static extern int GetWindowThreadProcessId([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, out int lpdwProcessId); + + [DllImport("User32.dll", CharSet = CharSet.Auto)] + public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); + } +'@ + Add-Type -TypeDefinition $sig + $HWND = [Win32Api]::FindWindow('pageant', 'pageant') + + $myPid = [IntPtr]::Zero + [void][Win32Api]::GetWindowThreadProcessId($HWND, [ref] $myPid); + $pageantPid = Get-Process | Where-Object { $_.Id -eq $myPid } + + if ($null -ne $pageantPid) { + return $pageantPid.Id + } } else { $agentPid = $Env:SSH_AGENT_PID if ($agentPid) { @@ -347,7 +368,9 @@ function Add-SshKey() { $keystring = "" $keyPath = Join-Path $Env:HOME ".ssh" $keys = Get-ChildItem $keyPath/"*.ppk" -ErrorAction SilentlyContinue | Select -ExpandProperty FullName - & $pageant $keys + if ($keys){ + & $pageant $keys + } } else { foreach ($value in $args) { & $pageant $value