Advertisement
guyk

Get-AzureDevOpsTokenFromGit

Oct 2nd, 2022
1,470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-AzureDevOpsTokenFromGit
  2. {
  3.     Param
  4.     (
  5.         [Parameter(Mandatory = $true)]
  6.         [string]
  7.         $ProjectPath,
  8.        
  9.         [Parameter()]
  10.         [string]
  11.         $GitTokenHelperPath = "${env:ProgramFiles}/Git/mingw64/bin/git-credential-manager-core.exe"
  12.     )
  13.  
  14.     if (-not (Test-Path -Path $GitTokenHelperPath))
  15.     {
  16.         throw "No git credential helper found at '$GetTokenHelperPath'"
  17.     }
  18.  
  19.     $helperParams =
  20. @"
  21. protocol=https
  22. host=dev.azure.com
  23. path=$ProjectPath
  24. "@
  25.  
  26.     $tokenData = $helperParams | & $GitTokenHelperPath get
  27.     $token = ($tokenData | Select-String -Pattern "^password=.+" | Select-Object -First 1) -split '=' `
  28.         | Select-Object -Last 1
  29.     if (-not $token)
  30.     {
  31.         throw "No password in git credential helper output"
  32.     }
  33.  
  34.     Write-Output $token
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement