Skip to content

Instantly share code, notes, and snippets.

@bumbummen99
Last active February 22, 2023 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bumbummen99/539c2fbaed423c327206678a4906548d to your computer and use it in GitHub Desktop.
Save bumbummen99/539c2fbaed423c327206678a4906548d to your computer and use it in GitHub Desktop.
Script to enable ssh-agent and add existing key files in Windows 10 / 11 for use with VSCode DevContainer.
# SSH-Helper
#
# This PowerShell script is a small helper to:
# 1) Enable the ssh-agent service in Windows and start it
# 2) Run ssh-add for all private key files in the home directory
#
# Important: This script assumes your keys to be saved *NIX style under "%USERPROFILE%/.ssh"!
#
# Author: Patrick Henninger <privat@skyraptor.eu>
# License: GPLv3
# Check admin privilege and pop UAC dialog to elevate+
if(!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList "-File `"$($MyInvocation.MyCommand.Path)`" `"$($MyInvocation.MyCommand.UnboundArguments)`""
Exit
}
# Enable ssh-agent on Windows (requires privileges)
echo "Enabling ssh-agent servce and start it..."
Set-Service -Name ssh-agent -StartupType Automatic
Set-Service -Name ssh-agent -Status Running
# Import all files in .ssh folder except files with extension, config and known_hosts
$paths = Get-ChildItem -Path "$($env:USERPROFILE)\.ssh\*" -Exclude *.*,config,known_hosts
foreach ($path in $paths) {
echo "Adding $path to the ssh-agent... "
ssh-add $path
}
# Pause the execution so the user can see the informational outputs
PAUSE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment