Last active
October 17, 2024 13:45
-
-
Save darrenjrobinson/7a313677016019b0814952e42c353310 to your computer and use it in GitHub Desktop.
Retrieve all my Github Gists via search using PowerShell. Associated blog post here https://blog.darrenjrobinson.com/searching-and-retrieving-your-github-gists-using-powershell/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Authenticate | |
$clientID = 'myGitHubUsername' | |
# GitHub API Client Secret | |
$clientSecret = '21c22a9f0ca888373a3077614d0abcdefghijklmnop' | |
# Basic Auth | |
$Bytes = [System.Text.Encoding]::utf8.GetBytes("$($clientID):$($clientSecret)") | |
$encodedAuth = [Convert]::ToBase64String($Bytes) | |
# Search based on Description | |
$search = "Import Script" | |
$Headers = @{Authorization = "Basic $($encodedAuth)"; Accept = 'application/vnd.github.v3+json'} | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
$githubURI = "https://api.github.com/user" | |
$githubBaseURI = "https://api.github.com" | |
$auth = Invoke-RestMethod -Method Get -Uri $githubURI -Headers $Headers -SessionVariable GITHUB | |
if ($auth) { | |
# Get my GISTS | |
$myGists = Invoke-RestMethod -method Get -Uri "$($githubBaseURI)/users/$($clientID)/gists" -Headers $Headers -WebSession $GITHUB | |
$privateGists = $myGists | Select-Object | Where-Object {$_.public -eq $false} | |
$privateGists.url | |
$script = $privateGists | Select-Object | where-Object {$_.description -eq $search} | |
if ($script){ | |
foreach ($file in $script.files){ | |
$filename = $file | Get-Member | Where-Object {$_.memberType -eq "NoteProperty"} | select-object Name | |
# Get File | |
$fileProps = $file."$($filename.Name)" | |
$rawURL = $fileProps.raw_url | |
$fileraw = Invoke-RestMethod -Method Get -Uri $rawURL -WebSession $GITHUB | |
$fileraw | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment