How To access a GitHub Repo by providing Github Secret in authentication from Powershell?

115 Views Asked by At

I have a Database folder in my Github repository which contains SQL scripts. I can access the repo only using GitHub secrets. I want to download the scripts using Invoke-WebRequest Powershell cmdlet and run those scripts using Invoke-Sqlcmd cmdlet in my local db.

I have made the following script :

 Add-PSSnapin SqlServerCmdletSnapin100
 Add-PSSnapin SqlServerProviderSnapin100
 $GitRepository = ("https://github.com/XYZ/ABC/tree/ABCD/XYZXYZ/Database/")
 $GitManifestArray = @("Database.sql","XYZ.sql","ABC.sql","SP.sql","RSP.sql","FN.sql","E.sql")
 $SQLInstances = ".\SQLEXPRESS"
 $DefaultDatabase = "MYDEFDATABASE"
 ForEach ($SQLInstance in $SQLInstances.Split(","))
  {
      echo "Running Scripts on " $SQLInstance
      #foreach ($File In $Manifest)
      ForEach ($file in $GitManifestArray)
      {
          $ScriptURL = ($GitRepository+$file.Replace("/blob/","/"))
         echo "Running " $ScriptURL
         $Headers = @{
            accept = "application/vnd.github.v3.raw"
            authorization = '${{secrets.ABC_XYZ_ABC_TOKEN}}'
            }
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls,  [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Ssl3

          $ScriptFromGit = Invoke-WebRequest -Uri $ScriptURL  -Headers $Headers -UseBasicParsing
          Invoke-Sqlcmd -ServerInstance $SQLInstance  -Query $ScriptFromGit.Content
      }
  }

but I'm getting the following error :

  *Invoke-WebRequest : The request was aborted: The connection was closed unexpectedly.
  At line:18 char:27
  +          $ScriptFromGit = Invoke-WebRequest -Uri $ScriptURL  -Headers $Headers - ...*

I tried to put authorization as :

authorization = 'token ${{secrets.ABC_XYZ_ABC_TOKEN}}'

but still I'm getting the same error.

0

There are 0 best solutions below