# 手動輸入
$u = Read-Host '請輸入帳號 (Username)'
$s = Read-Host '請貼上新的 Access Token / 密碼(畫面不會顯示)' -AsSecureString
$repoUrl = Read-Host '請輸入 Repo URL (例如 https://git.test.com/test/test.git)'
# 自動輸入
$u = 'Test_on_TestVM'
$s = ConvertTo-SecureString "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -AsPlainText -Force
$repoUrl = 'https://git.test.com/test/test.git'
# 解析 URL 以取得 protocol 與 host
$uri = [System.Uri]$repoUrl
$protocol = $uri.Scheme
$hostName = $uri.Host
# 還原 SecureString 為明文
$t = [Runtime.InteropServices.Marshal]::PtrToStringBSTR([Runtime.InteropServices.Marshal]::SecureStringToBSTR($s))
# 清除舊憑證並寫入新憑證
"protocol=$protocol`nhost=$hostName`nusername=$u`n`n" | git credential reject
"protocol=$protocol`nhost=$hostName`nusername=$u`npassword=$t`n`n" | git credential approve
# 清除記憶體中的敏感變數
Remove-Variable t, s
# 組裝帶入帳號的測試 URL(若原 URL 已含帳號則替換)並驗證連線
$builder = [System.UriBuilder]$uri
$builder.UserName = $u
$testUrl = $builder.Uri.AbsoluteUri
git ls-remote $testUrl refs/heads/main
沒有留言:
張貼留言