Skip to content

Instantly share code, notes, and snippets.

@devhawk
Created October 4, 2018 23:18
Show Gist options
  • Select an option

  • Save devhawk/f9a108dea723958b33d20223f50002d3 to your computer and use it in GitHub Desktop.

Select an option

Save devhawk/f9a108dea723958b33d20223f50002d3 to your computer and use it in GitHub Desktop.
create-gist.ps1
[CmdletBinding()]
Param(
[Parameter(ValueFromPipelineByPropertyName)] $FullName,
[string] $description,
[switch] $public,
[switch] $launchBrowser
)
begin
{
if ( -not (test-path env:GITHUB_TOKEN))
{
throw "Missing GITHUB_TOKEN"
}
$files = @{}
}
process
{
$filename = split-path $FullName -Leaf
$content = get-content -path $FullName -Raw
$files.Add($filename, @{"content" = "$content"})
}
end
{
$body = ConvertTo-Json (@{"description" = $description; "public" = if ($public) {"true"} else {"false"} ; "files" = $files })
$headers = @{"Authorization" = "token $env:GITHUB_TOKEN"}
$resp = Invoke-RestMethod -Uri "https://api.github.com/gists" -Method Post -Body $body -ContentType 'application/json' -Headers $headers
$url = $resp.html_url
Write-Output "New Gist URL: $url"
if ($launchBrowser)
{
Start-Process $url
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment