├── UsingPoshGist.gif ├── Test-Gist.ps1 ├── Remove-Gist.ps1 ├── Get-GistAuthHeader.ps1 ├── .gitattributes ├── Get-GistContent.ps1 ├── Get-Gist.ps1 ├── PoShGist.psm1 ├── README.md ├── Send-Gist.ps1 └── .gitignore /UsingPoshGist.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfinke/Posh-Gist/HEAD/UsingPoshGist.gif -------------------------------------------------------------------------------- /Test-Gist.ps1: -------------------------------------------------------------------------------- 1 | function Test-Gist { 2 | param( 3 | [Parameter(Mandatory)] 4 | [string]$FileName 5 | ) 6 | 7 | Get-GistAuthHeader | Out-Null 8 | (Get-Gist $Global:cred.UserName $FileName) -ne $null 9 | } -------------------------------------------------------------------------------- /Remove-Gist.ps1: -------------------------------------------------------------------------------- 1 | function Remove-Gist { 2 | param( 3 | [Parameter(Mandatory, ValueFromPipelineByPropertyName)] 4 | [string]$GistID 5 | ) 6 | 7 | Begin { $Header = Get-GistAuthHeader } 8 | Process { Invoke-RestMethod -Method 'Delete' -Uri "https://api.github.com/gists/$($GistID)" -Headers $Header } 9 | } -------------------------------------------------------------------------------- /Get-GistAuthHeader.ps1: -------------------------------------------------------------------------------- 1 | function Get-GistAuthHeader { 2 | 3 | if(!$Global:GitHubCred) { $Global:GitHubCred = Get-Credential ''} 4 | 5 | $authInfo = "{0}:{1}" -f $Global:GitHubCred.UserName, $Global:GitHubCred.GetNetworkCredential().Password 6 | $authInfo = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($authInfo)) 7 | 8 | @{ 9 | 'Authorization' = 'Basic ' + $authInfo 10 | 'Content-Type' = 'application/json' 11 | } 12 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /Get-GistContent.ps1: -------------------------------------------------------------------------------- 1 | function Get-GistContent { 2 | param( 3 | [Parameter(ValueFromPipelineByPropertyName)] 4 | [string]$RawUrl, 5 | 6 | [Parameter()] 7 | [switch]$SendToISE 8 | ) 9 | 10 | Begin { $Header = Get-GistAuthHeader } 11 | 12 | Process { 13 | $gistContent = Invoke-RestMethod -Uri $RawUrl -Headers $Header 14 | 15 | if($SendToISE) { 16 | $newISETab = $psISE.CurrentPowerShellTab.Files.Add() 17 | $newISETab.Editor.Text = $gistContent 18 | $newISETab.Editor.SetCaretPosition(1,1) 19 | } 20 | 21 | else { 22 | Write-Output $gistContent 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /Get-Gist.ps1: -------------------------------------------------------------------------------- 1 | function Get-Gist { 2 | 3 | param( 4 | [Parameter(Mandatory)] 5 | [string]$User, 6 | [string]$FileName 7 | ) 8 | 9 | $(ForEach($gist in (Invoke-RestMethod -Headers (Get-GistAuthHeader) -Uri "https://api.github.com/users/$($User)/gists")) { 10 | 11 | $GetFileName = {($gist.files| Get-Member -MemberType NoteProperty).Name} 12 | [PSCustomObject]@{ 13 | FileName = &$GetFileName 14 | Url = $gist.url 15 | RawUrl = ($gist.files).(&$GetFileName).raw_url 16 | GistID = Split-Path -Leaf $gist.url 17 | } 18 | }) | Where-Object {$_.FileName -match $FileName} 19 | } -------------------------------------------------------------------------------- /PoShGist.psm1: -------------------------------------------------------------------------------- 1 | . (Join-Path $PSScriptRoot Get-Gist.ps1) 2 | . (Join-Path $PSScriptRoot Get-GistAuthHeader.ps1) 3 | . (Join-Path $PSScriptRoot Get-GistContent.ps1) 4 | . (Join-Path $PSScriptRoot Send-Gist.ps1) 5 | . (Join-Path $PSScriptRoot Test-Gist.ps1) 6 | 7 | function Add-MenuItem { 8 | param([string]$DisplayName, $SB, $ShortCut) 9 | 10 | $menu=$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus | Where {$_.DisplayName -Match $DisplayName} 11 | 12 | if($menu) { 13 | [void]$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Remove($menu) 14 | } 15 | 16 | [void]$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add($DisplayName, $SB, $ShortCut) 17 | } 18 | 19 | Add-MenuItem '_Send Gist' { Send-Gist } 'CTRL+Shift+S' -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Posh-Gist 2 | = 3 | Share your PowerShell scripts easily with [GitHub Gists](https://gist.github.com/) and Send-Gist. 4 | 5 | After doing an `Import-Module PoshGist` ***Ctrl+Shift+S*** is wired up in PowerShell ISE. Load up a script or write one from scratch, press the shortcut keys and you've created a gist. 6 | **NOTE.** If a selection is made, only the selection will be used for the new gist. 7 | 8 | ![Image](https://raw.github.com/dfinke/Posh-Gist/master/UsingPoshGist.gif) 9 | 10 | You need a [GitHub](https://github.com/join) account to post a gist, this does not support anonymous posts. 11 | 12 | ToDo 13 | - 14 | * ~~Add Updating an existing gist~~ 15 | * ~~Add Deleting an existing gist~~ 16 | * ~~Expand capabilities to send files on disk~~ 17 | * ~~Load an existing gist into ISE~~ -------------------------------------------------------------------------------- /Send-Gist.ps1: -------------------------------------------------------------------------------- 1 | function Send-Gist { 2 | param([string]$Path, [string]$Description) 3 | 4 | if($Path) { 5 | if (Test-Path -Path $Path) { 6 | $fileName = Split-Path -Path $Path -Leaf 7 | $contents = Get-Content -Path $Path -Raw 8 | } 9 | 10 | else { 11 | Write-Warning "$($Path) not found" 12 | break 13 | } 14 | } 15 | 16 | else { 17 | if($psISE) { 18 | $fileName = Split-Path -Leaf $psISE.CurrentFile.FullPath 19 | 20 | if($psISE.CurrentFile.Editor.SelectedText) { 21 | $contents = $psISE.CurrentFile.Editor.SelectedText 22 | } 23 | 24 | else { 25 | $contents = $psISE.CurrentFile.Editor.Text 26 | } 27 | } 28 | 29 | else { 30 | Write-Warning 'Using this function without the Path parameter only works in PowerShell ISE' 31 | break 32 | } 33 | } 34 | 35 | if($Description) { 36 | $gistDescription = $Description 37 | } 38 | 39 | else { 40 | $gistDescription = "Description for $($fileName)" 41 | } 42 | 43 | $gist = @{ 44 | 'description' = $gistDescription 45 | 'public' = $true 46 | 'files' = @{ 47 | "$($fileName)" = @{ 48 | 'content' = "$($contents)" 49 | } 50 | } 51 | } 52 | 53 | $Header = Get-GistAuthHeader 54 | 55 | $BaseUri = $Uri = 'https://api.github.com/gists' 56 | $Method = 'POST' 57 | 58 | $targetGist = Get-Gist $Global:GitHubCred.UserName $fileName 59 | if($targetGist) { 60 | 61 | $r = [System.Windows.MessageBox]::Show('Gist already exists. Do you want to overwrite?', 'Confirmation', 'YesNo', 'Question') 62 | 63 | if($r -eq 'no') {return} 64 | 65 | $Uri = $BaseUri + "/$($targetGist.GistID)" 66 | $Method = 'Patch' 67 | } 68 | 69 | $resp = Invoke-RestMethod -Uri $Uri -Method $Method -Headers $Header -Body ($gist | ConvertTo-Json) 70 | 71 | Start-Process $resp.'html_url' 72 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | --------------------------------------------------------------------------------