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.
Background
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 @"
using System.Net;
using System.Text;
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);
}
return;
}
}
"@
$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('*', ''))";
[SharePointFileUploader]::UploadFile($textToOutput, $uploadPath)
}
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.
Future Goals
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.
CodePlex Project
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.
Conclusion
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.
-Frog Out
Links
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
Originally posted on: https://briantjackett.com/archive/2010/10/11/announcing-ldquosave-powershell-to-sharepointrdquo-codeplex-project.aspx#548339That is an outstanding idea. I’m in the process of deploying SharePoint 2010, and this is the one feature I’ve thought was missing from 2007, from an admin perspective. I will definately be monitoring this and testing it out. Keep up the great work
LikeLike
Originally posted on: https://briantjackett.com/archive/2010/10/11/announcing-ldquosave-powershell-to-sharepointrdquo-codeplex-project.aspx#548458Roland, Thanks for the encouragement. I’ve found this side project to be a great learning experience thus far about some of the more advanced PowerShell topics and nuances. On a sad note, if you read my latest blog post you’ll see that I’m putting this project on hold for a few weeks while I recover from a fracture on my right hand that I have to wear a cast for the next 3 1/2 weeks. When I’m recovered I’ll be back at it with some new things I’ve researched since my last code release. Until then feel free to submit any feedback or comments you may have. Thanks again.
LikeLike
Originally posted on: https://briantjackett.com/archive/2010/10/11/announcing-ldquosave-powershell-to-sharepointrdquo-codeplex-project.aspx#553863Dale, Yes I’ve seen it common that CodePlex is blocked by corporate firewalls or networks. Let me know your thoughts when you get a chance to try it out. It’s a work in progress that I add to over time. Thanks.
LikeLike