Skip to content

Instantly share code, notes, and snippets.

@tunisiano187
Created May 22, 2020 13:12
Show Gist options
  • Save tunisiano187/7f2a346e9f57f2386667d1c133dce9e1 to your computer and use it in GitHub Desktop.
Save tunisiano187/7f2a346e9f57f2386667d1c133dce9e1 to your computer and use it in GitHub Desktop.
# Author: Miodrag Milic <[email protected]>
# Last Change: 10-Nov-2016.
<#
.SYNOPSIS
Upload files to Github gist platform.
.DESCRIPTION
Plugin uploads one or more local files to the gist with the given id
#>
param(
$Info,
# Gist id, leave empty to create a new gist
[string] $Id,
# Github ApiKey, create in Github profile -> Settings -> Personal access tokens -> Generate new token
# Make sure token has 'gist' scope set.
[string] $ApiKey,
# File paths to attach to gist
[string[]] $Path,
# Gist description
[string] $Description = "#powershell"
)
# Create gist
$gist = @{
description = $Description
public = $true
files = @{}
}
ls $Path | % {
$name = Split-Path $_ -Leaf
$content = gc $_ -Raw
$gist.files[$name] = @{content = "$content"}
}
# request
[System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor [System.Net.SecurityProtocolType]::Tls -bor [System.Net.SecurityProtocolType]::Ssl3
$uri = 'https://api.github.com/gists'
$params = @{
ContentType = 'application/json'
Method = if ($Id) { "PATCH" } else { "POST" }
Uri = if ($Id) { "$uri/$Id" } else { $uri }
Body = $gist | ConvertTo-Json
UseBasicparsing = $true
}
if ($ApiKey) {
$params.Headers = @{
Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($ApiKey))
}
}
$res = iwr @params
$id = (($res.Content | ConvertFrom-Json).history[0].url -split '/')[-2,-1] -join '/'
"https://gist.github.com/$id"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment