Skip to content

Instantly share code, notes, and snippets.

@jdmallen
Last active July 5, 2024 12:02
Show Gist options
  • Select an option

  • Save jdmallen/2ce27d569b39a9b0be6152daf9a87009 to your computer and use it in GitHub Desktop.

Select an option

Save jdmallen/2ce27d569b39a9b0be6152daf9a87009 to your computer and use it in GitHub Desktop.
My personal PowerShell profile-- think of it as a `.bashrc` file for PS-- that loads a common SSH agent for git
# Gives me git info in the shell when in git directories
Import-Module posh-git
# Simple function to tell me if I'm in an admin window or not
Function Test-Elevated
{
$wid = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp = New-Object System.Security.Principal.WindowsPrincipal($wid)
$adm = [System.Security.Principal.WindowsBuiltInRole]::Administrator
$prp.IsInRole($adm)
}
If (Test-Elevated)
{
echo "Admin Mode"
}
Else
{
echo "User Mode"
}
# Starts an SSH agent for use with git.
# I designed this to share the same agent between PowerShell and Git Bash.
# Make sure you set $Env:GIT_SSH at the User or System level to point to
# whichever ssh-agent.exe PowerShell is using, or else this won't work.
Function Start-SSHAgent
{
& ssh-agent
# Output agent's PID to file
(Get-Process | Where-Object {
$_.ProcessName -eq "ssh-agent"
} | Select Id)[0].Id > $Env:USER_PROFILE\.ssh\pid
& ssh-add "$env:USER_PROFILE\.ssh\id_rsa.pri"
}
# Look for PID file
if (Test-Path $Env:USER_PROFILE\.ssh\pid) {
# Start new agent only if existing process's ID does not match PID in file
$filePid = Get-Content $Env:USER_PROFILE\.ssh\pid -Raw
$runningPid = (Get-Process | Where-Object {
$_.ProcessName -eq "ssh-agent" -and $_.Id -eq $filePid
}).Id
if ($runningPid -ne $filePid)
{
Write-Host "No SSH agent found. Starting new SSH agent."
Start-SSHAgent
}
else
{
Write-Host "SSH agent with PID $runningPid found."
}
}
else
{
# if no environment file at all, start new one
Write-Host "No SSH agent found. Starting new SSH agent."
Start-SSHAgent
}
cd D:\_projects
clear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment