Send content to PasteBin using PowerShell

by ravikanthchaganti on June 14, 2010
141 Views

PasteBin.com is a good way to share scripts with others. Over there, you can just paste scripts written in almost any language and select the language for syntax highlighting. The interesting part here is that they provide an API to do that. Like anything else that got an API, this one can also be scripted using PowerShell. So, here is a simple module that lets you paste content to PasteBin using PowerShell.  

PasteBin (Downloaded 36 times)
      

Function Test-Proxy {
 $webclient = New-Object System.Net.WebClient
 return $webclient.Proxy.IsBypassed("http://www.microsoft.com")
}            

function New-PasteBin {
 [CmdletBinding()]
 param (
  [Parameter(Mandatory=$True)]
     [String]$pasteCode,            

     [Parameter(Mandatory=$False)]
  [String]$pasteName,            

  [Parameter(Mandatory=$false)]
  [String]$pasteEmail,            

  [Parameter(Mandatory=$false)]
  [String]$pasteSubDomain,            

  [Parameter(Mandatory=$false)]
     [Switch]$pastePrivate,            

  [Parameter(Mandatory=$False)]
  [ValidateSet("N", "10M", "1H", "1D", "1M")]
     [String]$pasteExpireDate,            

  [Parameter(Mandatory=$false)]
     [String]$pasteFormat
 )            

 $uri = 'http://pastebin.com/api_public.php'
 $request = [System.Net.WebRequest]::Create($uri)
 $request.ContentType = "application/x-www-form-urlencoded"
 $request.Method = "POST"
    [System.Net.ServicePointManager]::Expect100Continue = $false            

 if (!(Test-Proxy)) {
  if (!$Script:proxyCred) {
   $Script:proxyCred = Get-Credential
  }
  $request.Proxy.Credentials = $Script:proxyCred
 }            

 #start constructing parameters
 $parameters = "paste_code=$pasteCode"
 if ($pasteName -ne "") {
  $parameters += "&paste_name=$pasteName"
 }            

 if ($pasteEmail -ne "") {
  $parameters += "&paste_email=$pasteEmail"
 }            

 if ($pasteSubDomain -ne "") {
  $parameters += "&paste_subdomain=$pasteSubDomain"
 }            

 if ($pastePrivate) {
  $parameters += "&paste_private=1"
 }            

 if ($pasteExpireDate) {
  $parameters += "&paste_expire_date=$pasteExpireDate"
 }            

 if ($pasteFormat) {
  $parameters += "&paste_format=$pasteFormat"
 }            

 try {
  $bytes = [System.Text.Encoding]::ASCII.GetBytes($parameters)
  $request.ContentLength = $bytes.Length
  $stream = $request.GetRequestStream()
  $stream.Write($bytes,0,$bytes.Length)
  $stream.Close()            

  $resp = $request.GetResponse()            

  $sr = [System.IO.StreamReader] $resp.GetResponseStream()
  $returnUri = $sr.ReadToEnd().Trim()
 }
 catch {
  Write-Host "Error pasting the content"
  return $false
 }
 return $returnUri
}

The usage of this is quite easy. There is really only one mandatory paramter, pasteCode. This parameter is used to specify the script code that you need to upload to PasteBin. Other parameters can be quite helpful. For example,   

  • You can use pasteFormat to specify the language of the script so that PasteBin can enable syntax highlighting 
  • pasteSubDomain to specify a subdomain name to upload your scripts to. For example, http://ravikanth.pastebin.com has all the script uploaded by me
  • pasteEmail to send you an email with the PasteBin link
  • pasteExpireDate to set how long the pose will be kept online and pastePrivate switch parameter to hide it from public

Once you run this script and if everything is fine, you will receive the URL to your script in the output. Of course, there can be many options here as well. For example, instead of just showin the output, we can just pipe it to start-process to open it in a web browser or even send that URL in a tweet. This is all trivial and can be done in few more lines of code. But what is coming in the next post isn’t a trivial one. Here is the sneak peak..! 

PasteBin GUI

PasteBin WPF addon for PowerShell ISE and PowerGUI 2.1 

 

Related Posts

{ 2 trackbacks }

Leave a Comment

Previous post:

Next post: