Questo script
PowerShell permette di recuperare un file da un content database di
SharePoint 2010.
Vanno solo impostate le variabili:
- $sqlServer: il nome o l'alias del server di database
- $database: il nome del database di content
- $url: la url del file da recuperare, la prima parte del nome (protocollo e nome host) non è importante in quanto i file sono salvati con indirizzo relativo
il file estratto viene salvato nella directory corrente ($PWD) con il nome originale.
# http://www.sgart.it
# Recupero di un file da un database SharePoint 2010
# il database non deve necessariamente essere agganciato a SharePoint
# DATI DI INPUT $sqlServer, $database, $url del file da recuperare
# nome server o alias sql
$sqlServer = "SharePointDB"
# nome del db restorato anche non agganciato a SharePoint
$database = "SP_Content_Sgart_Temp"
# url del file da recuperare
# (la parte iniziale http://sharepoint.sgart.local è ininfluente)
[uri]$url="http://sharepoint.sgart.local/Documentazione/Documents/UFFICIO/area-01.xlsx"
#trasformazione part assoluto e nome file
[void][System.Reflection.Assembly]::LoadWithPartialName("System.web")
$path=$url.AbsolutePath
$DirName=[System.Web.HttpUtility]::UrlDecode(([system.io.path]::GetDirectoryName($path)).replace('\','/').trimstart('/'))
$LeafName=[System.Web.HttpUtility]::UrlDecode([system.io.path]::GetFileName($path))
# connesione al db
$cnn = "server=$sqlServer;database=$database;Integrated Security=sspi"
# lettura guid del file
$query = "SELECT [Id], [Size], [UIVersion]/512 AS [Version],[TimeLastModified], [internalVersion] FROM [AllDocs] WHERE DirName='$DirName' AND LeafName='$LeafName'"
# nel caso di più versioni aggiungere alla where:
# and InternalVersion=nnn per identificare una specifica versione
write-host $query -foreground green
$da = new-object System.Data.SqlClient.SqlDataAdapter($query, $cnn)
$tbl = new-object System.Data.DataTable
$count = $da.fill($tbl)
$da.dispose()
$tbl.rows | % { Write-Host "File Id: $($_.Id), Modified: $($_.TimeLastModified), Version: $($_.Version), Size: $($_.Size), InternalVersion: $($_.InternalVersion)" -foreground green}
$tbl.dispose()
#return data
if($count > 1){
write-host "Too many versions found. END" -ForegroundColor Green
}else{
$guid = $tbl.rows[0][0]
Write-Host "ID: $guid" -ForegroundColor Green
# lettura dei bytes dell'ULTIMA versione del file
$query1 = "SELECT TOP 1 Content FROM [dbo].[AllDocStreams] WHERE Id = '$guid' ORDER BY InternalVersion ASC"
write-host $query1 -foreground green
$da1 = new-object System.Data.SqlClient.SqlDataAdapter($query1, $cnn)
$tbl1 = new-object System.Data.DataTable
$count = $da1.fill($tbl1)
$da1.dispose()
$binaryFile=$tbl1.rows[0][0]
write-host "File length: $($binaryFile.Length) bytes" -foreground green
# salvataggio del file nel persorso corrente
$dest=$pwd.path +"\"+ $LeafName
[system.io.file]::WriteAllBytes($dest,$binaryFile)
write-host "File salvato in : '$dest'" -foreground green
$tbl1.dispose()
}
Per funzionare va eseguito con un utente che ha accesso al DataBase.