In this post I will talk about a new script I wrote to save PowerShell ISE files to a versioned SharePoint document library as well as a side project I created on CodePlex to host this code and future releases.
Recently I began listening to the PowerScripting Podcast and I’m slowly catching up on old podcasts. On episodes 121 and 122 (most recent ones I’ve listened to as of this writing) Hal and Jonathan talked about an idea that piqued my interest immediately: using SharePoint as a script repository for PowerShell scripts. As I was in my car at the time I began to brainstorm ideas on how to accomplish this and what should be in scope for such a project. As soon as I reached my destination I began working on a proof of concept and subsequent list of additional tasks to flesh this out. Below you will find an adaptation of my C# code snippet to upload a file to a SharePoint document library (blogged about here) that I worked into a PowerShell script.
Add-Type -TypeDefinition @"
public class SharePointFileUploader
public static void UploadFile(string textToOutput, string uploadPath)
ASCIIEncoding encoder = new ASCIIEncoding();
byte bytesToOutput = encoder.GetBytes(textToOutput.ToString());
using (WebClient client = new WebClient())
client.Credentials = System.Net.CredentialCache.DefaultCredentials;
client.UploadData(uploadPath, "PUT", bytesToOutput);
$documentLibraryPath = Read-Host -Prompt "Enter URL of Document Library";
$documentLibraryPath = $documentLibraryPath.TrimEnd('/')
foreach($aFile in $psise.CurrentPowerShellTab.Files)
$textToOutput = $aFile.Editor.Text;
$uploadPath = "$documentLibraryPath/$($aFile.DisplayName.Replace('*', ''))";
What The Script Does
Note: This script is just a proof of concept that took 15 minutes to write and is not entirely fleshed out. With that said, this script declares a new type that uploads a string to a specified URL path. Next it loops through all open files in your PowerShell ISE and uploads the content of these files (even unnamed/unsaved files) to a SharePoint document library using our newly added type. The beauty is that if you have turned on versioning for your document library the file content will add a new version.
While it is nice to have a script that performs this upload manually, wouldn’t it be nice to have this functionality happen automatically similar to how Microsoft Office Word or Excel auto saves your work every X minutes. That idea has led me to draw up some additional features I would like to build in as my spare time allows. Here are a few of those planned features (some already under development or research begun):
- Create/select SharePoint document library through a form (WPF?)
- Create background job to automatically run upload process every X minutes while PowerShell host is open
- Add support for PowerShell console (history commands?)
- Add support for deploying as a module with configuration settings in profile
- Add support for SharePoint 2010 native PowerShell commandlets
- Retrieve past version of script from document library
- Create help file
After I started writing down these features I began to realize that perhaps there would be enough interest to make this script into a CodePlex project that could be community developed and reviewed.
I am happy to announce the creation of Save PowerShell To SharePoint as a new CodePlex project. I have to admit, I have never created or participated in a CodePlex project up to this point. Despite my lack of previous involvement I feel like this is a great pet project for me to break into a community project. This project is currently in the alpha stages while I upload the script code and work on documenting the current status and to-do items. I’m trying to work towards attainable goals and plan on this being an organic project rather than planning on version X.0 from the get go.
In this post I provided a proof of concept PowerShell script for uploading the currently opened files in a PowerShell ISE instance and also announced the creation of my first CodePlex project Save PowerShell To SharePoint. Overall I am very excited to begin work on this project as it combines two of my great passions: PowerShell and SharePoint. A big thank you to Hal and Jonathan for bringing up this idea as I don’t want to steal any of their credit for the formation of the idea. Once again it amazed me that PowerShell provided a lightweight platform to throw together a useful script in such a short amount of time. I know that I won’t be able to devote as much time to this as I would like at first, but great things start with baby steps. Please feel free to try out the script and leave feedback on the project site as development progresses.
PowerScripting Podcast home page
PowerScripting Podcast Episode 122 – Don Jones Scripting Editor Shoot-Out
PowerScripting Podcast Episode 121 – Jay Dave on UAC and AppLocker
Programmatically uploading files to a SharePoint 2007 document library
Save PowerShell To SharePoint CodePlex project page
Monday, October 11, 2010 8:37 AM