Skip to content

Instantly share code, notes, and snippets.

@carschrotter
Created August 18, 2024 15:06
Show Gist options
  • Save carschrotter/e3d8fa848af41b8fe21fb8a2cc0d364d to your computer and use it in GitHub Desktop.
Save carschrotter/e3d8fa848af41b8fe21fb8a2cc0d364d to your computer and use it in GitHub Desktop.
function ssh-copy-id($server) {
# Find all .pub keys in the user's .ssh directory
$publicKeys = Get-ChildItem "$env:USERPROFILE\.ssh" | Where-Object { $_.Name -match "\.pub$" }
# If public keys were found, copy them to the remote server
if ($publicKeys) {
$allPublicKeys = ""
$keyPaths = @() # Array for the file paths of the keys found
foreach ($key in $publicKeys) {
$allPublicKeys += Get-Content $key.FullName
$allPublicKeys += "`n" # Add a line break to separate the keys
$keyPaths += $key.FullName # Füge den Dateipfad dem Array hinzu
}
# Send all keys to the remote server
# see https://gist.github.com/elonmallin/2cc94c45ab57e2060498e855acefade0
$allPublicKeys | ssh $server "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
Write-Host "Folgende SSH-Schlüssel wurden zum Remote-Server kopiert:" -ForegroundColor Green
$keyPaths | ForEach-Object { Write-Host $_ -ForegroundColor Yellow }
} else {
Write-Host "Keine öffentlichen SSH-Schlüssel (.pub) im Verzeichnis gefunden."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment