Download all content in a site collection

Hi,

I’ve been working on a script that will allow you to download all files that are stored in SharePoint in a given site collection.

If the path does not exist, the script will prompt you to create it for you. Before the script runs, it also checks if the site collection exists.

Run the script like this:

.\Get-SPContent.ps1 -SiteCollection "<SiteCollectionURL>" -Destination "<Path>"

Preview

The console shows which libraries were exported to your file system.

—– * Advanced * —–

If you have specific requirements as to which (type of) libraries you want to export, you can change the following line to fit your requirements:

$lists = $web.lists | ?{$_.itemcount -ge "1" -And $_.Hidden -eq $false -And $_.BaseType -eq "DocumentLibrary"} #Excludes all hidden libraries and empty libraries

Below is the code you can save as Get-SPContent.ps1

param
(
[Parameter(Mandatory=$true)]
[ValidateScript({asnp *sh* -EA SilentlyContinue;if (Get-SPSite $_){$true}else{Throw "Site collection $_ does not exist"}})]
[string]$SiteCollection,
[Parameter(Mandatory=$true)]
[ValidateScript(
{
if (Test-Path $_)
{$true}
else{
$d = $_
$title = "Create Folder?";
$message = "$_ doesn't exist, do you want the script to create it?";
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Creates directory $_";
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Exits script";
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no);
$result = $host.ui.PromptForChoice($title,$message,$options,1);
switch($result)
{
0 {New-Item $d -Type Directory;$true}
1 {Throw "Please create the folder before running the script again. `nExiting script"}
}
}
})]
[string]$Destination
)

Asnp *sh* -EA SilentlyContinue

Start-SPAssignment -Global | Out-Null

function Get-SPWebs($SiteCollection){
$SiteCollection = Get-SPSite $SiteCollection
$webs = @()
$SiteCollection.allwebs | %{$webs += $_.url}
return $webs
}

function Get-SPFolders($webs)
{
foreach($web in $webs)
{
$web = Get-SPWeb $web
Write-Host "`n$($web.url)"

$lists = $web.lists | ?{$_.itemcount -ge "1" -And $_.Hidden -eq $false -And $_.BaseType -eq "DocumentLibrary"} #Excludes all hidden libraries and empty libraries
#$lists = $web.lists | ?{$_.title -eq "Documents" -and $_.itemcount -ge "1" -And $_.BaseType -eq "DocumentLibrary"} #Change any identifier here
foreach($list in $lists)
{
Write-Host "- $($list.RootFolder.url)"

#Download files in root folder
$rootfolder = $web.GetFolder($list.RootFolder.Url)
Download-SPContent($rootfolder)

#Download files in subfolders
foreach($folder in $list.folders)
{
$folder = $web.GetFolder($folder.url)
Download-SPContent($folder)

}

}
$web.dispose()
}
}

function Download-SPContent($folder)
{
foreach($file in $folder.Files)
{
$binary = $file.OpenBinary()
$stream = New-Object System.IO.FileStream($destination + "/" + $file.Name), Create
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$stream.Close()
$writer.Close()
}
}

$webs = Get-SPWebs -SiteCollection $Sitecollection
Get-SPFolders -Webs $webs

Stop-SPAssignment -Global
Advertisements

Resolving "The super user account utilized by the cache is not configured." by using PowerShell

We have all seen the following error in the Application Event viewer after installing a clean SharePoint installation and creating site collections (Event ID 7362):

Object Cache: The super user account utilized by the cache is not configured. This can increase the number of cache misses, which causes the page requests to consume unneccesary system resources.

The first time I saw this error I figured there had to be a way to do this in PowerShell. This post helped me achieve my goal: Mirjam’s blog.

Because I implement several new farms each month, I was getting annoyed that I had to set the permissions and then use PowerShell to set the accounts for each web application.

I created the following PowerShell script to enable the caching for all Web applications. The 2 users that are explained in Mirjam’s blog have to already exist in the current environment.

If there are any questions or remarks regarding the script I would be pleased to hear them, so I can update the script and hopefully improve it. So here it is:


# This script will add the superuser and superreader account to the Policy for web app
# and also set the web application properties so that the users will be used for caching
# The script is designed in a way that you do NOT have to adjust the script before use
Add-PSSnapin Microsoft.SharePoint.PowerShell

# Function
function addUsersToPolicyAndProperties
{
$webapps = get-spwebapplication
$superuser = Read-Host "Fill in the SuperUser account: <Domain>\<Username>"
$superreader = Read-Host "Fill in the SuperReader: <Domain>\<Username>"

#foreach statement that will loop through all web applications
foreach($webapp in $webapps)
{
try {
$SUpolicy = $webapp.Policies.Add($superuser,$superuser)
$SUpolicy.PolicyRoleBindings.Add($webapp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl))
$SRpolicy = $webapp.Policies.Add($superreader,$superreader)
$SRpolicy.PolicyRoleBindings.Add($webapp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullRead))
$webapp.Properties["portalsuperuseraccount"] = [string]$superuser
$webapp.Properties["portalsuperreaderaccount"] = [string]$superreader

$webapp.Update()

Write-Host "$webapp.displayname has been configured correctly"
}
catch
{
Write-Host "Error adding the users for $webapp.displayname" -fore red
}
}
Write-Host "All web applications are done, press any key to exit" -fore green
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
#Call function

addUsersToPolicyAndProperties

Resolving “The super user account utilized by the cache is not configured.” by using PowerShell

We have all seen the following error in the Application Event viewer after installing a clean SharePoint installation and creating site collections (Event ID 7362):

Object Cache: The super user account utilized by the cache is not configured. This can increase the number of cache misses, which causes the page requests to consume unneccesary system resources.

The first time I saw this error I figured there had to be a way to do this in PowerShell. This post helped me achieve my goal: Mirjam’s blog.

Because I implement several new farms each month, I was getting annoyed that I had to set the permissions and then use PowerShell to set the accounts for each web application.

I created the following PowerShell script to enable the caching for all Web applications. The 2 users that are explained in Mirjam’s blog have to already exist in the current environment.

If there are any questions or remarks regarding the script I would be pleased to hear them, so I can update the script and hopefully improve it. So here it is:


# This script will add the superuser and superreader account to the Policy for web app
# and also set the web application properties so that the users will be used for caching
# The script is designed in a way that you do NOT have to adjust the script before use
Add-PSSnapin Microsoft.SharePoint.PowerShell

# Function
function addUsersToPolicyAndProperties
{
$webapps = get-spwebapplication
$superuser = Read-Host "Fill in the SuperUser account: <Domain><Username>"
$superreader = Read-Host "Fill in the SuperReader: <Domain><Username>"

#foreach statement that will loop through all web applications
foreach($webapp in $webapps)
{
try {
$SUpolicy = $webapp.Policies.Add($superuser,$superuser)
$SUpolicy.PolicyRoleBindings.Add($webapp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl))
$SRpolicy = $webapp.Policies.Add($superreader,$superreader)
$SRpolicy.PolicyRoleBindings.Add($webapp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullRead))
$webapp.Properties["portalsuperuseraccount"] = [string]$superuser
$webapp.Properties["portalsuperreaderaccount"] = [string]$superreader

$webapp.Update()

Write-Host "$webapp.displayname has been configured correctly"
}
catch
{
Write-Host "Error adding the users for $webapp.displayname" -fore red
}
}
Write-Host "All web applications are done, press any key to exit" -fore green
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
#Call function

addUsersToPolicyAndProperties