Advertisement
Harshmage

Out-Pastebin

Mar 17th, 2016
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.     Authenticates to PasteBin.com, and uploads text data directly to the website
  4. .DESCRIPTION
  5.     Uses the PasteBin API to take content from a file and create a new paste from it, including expiration, format, visibility, and title.
  6. .PARAMETER InputObject
  7.     Content to paste to Pastebin
  8. .PARAMETER Visibility
  9.     Public, Private, or Unlisted visibility for the new paste
  10. .PARAMETER Format
  11.     The format of the paste for syntax highlighting
  12. .PARAMETER ExpiresIn
  13.     Never: N, 10 Minutes: 10M, 1 Hour: 1H, 1 Day: 1D, 1 Week: 1W, 2 Weeks: 2W, 1 Month: 1M
  14. .PARAMETER PasteTitle
  15.     Title text for the paste
  16. .PARAMETER OpenInBrowser
  17.     Open the paste URL in the default browser
  18. .EXAMPLE
  19.     Out-Pastebin  -InputObject $(Get-Content "somefile.txt") -PasteTitle "My Pastebin Note" -ExpiresIn 10M -Visibility Unlisted -OpenInBrowser
  20. .NOTES
  21.     Original code is located here: http://poshcode.org/4982
  22.     Now requires Powershell 3.0+ (Invoke-WebRequest)
  23.     Added authentication snipets from here: http://www.altitudeintegration.com/PowerShellPasteBinLog.aspx
  24.     Fixed the OpenInBrowser switch to open with the default browser and clipboard option
  25.     I think parts of this are outdated now with newer versions of Powershell, but I'll update those in time.
  26. #>
  27.  
  28.  
  29. $PastebinDeveloperKey = ''
  30. $PastebinPasteURI = 'https://pastebin.com/api/api_post.php'
  31. $PastebinLoginUri = "https://pastebin.com/api/api_login.php"
  32. $PastebinUsername = ""
  33. $PastebinPassword = ""
  34.  
  35. $Authenticate = "api_dev_key=$PastebinDeveloperKey&api_user_name=$PastebinUsername&api_user_password=$PastebinPassword"
  36.  
  37. Function Script:EncodeForPost ( [Hashtable]$KeyValues )
  38. {
  39.     @(  
  40.         ForEach ( $KV in $KeyValues.GetEnumerator() )
  41.         {
  42.             "{0}={1}" -f @(
  43.             $KV.Key, $KV.Value |
  44.             #ForEach-Object { $_ -replace ' ', '+' } | # Pastebin's server doesn't correctly decode these, so don't bother.
  45.             ForEach-Object { [System.Web.HttpUtility]::UrlEncode( $_, [System.Text.Encoding]::UTF8 ) }
  46.             )
  47.         }
  48.     ) -join '&'
  49. }
  50.  
  51. Function Out-Pastebin
  52. {
  53.     [CmdletBinding()]
  54.    
  55.     Param
  56.     (
  57.         [Parameter(Mandatory=$True, ValueFromPipeline=$True)]
  58.         [AllowEmptyString()]
  59.         [String[]]
  60.         $InputObject,
  61.        
  62.         [ValidateSet('Public', 'Unlisted', 'Private')]
  63.         [String]
  64.         $Visibility = 'Unlisted',
  65.        
  66.         # Specifies paste language
  67.         [String]
  68.         $Format,
  69.        
  70.         [ValidateSet('N', '10M', '1H', '1D', '1W', '2W', '1M')]
  71.         [String]
  72.         $ExpiresIn = '1D',
  73.  
  74.         [Parameter(Mandatory=$True)]
  75.         [String]
  76.         $PasteTitle,
  77.        
  78.         [Switch]
  79.         $OpenInBrowser,
  80.        
  81.         [Switch]
  82.         $PassThru
  83.     )
  84.    
  85.     Begin
  86.     {
  87.         Add-Type -AssemblyName System.Web
  88.  
  89.         $script:s = Invoke-RestMethod -Uri $PastebinLoginUri -Body $Authenticate -Method Post
  90.        
  91.         $Post = [System.Net.HttpWebRequest]::Create( $PastebinPasteURI )
  92.         $Post.Method = "POST"
  93.         $Post.ContentType = "application/x-www-form-urlencoded"
  94.        
  95.         [String[]]$InputText = @()
  96.     }
  97.    
  98.     Process
  99.     {
  100.         ForEach ( $Line in $InputObject )
  101.         {
  102.             $InputText += $Line
  103.         }
  104.     }
  105.    
  106.     End
  107.     {
  108.         $Parameters = @{
  109.             api_user_key   = $script:s;
  110.             api_dev_key    = $PastebinDeveloperKey;
  111.             api_option     = 'paste';
  112.             api_paste_code  = $InputText -join "`r`n";
  113.             api_paste_name = $PasteTitle;
  114.            
  115.             api_paste_private = Switch($Visibility) { Public { '0' }; Unlisted { '1' }; Private { '2' }; };
  116.             api_paste_expire_date = $ExpiresIn.ToUpper();
  117.         }
  118.        
  119.         If ( $Format ) { $Parameters[ 'api_paste_format' ] = $Format.ToLower() }
  120.        
  121.         $Content = EncodeForPost $Parameters
  122.        
  123.         $Post.ContentLength = [System.Text.Encoding]::ASCII.GetByteCount( $Content )
  124.        
  125.         $WriteStream = New-Object System.IO.StreamWriter ( $Post.GetRequestStream( ), [System.Text.Encoding]::ASCII )
  126.         $WriteStream.Write( $Content )
  127.         $WriteStream.Close( )
  128.        
  129.         # Send request, get response
  130.         $Response = $Post.GetResponse( )
  131.         $ReadEncoding = [System.Text.Encoding]::GetEncoding( $Response.CharacterSet )
  132.         $ReadStream = New-Object System.IO.StreamReader ( $Response.GetResponseStream( ), $ReadEncoding )
  133.        
  134.         $Result = $ReadStream.ReadToEnd().TrimEnd( )
  135.        
  136.         $ReadStream.Close( )
  137.         $Response.Close( )
  138.        
  139.         If ( $Result.StartsWith( "http" ) ) {
  140.             If ( $OpenInBrowser ) {
  141.                 Try { Start-Process -FilePath $Result } Catch { }
  142.             } Else {
  143.                 $Result | clip.exe
  144.             }
  145.            
  146.             $Result | Write-Output
  147.         } Else {
  148.             Throw "Error when uploading to pastebin: {0} : {1}" -f $Result, $Response
  149.         }
  150.     }
  151. }
  152.  
  153. #$list = Get-Content "C:\Temp\Telemarketing_RoboCall_Weekly_Data_Deltas-20160222.csv"
  154. #Out-Pastebin -InputObject $list -PasteTitle "Deltas List" -ExpiresIn 10M -Visibility Unlisted -OpenInBrowser -OutVariable PastedURI
  155. #Write-Host "Pasted URI: $PastedURI"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement