Huddled Masses
You can do more than breathe for free...
Browse: Home / PowerShell Send-Paste script

PowerShell Send-Paste script

By Joel 'Jaykul' Bennett on 22-Apr-2008

In the #PowerShell IRC channel (on irc.freenode.net) we have a bot and pastebin (which is set up here on my site) ... if you paste something in the pastebin site, the bot posts a link to it in the IRC channel for you, so you don’t have to mess with copying the resulting URL and pasting it to the channel to ask for help.

I also run a version of pastebin that’s been modified for long term storage as the main PowerShell Script Repository (which is, among other things, the main repository for the PowerShell Community).

So, when someone showed me sprunge earlier today, along with a python script for pasting there … I couldn’t help but think that it would be nice to have that for our pastebin sites… so here it is, Send-Paste


## Send-Paste.ps1 (aka sprunge for Pastebin)
##############################################################################################################
## Uploads code to any pastebin.com based pastebin site and returns the url for you.
##############################################################################################################
## Usage:
##    get-content myscript.ps1 | Send-Paste "An example for you" "This is just to show how to do it"
##       would send the script with the specified title and description
##    ls *.ps1 | Send-Paste -Keep Forever
##       would flood the pastebin site with all your scripts, using filename as the title
##       and a generic description, and mark them for storing indefinitely
##    get-history -count 5 | % { $_.CommandLine } | Send-Paste
##       would paste the last 5 commands in your history!
##############################################################################################################
## History:
## v 2.0 - works with "pastebin" (including http://posh.jaykul.com/p/ and http://PowerShellCentral.com/Scripts/)
## v 1.0 - Worked with a special pastebin
##############################################################################################################
#function Send-Paste {
param(
   $Title,
   $Description="Automated paste from PowerShell console",
   $KeepFor="d",
   $Language="posh",
   $Author = $(Read-Host "Your name"),
   $url="http://posh.jaykul.com/p/"
)
   
BEGIN {
   $null = [Reflection.Assembly]::LoadWithPartialName("System.Web")
   [string]$data = $null;
   [string]$meta = $null;

   if($language) {
      $meta = "format=" + [System.Web.HttpUtility]::UrlEncode($language)
      # $url = $url + "?" +$lang
   } else {
      $meta = "format=text"
   }
 
   do {
      switch -regex ($KeepFor) {
         "^d.*" { $meta += "&expiry=d" }
         "^m.*" { $meta += "&expiry=m" }
         "^f.*" { $meta += "&expiry=f" }
         default {
            $KeepFor = Read-Host "Invalid value for 'KeepFor' parameter. Please specify 'day', 'month', or 'forever'"
         }
      }
   } until ( $meta -like "*&expiry*" )

   if($Description) {
      $meta += "&descrip=" + [System.Web.HttpUtility]::UrlEncode($Description)
   } else {
      $meta += "&descrip="
   }  
   $meta += "&poster=" + [System.Web.HttpUtility]::UrlEncode($Author)
   
   function PasteBin-Text ($meta, $title, $data) {
      $meta += "&paste=Send&posttitle=" + [System.Web.HttpUtility]::UrlEncode($Title)
      $data = $meta + "&code2=" + [System.Web.HttpUtility]::UrlEncode($data)
     
      # Write-Host $data -fore yellow
     
      $request = [System.Net.WebRequest]::Create($url)
      $request.ContentType = "application/x-www-form-urlencoded"
      $request.ContentLength = $data.Length
      $request.Method = "POST"

      $post = new-object IO.StreamWriter $request.GetRequestStream()
      $post.Write($data,0,$data.Length)
      $post.Flush()
      $post.Close()

      # $reader = new-object IO.StreamReader $request.GetResponse().GetResponseStream() ##,[Text.Encoding]::UTF8
      # write-output $reader.ReadToEnd()
      # $reader.Close()
      write-output $request.GetResponse().ResponseUri.AbsoluteUri
      $request.Abort()      
   }
}

PROCESS {
   switch($_) {
      {$_ -is [System.IO.FileInfo]} {
         $Title = $_.Name
         Write-Output $_.FullName
         Write-Output $(PasteBin-Text $meta $Title $([string]::join("`n",(Get-Content $_.FullName))))
      }
      {$_ -is [String]} {
         if(!$data -and !$Title){
            $Title = Read-Host "Give us a title for your post"
         }
         $data += "`n" + $_
      }
      ## Todo, handle folders?
      default {
         Write-Error "Unable to process $_"
      }
   }
}
END {
   if($data) {
      Write-Output $(PasteBin-Text $meta $Title $data)
   }
}
#}

Incidentally, the matching “Receive-Paste” script is just a matter wrapping a call to my wget for powershell in a script that lets you put just the ID in, and builds a url like http://powershellcentral.com/scripts/?dl=172 (notice the ?dl= part, that’s the important part) ...

Similar Posts:

  • Parenthesis in PowerShell
  • More Custom Attributes for PowerShell (Parameter Transformation)
  • Did you know PowerShell can use Selenium?
  • What Scope Am I In?
  • Working with multiple versions of PowerShell Modules

Posted in Huddled | Tagged Pastebin, PowerShell, Scripting, Upload

« Previous Next »

Lijit Search

Tags

.Net .Net 2008 Scripting Games Automation Bugs Design Development Funny Gadgets GeoShell GUI Huddled Masses Internet licensing Microsoft Modules My Software News Personal PInvoke Pipeline Politics PoshCode PoshConsole PowerBoots PowerShell PowerShell Functions PowerTips Rants Recommender Repository Scripting ShowUI Software Solutions Textile Tips User Group UserInterface WalkThrough WebHosting Windows 7 WordPress WPF Xml

About Huddled Masses

This is web site is dedicated to the musings of Joel Bennett (aka Jaykul) about technology, software, software development, the web, and the world.

Any resemblance of the views expressed and the views of my employer, my terminal, or the view out my window are purely coincidental. The resemblance between them and my own views is non-deterministic. The question of the existence of views in the absence of anyone to hold them is left as an exercise for the reader.

P.S.: I occasionally link to things I think are great. When I do, I occasionally find a "referral code" so I can make a little cash. I promise that I don't link to anything just because of that cash (I wouldn't cross the street for the amount of cash those links bring in, never mind write a whole blog post) ... but I do not promise that things I link to will stay great as time passes, nor that you will agree with me about their greatness!

Archives

  • April 2012
  • February 2012
  • January 2012
  • October 2011
  • August 2011
  • July 2011
  • June 2011
  • March 2011
  • February 2011
  • January 2011

Copyright © 2012 Joel Bennett.

Powered by WordPress and Hybrid.