Skip to content

Instantly share code, notes, and snippets.

@PrateekKumarSingh
Last active May 26, 2016 18:00
Show Gist options
  • Save PrateekKumarSingh/a35f14088d008a5458e1a210a12eeebc to your computer and use it in GitHub Desktop.
Save PrateekKumarSingh/a35f14088d008a5458e1a210a12eeebc to your computer and use it in GitHub Desktop.
Updated on 26-May-16 11:30:26
Function Get-GithubGist
{
Param(
[Parameter(Mandatory=$True, ValueFromPipelineByPropertyName = $True)] [String[]] $GithubUser,
[String[]] $FileName,
[switch] $openInISE
)
Begin
{
$output = @()
if(!$Global:GitHubCred){$Global:GitHubCred = Get-Credential ''}
$AuthString = "{0}:{1}" -f $Global:GitHubCred.UserName, $Global:GitHubCred.GetNetworkCredential().Password
$AuthString = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($AuthString))
$Header = @{
'Authorization' = 'Basic ' + $AuthString
'Content-Type' = 'application/json'
}
}
Process
{
Foreach($User in $GithubUser)
{
$Gists = Invoke-RestMethod -Headers $Header -Uri "https://api.github.com/users/$User/gists?per_page=100"
$Result = ForEach($Gist in $Gists)
{
$FileInfo = $Gist.files."$(($Gist.files |gm -MemberType NoteProperty).Name)"
#$GetFileName = {($gist.files| Get-Member -MemberType NoteProperty).Name}
[PSCustomObject]@{
GithubUser = $User
Language = $FileInfo.language
FileName = $FileInfo.Filename
Visibility = if($Gist.public){"Public"}else{"Secret"}
Url = $gist.url
RawUrl = ($gist.files).($FileInfo.filename).raw_url
GistID = Split-Path -Leaf $gist.url
}
}
$output += $Result | Where-Object {$_.FileName -like "*$FileName*"}
}
$output
If($openInISE)
{
$output | Foreach{
$OpenTab = $psISE.CurrentPowerShellTab.Files.Add()
$OpenTab.Editor.Text = Invoke-RestMethod -Uri $($_.rawurl).tostring() -Headers $Header
#$OpenTab.DisplayName = "[Github] $($_.filename)"
}
}
}
End
{
}
}
Get-GithubGist -GithubUser 'prateekkumarsingh' -FileName ""|ft -AutoSize #-openInISE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment