Posts Tagged ‘Pastebin’

There are two requests that I get with overwhelming regularity for the PowerShell script repository: first, that I would add some sort of “browsing” functionality, and second, that it should have an RSS feed. Well, browsing may have to wait until a future iteration of the repository, but feeds are easy, because they’re basically just a hack-up of the “recent” items that are in the repository sidebar already, so I did it this evening.

Presenting …

PowerShellCentral.com/scripts/feed

Right now the number of items is limited to about ten, but if traffic picks up I can increase that easily — you can limit the count by passing the number of items you want to list like this: ?list=2.

You can also create a feed for specific search results (the feed will show items in relevance order, but the date is in the RSS so if you want to sort by that, you can). For instance, if you wanted to keep up to date on scripts that used SecureStrings, you would append your search terms like this: ?q=*securestring*. :)

As usual, my modifications to the PasteBin.com site are available on the repository site, and without much in the way of documentation. I will say this: in addition to creating the feed.php file, I moved some of the common translation functions into a translate.php file, and modified the dates that were being returned from db.mysql.class.php — it’s all under the Affero General Public License that Paul Dixon wrote PasteBin under in the first place.

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) ...

Search My Content