PowerShell/scripts/new-ssh-key.ps1

35 lines
1020 B
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02:00
<#
2024-09-04 10:14:27 +02:00
.SYNOPSIS
Creates a new SSH key
.DESCRIPTION
This PowerShell script creates a new SSH key for the user.
.EXAMPLE
PS> ./new-ssh-key.ps1
2024-10-01 13:37:53 +02:00
New SSH key of Ed25519 type saved to ~/.ssh - your public key is:
2024-09-04 18:06:25 +02:00
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILb8s5zU9YDApGQ82H45fMKVPMr5cw9fzh3PEBjZZ+Rm markus@PI
2024-09-04 10:14:27 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
try {
& ssh-keygen
if ($lastExitCode -ne "0") { throw "ssh-keygen failed" }
if (Test-Path "~/.ssh/id_ed25519.pub") {
$publicKey = Get-Content "~/.ssh/id_ed25519.pub"
2024-09-04 18:06:25 +02:00
$enc = "Ed25519"
2024-09-04 10:14:27 +02:00
} elseif (Test-Path "~/.ssh/id_rsa.pub") {
$publicKey = Get-Content "~/.ssh/id_rsa.pub"
2024-09-04 18:06:25 +02:00
$enc = "RSA"
2024-09-04 10:14:27 +02:00
} else {
throw "No public key found."
}
2024-10-01 13:37:53 +02:00
"✅ New SSH key of $enc type saved to ~/.ssh - your public key is:"
2024-09-04 18:06:25 +02:00
" $publicKey"
2024-09-04 10:14:27 +02:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}