Skip to content

Instantly share code, notes, and snippets.

@PrateekKumarSingh
Last active May 26, 2016 17:53
Show Gist options
  • Save PrateekKumarSingh/94cb827bfe8ac510a6757e2484f7faa8 to your computer and use it in GitHub Desktop.
Save PrateekKumarSingh/94cb827bfe8ac510a6757e2484f7faa8 to your computer and use it in GitHub Desktop.
Updated on 26-May-16 11:23:09
Function Update-GithubGist
{
Param(
[Parameter(Mandatory=$true)] [String] $Path,
[Parameter(Mandatory=$true)] [String] $GistName,
[Switch] $SecretGist = $False
)
if (Test-Path -Path $Path)
{
$fileName = Split-Path -Path $Path -Leaf
$contents = Get-Content -Path $Path -Raw
}
$Description = "Updated on $((Get-Date).tostring("dd\-MMM\-yy hh\:mm\:ss"))"
$Gist = @{
'description' = $Description
'public' = !$SecretGist
'files' = @{
"$($fileName)" = @{
'content' = "$($contents)"
}
}
}
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'
}
$URL = $APIURL = "https://api.github.com/gists"
$Method = 'POST'
$TargetGithubGist = Get-GithubGist -GithubUser $Global:GitHubCred.UserName -FileName $GistName
if($TargetGithubGist) {
$URL = $APIURL + "/$($TargetGithubGist.GistID)"
$Method = 'Patch'
}
Invoke-RestMethod -Uri $URL -Method $Method -Headers $Header -Body ($gist | ConvertTo-Json)
}
Update-GithubGist -Path "D:\Powershell\Scripts\update-githubgist.ps1" -GistName "Update-GithubGist" -SecretGist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment