Every once in a while I get to add a little flair to a project I’m working on. Recently I was working on a couple of projects that intersected, sort of. First, I’ve been writing some scripts to automate some processes we have. These scripts do some pretty good logging to the file system in case there are problems. The average person would have stopped there, but not me. I wanted to take it a step farther. I added a bit where the script uploads the log files to a SharePoint document library. This makes them easier to get to for support personnel, and it makes it easier to search through them for specific issues. SharePoint saves the day again.
The other project I’ve working on is making my Netcast production more automated. Part of that process was using PowerShell to edit the MP3 tags on my Netcast files. While I was pretty proud of that, the previous project made me realize I could take things one step further and have that same script go ahead and upload the MP3 file when it’s finished. One less thing for me to put off when I’m procrastinating. Hooray efficiency. Plus I get braggin’ rights. Score!
On the surface, these two things sound like they’re the same thing, uploading files to SharePoint. Once you get into the weeds though, they’re actually different. In the first case I’m uploading a file to a local SharePoint server. This is pretty simple. While PowerShell doesn’t have a Upload-SPFile cmdlet we’re pretty close to it. Easy peasy. In the second case I’m uploading the files to a remote SharePoint server. Since it’s a remote server we can’t just add the SharePoint PowerShell module and use the same techniques as we would with a local server. Subtle differences, but important differences. As I was searching for “Upload files to SharePoint with PowerShell” I found most articles covered one or the other. Which is very handy if your situation matches the article’s. Not so handy if they don’t. So I wrote this blog post to cover both scenarios.
Scenario 1: Uploading Files to SharePoint on the SharePoint Server
Here’s a quick example of how to upload a document on a SharePoint server using the SharePoint PowerShell module which uses the SharePoint Object Model.
# Set the variables
$WebURL = “http://portal.contoso.com/sites/stuff”
$DocLibName = “Docs”
$FilePath = “C:\Docs\stuff\Secret Sauce.docx”
# Get a variable that points to the folder
$Web = Get-SPWeb $WebURL
$List = $Web.GetFolder($DocLibName)
$Files = $List.Files
# Get just the name of the file from the whole path
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)
# Load the file into a variable
$File= Get-ChildItem $FilePath
# Upload it to SharePoint
$Files.Add($DocLibName +"/" + $FileName,$File.OpenRead(),$false)
$web.Dispose()
In the case we use the Add member of the SPFileCollection class. It has a few overloads, so check them out, there might be one that better fits what you’re trying to do.
Scenario 2: Uploading Files to SharePoint from a Remote Machine
Since we can’t use the object model to upload files remotely we have to go about it a different way. I use the WebClient object, though there might be other ways. Here’s an example:
# Set the variables
$destination = "http://portal.contoso.com/sites/stuff/Docs”
$File = get-childitem “C:\Docs\stuff\Secret Sauce.docx”
# Since we’re doing this remotely, we need to authenticate
$securePasssword = ConvertTo-SecureString "pass@word1" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ("contoso\johnsmith", $securePasssword)
# Upload the file
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = $credentials
$webclient.UploadFile($destination + "/" + $File.Name, "PUT", $File.FullName)
Like the SPFileCollection class’ Add member, the UploadFile member of the WebClient class has a few overloads to consider. Check them out so you know what your options are.
PowerShell is cool. SharePoint is cool. Uploading files is cool. It just makes sense to use PowerShell to upload files to SharePoint. Hopefully this blog post covers all the scenarios you might encounter.
tk
ShortURL: http://www.toddklindt.com/PoshUploadFiles
Edit 4/8/2014 – 1,000 apologies. The code in Scenario 2 didn’t work. I should have checked it better. It’s all good now.