Mark Ku's Blog
首頁 關於我
Powershell 一鍵產生 git 所需的 ssh & 並將 ssh 上傳 github & git clone repo
PowerShell
Powershell 一鍵產生 git 所需的 ssh & 並將 ssh 上傳 github & git clone repo
Mark Ku
Mark Ku
May 31, 2023
1 min

Powershell 一鍵產生 git 所需的 ssh & 並將 ssh 上傳 github & git clone repo

前言

由於經常需要重裝開發環境,每次都手動生成並上傳這些 Token 到 GitHub,這一過程讓我感到繁瑣不便。因此,我撰寫了一個腳本來簡化這項工作。

首先得先申請 github person token

使用者資訊

$personalAccessToken = "your totken"
$yourRepo ="NextJS.git"
$githubUser = "MarkKu666"
$githubEmail = "[email protected]"
## 設定Git資料
$gitRepo = "[email protected]:$githubUser/$yourRepo"
$destinationFolder = "C:\Your\Destination\Folder"

產生SSH鍵

Write-Host "Generating SSH key..."
ssh-keygen -t rsa -b 4096 -C $githubEmail

顯示新生成的公鑰以供複製

Write-Host "Your new SSH key:"
$sshPublicKey = Get-Content "$env:USERPROFILE\.ssh\id_rsa.pub" | Out-String | ForEach-Object { $_ -replace "`n","" -replace "`r","" }

$sshPublicKey

更新GitHub SSH鍵

Write-Host "Updating GitHub SSH key..."
$headers = @{
    Authorization = "token $personalAccessToken"
    Accept = "application/vnd.github.v3+json"
}
$body = @{
    title = "PowerShell generated key"
    key = $sshPublicKey
} | ConvertTo-Json
Invoke-RestMethod -Uri "https://api.github.com/user/keys" -Method Post -Body $body -Headers $headers

檢查資料夾是否存在,如果不存在則創建

if(!(Test-Path -Path $destinationFolder )){
    Write-Host "Creating destination folder..."
    New-Item -ItemType directory -Path $destinationFolder
}

Clone Git 倉庫

Write-Host "Cloning git repository..."
git clone $gitRepo $destinationFolder

Tags

Mark Ku

Mark Ku

Software Developer

9年以上豐富網站開發經驗,開發過各種網站,電子商務、平台網站、直播系統、POS系統、SEO 優化、金流串接、AI 串接,Infra 出身,帶過幾次團隊,也加入過大團隊一起開發。

Expertise

前端(React)
後端(C#)
網路管理
DevOps
溝通
領導

Social Media

facebook github website

Related Posts

Reduce the risk of manual deployment using powershell and beyondcompare
Reduce the risk of manual deployment using powershell and beyondcompare
June 27, 2023
1 min

Quick Links

關於我

Social Media