SharePoint: Script PowerShell pour Copier les Documents Entre des Sites

S'applique à

  • SharePoint 2007
  • SharePoint 2010

Note : Cet article est une traduction.

Une tâche très commune et pénible pour les administrateurs est de copier des documents entre des sites. Les flux de travail servent à faire la même chose. Pour donner plus de souplesse, le script suivant. Ce script est exécuté dans deux modes.

Dans le premier mode, il copie tous les fichiers du dossier source vers le dossier de destination. Ces dossiers peuvent être la bibliothèque de documents ou les dossiers à l'intérieur. Lorsque vous sera demandé l'URL des dossiers, vous devez prévoir le format suivant : http://site/Library ou http://site/library/Foldername.

Dans le second mode, vous pouvez choisir de copier un seul fichier parmi tous les fichiers du dossier source dans le dossier de destination. Cela fonctionne dans MOSS 2007 ainsi qu'en SharePoint 2010.

param([switch]$help) [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") function GetHelp() { $HelpText = @" DESCRIPTION:This script can be executed in two modes in the first mode it will copy all files from the source folder to the destination folder, in the second mode it will copy thespecific file from the source folder to destination folder. This folder can be a document library or a folder inside the document library.This will also check in the file so that it will be visible to authorized users. If the file already exists in the destination location this tool will overwrite thefile."@ $HelpText } function RahulCopyDocumentsInSharepoint() {  write-host "Hello, This tool will copy the the files between the folders specified. It will overwrite the file if it exists at the destination location"        write-host "Please enter the URL of the source folder"        $sourceURL = read-host        write-host "Please enter the URL of the destination folder"        $destinationURL = read-host        write-host "do you want to copy a specific file?if yes then type y or else type anything else"        $fileDecision = read-host        $cFile = [String]::Empty        if($fileDecision -eq "y")                {                    write-host "Please enter the filename with extension of the file"                    $cFile = read-host                }        $sourceSite = New-Object Microsoft.SharePoint.SPSite($sourceURL)        $sourceWeb = $sourceSite.OpenWeb()        $destinationSite = New-Object Microsoft.SharePoint.SPSite($destinationURL)        $destinationWeb = $destinationSite.OpenWeb()        $sList = [Microsoft.Sharepoint.SPFolder]$sourceWeb.GetFolder($sourceURL)        $dList = [Microsoft.Sharepoint.SPFolder]$destinationWeb.GetFolder($destinationURL)        $dLibrary = $destinationWeb.Lists[$dList.ContainingDocumentLibrary]        $files = $sList.Files        if ($fileDecision -ne "y")        {        foreach ($ctFile in $files)        {        $sbytes = $ctFile.OpenBinary()        $dListFiles = $dList.Files        foreach ($dListFile in $dListFiles)        {        if($dListFile.Name.Equals($ctFile.Name))         {         $dListFile.CheckOut()         }         }        $dFileName = $dList.Files.Add($ctFile.Name, $sbytes, $true)        if ($dFileName.CheckOutStatus -ne [Microsoft.Sharepoint.SPFile]::SPCheckOutStatus::None)           {           $dFileName.CheckIn("Checking in")            }            }            }            else            {            $desFile = $sList.Files[$sourceURL +"/" + $cFile]            $sbytes = $desFile.OpenBinary()             $dListFiles = $dList.Files            foreach ($dListFile in $dListFiles)             {            if($dListFile.Name.Equals($desFile.Name))               {          $dListFile.CheckOut()              }                                        }            $dFileName = $dList.Files.Add($desFile.Name, $sbytes, $true)            if ($dFileName.CheckOutStatus -ne [Microsoft.Sharepoint.SPFile]::SPCheckOutStatus::None)            {          $dFileName.CheckIn("Checking in")             }            }                                $destinationWeb.Update()                                write-host "The operation completed successfully" $sourceSite.Dispose()$sourceWeb.Dispose()$destinationSite.Dispose()$destinationWeb.Dispose()        }  if($help) { GetHelp; Continue }else { RahulCopyDocumentsInSharepoint }                

Autres Langues

Cet article est également disponible dans les langues suivantes :