-
Notifications
You must be signed in to change notification settings - Fork 3
/
ssh-agent.ps1
82 lines (75 loc) · 2.96 KB
/
ssh-agent.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
############################################################################
#
# PowerShell wrapper to configure ssh-agent and environment
# Chris J, June 2018
#
# https://github.com/rangercej/ssh-agent-powershell
#
############################################################################
#------------------------------------------------------------------------------
function Start-SshAgent
{
$running = Get-SshAgent
if ($running -ne $null) {
write-warning "ssh-agent is running at pid = $($running.id)"
return
}
$shout = & "$env:ProgramFiles\Git\usr\bin\ssh-agent.exe" -c
$shout | foreach-object {
$parts = $_ -split " "
if ($parts[0] -ieq "setenv") {
$val = $parts[2] -replace ";$",""
# This, frustatingly, can be slow. See https://superuser.com/questions/565771/setting-user-environment-variables-is-very-slow
# for detailed info.
[Environment]::SetEnvironmentVariable($parts[1], $val, "User")
[Environment]::SetEnvironmentVariable($parts[1], $val, "Process")
} elseif ($parts[0] -ieq "echo") {
$val = $parts[1..$($parts.count)] -join " "
write-host $val
} else {
write-warning "Unknown command: $_"
}
}
}
#------------------------------------------------------------------------------
function Get-SshAgent
{
$found = $false
# ssh-agent shipped with git now returns a different PID to the actual windows PID. So
# we need to do a couple of contortions to make sure that any ssh-agent we find is
# owned by the running user.
if ($env:SSH_AGENT_PID -ne $null) {
$proc = Get-Process -name ssh-agent -ea SilentlyContinue
foreach ($process in $proc) {
$id = $process.Id
$owner = (get-wmiobject win32_process -filter "ProcessId = $id").GetOwner()
if ($owner.Domain -eq $env:UserDomain -and $owner.User -eq $env:UserName) {
$process
$found = $true
}
}
}
if (-not $found) {
# This, frustatingly, can be slow. See https://superuser.com/questions/565771/setting-user-environment-variables-is-very-slow
# for detailed info.
[Environment]::SetEnvironmentVariable("SSH_AGENT_PID", $null, "User")
[Environment]::SetEnvironmentVariable("SSH_AGENT_PID", $null, "Process")
[Environment]::SetEnvironmentVariable("SSH_AUTH_SOCK", $null, "User")
[Environment]::SetEnvironmentVariable("SSH_AUTH_SOCK", $null, "Process")
$null
}
}
#------------------------------------------------------------------------------
function Stop-SshAgent
{
$agent = Get-SshAgent
if ($agent -ne $null) {
stop-process $agent
# This, frustatingly, can be slow. See https://superuser.com/questions/565771/setting-user-environment-variables-is-very-slow
# for detailed info.
[Environment]::SetEnvironmentVariable("SSH_AGENT_PID", $null, "User")
[Environment]::SetEnvironmentVariable("SSH_AGENT_PID", $null, "Process")
[Environment]::SetEnvironmentVariable("SSH_AUTH_SOCK", $null, "User")
[Environment]::SetEnvironmentVariable("SSH_AUTH_SOCK", $null, "Process")
}
}