PowerShell: Upload file su una document library
Con questo script è possibile caricare in SharePoint 2007 (WSS3 - MOSS), in automatico, tutti i file contenuti in una cartella (mydocs) all'interno di una document library (http://localhost/DocForTest/
)
una variante dello script con la possibilità di passare i parametri:
File: UploadFiles.ps1
esempio
PowerShell
# carico l'assembly che mi serve
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$wc = new-object System.Net.WebClient
$wc.Credentials = [System.Net.CredentialCache]::DefaultCredentials
function getname($filename){ "http://localhost/DocForTest/" + $(split-path -leaf $filename)}
dir "mydocs" | % { $uploadname=getname $_; $wc.UploadFile($uploadname,"PUT", $_.FullName) }
File: UploadFiles.ps1
PowerShell
#carica i file di una directory su una doclib di sharepoint
Param (
[string]$netPath = "",
[string]$sharePointPath = ""
)
if([string]::IsNullOrEmpty($netPath) -or [string]::IsNullOrEmpty($sharePointPath)){
write-host "Carica i file in una document library di SharePoint"
write-host "Sintassi: UploadFiles.ps1 <nomeDirectory> <nomeDocLib>"
write-host "Esempio: UploadFiles.ps1 .\*.zip http://sharepoint.sgart.it/Documents"
break
}
#$netPath = ".\*.zip"
#$sharePointPath = "http://sharepoint.sgart.it/Documents"
write-host "Leggo da: $netPath" -f green
write-host "Carico su: $sharePointPath" -f green
$wc = new-object System.Net.WebClient
$wc.Credentials = [System.Net.CredentialCache]::DefaultCredentials
$destPath = $sharePointPath
if($destPath.EndsWith("/") -eq $false) {
$destPath = $destPath + "/"
}
write-host "Uploading..."
$dir = (dir $netPath )
$m = $dir.Count
write-host "Uploading $m files ..."
$i = 1
$dir| % {
$fileName = $_.Name
$uploadname = [string]::format("{0}{1}", $destPath,$fileName)
write-host "$i / $m - $uploadname"
$wc.UploadFile($uploadname,"PUT", $_.FullName)
$i = $i + 1
}
PowerShell
.\UploadFiles.ps1 .\*.zip http://sharepoint.sgart.it/Documents