代码之家  ›  专栏  ›  技术社区  ›  Michael A

在同一网站集中的文档库之间移动文件

  •  4
  • Michael A  · 技术社区  · 12 年前

    我需要将大约10000个文件从同一网站集中的一个文档库移动到另一个文档馆。我相信powershell是实现这一目标的最佳手段。

    我发现了以下文章: http://blog.isaacblum.com/2011/10/04/spfilecollection-class-copy-files-to-another-document-library/#respond 这为我提供了一种方法,但我不确定如何改编这个脚本(我将通过这个项目首次接触Powershell)。

    我尝试了以下操作,但没有成功:

    $PSSnapin = Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
    clear
    
    $org = "hhttp://farm/sitecollection/Document Library Source/Forms/AllItems.aspx"
    $dest = "hhttp://farm/sitecollection/Document Library Destination/Forms/AllItems.aspx"
    
    $orgLibrary = (Get-SPWeb $org).Folders["Documents"]
    $destLibrary = (Get-SPWeb $dest).Folders["Documents"]
    $destFiles = $destLibrary.Files
    foreach ($file in $orgLibrary.Files)
    {
        $curFile = $file.OpenBinary()
        $destURL = $destFiles.Folder.Url + "/" + $file.Name
        $destFiles.Add($destURL, $curFile, $true)
    }
    

    有其他方法吗?请注意,我使用的是MOS2007和Powershell 2.0,而不是SharePoint 2010。

    更新/半回答:

    根据下面x0n的帖子,SharePoint 2007(仅2010年)不支持此功能。我在这个帖子之外收到了以下建议,这些建议很中肯,将来应该会帮助其他人:

    不幸的是,SharePoint 2010的命令行管理程序(它是PowerShell 管理单元和关联的cmdlet)与MOSS 2007不兼容,并且 没有可直接从Microsoft获得的cmdlet SharePoint版本。这意味着你仍然可以使用 PowerShell与MOSS 2007,但您要么必须编写 您自己的使用STSADM或SharePoint对象模型的cmdlet 直接执行,否则您将不得不使用与MOSS 2007兼容的cmdlet 来自第三方。我建议查看Gary Lapointe的博客 许多适用于MOSS 2007的优秀PowerShell cmdlet (http://blog.falchionconsulting.com/),或人们上传的地方 cmdlet,例如CodePlex.com、TechNet脚本库, POSHCode.org,或 http://get-spscripts.com/

    1 回复  |  直到 12 年前
        1
  •  4
  •   x0n    12 年前

    我并不感到惊讶:Microsoft.SharePoint.PowerShell管理单元仅适用于SharePoint 2010,在SharePoint 2007服务器上不可用。

    坦率地说,最简单的方法是打开Internet Explorer,导航到源文档库并打开“资源管理器视图”。选择所有文档,然后复制(ctrl+c)。打开另一个IE窗口,对目标文档库执行相同的操作并粘贴(ctrl+v)。

    如果它无法在资源管理器视图中打开,请确保用于进行复制/粘贴的机器正在运行“WebClient”服务。如果您运行的是Windows 2008 R2,除非您决定添加“桌面体验”功能,否则此服务不可用。相反,找到一台拥有WebClient服务的Windows7机器要容易得多(但要确保它正在运行)

    更新:

    也就是说,你的脚本可能已经有80%了,并不需要2010年的管理单元。我现在无法对此进行测试(抱歉),但应该有99%的正确率:

    [reflection.assembly]::loadwithpartialname("microsoft.sharepoint") > $null
    
    $org = "http://farm/sitecollection/sourcedoclib" 
    $dest = "http://farm/sitecollection/targetdoclib" 
    
    $site = new-object microsoft.sharepoint.spsite $org
    $web = $site.openweb()
    
    $srcLibrary = $web.Lists["sourcedoclib"] 
    $destLibrary = $web.Lists["targetdoclib"] 
    
    $destFiles = $destLibrary.Folders["Archived"]
    
    foreach ($item in $srcLibrary.Items) 
    { 
       if ($item.File) {
            $curFile = $item.file.OpenBinary() 
            $destURL = $destFiles.Folder.Url + "/" + $item.file.Name 
            $destFiles.Add($destURL, $curFile, $true)
        }
    } 
    

    祝你好运