I often get asked to help customers write PowerShell scripts to aid with the reporting and administration of their SharePoint environment and as you can probably see from my previous Blog posts, this is something I rather enjoy! Recently a customer asked me to help them write a script that would extract information from SharePoint (for example a list of Sites) and then upload this information to a SharePoint document library. This was actually a lot easier than I anticipated and I thought I would share :)
Please find the script below, this has been tested on SharePoint 2010 and should work on 2013 too. the highlighted values need to be updated to reflect your environment -
ASNP *SharePoint* -EA SilentlyContinue #Declare Variables $Output = "D:\Logs\Output.txt" $WebURL = "http://intranet.contoso.com/Sites/IT" $ListName = "SharePointInfo"
#Create something to upload, in this case a list of all sitesGet-SPSite | Out-File -FilePath $Output #Upload the results to SharePoint $File = Get-Item $Output $Stream = $File.OpenRead() $Web = Get-SPWeb $WebURL $List = $Web.Lists["$ListName"] $FileCollection = $List.RootFolder.Files $FileCollection.Add($File.Name,$Stream,$true) $Stream.Close() $File.Delete()
This is going to be incredibly handy for me as I have a couple of users who have to upload the same document to multiple locations with different names for each location. I'm going to adapt this concept to upload straight from their computer with the $Output effectively becoming $SourceFile. That's the easy part to add to this script. Here's my question...
What if the $Output has to be a .pdf?
Thanks in advance!
This script has to be run directly from a SharePoint server as it uses the object model, if I get chance next week will include an example that can be run remotely.