Skip to content

Instantly share code, notes, and snippets.

@chrisbrownie
Last active February 26, 2018 23:05
Show Gist options
  • Save chrisbrownie/55f8c24ce2d0241c86daa877d72efd5e to your computer and use it in GitHub Desktop.
Save chrisbrownie/55f8c24ce2d0241c86daa877d72efd5e to your computer and use it in GitHub Desktop.
Invoke-Gist is a PowerShell function that allows you to invoke a gist by ID or URL.
function Invoke-Gist {
Param(
[String]
$Identity,
[String]
$Arguments
)
$gistBase = "https://api.github.com/gists/"
if ( ($Identity.Length -eq 32) -and ($Identity -match '[A-Za-z0-9]*') ) {
# We got a gist ID
$gistUrl = $gistBase + $Identity
# Use TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$pageContents = Invoke-WebRequest -Uri $gistUrl
$gist = $pageContents.Content | ConvertFrom-Json
}
else {
# Not a gist ID, try the full URL (this is very bad, I am sorry)
$GistId = $Identity.TrimEnd("/").Split("/")[-1]
$gistUrl = $gistBase + $GistId
$pageContents = Invoke-WebRequest -Uri $gistUrl
$gist = $pageContents.Content | ConvertFrom-Json
}
Write-Verbose "Invoking gist from $gistUrl"
Write-Verbose "Gist by $($gist.owner.login)"
Write-Verbose "Created at $($gist.created_at), last modified at $($gist.updated_at)"
# Get the filenames and sort them alphabetically
$files = $gist.files | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | Select-Object -ExpandProperty Name | Sort-Object
foreach ($file in $files) {
$file = $gist.files.$file
Write-Verbose "Invoking file $($file.filename)"
if ($file.language -ne "PowerShell") {
Write-Warning "The file $($file.filename) is not marked as being of the PowerShell language."
}
Invoke-Expression -Command $file.Content
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment