繁体   English   中英

使用PowerShell从SharePoint Online提取附件

[英]Extracting attachments from SharePoint online using PowerShell

目前,我正在尝试从SharePoint Online上的列表中提取附件。 我在网上找到了应该执行此操作的代码,但出现错误。 我发现的代码如下:

$webUrl = "https://mm.sharepoint.com/teams/pj-b0000"
$library = "Photos" 
#Local Folder to dump files
$tempLocation = "C:\Users\C\Documents\temp"    
$s = new-object Microsoft.SharePoint.SPSite($webUrl)    
$w = $s.OpenWeb()         
$l = $w.Lists[$library]    
foreach ($listItem in $l.Items)
{
    Write-Host "    Content: " $listItem.ID 
    $destinationfolder = $tempLocation + "\" + $listItem.ID          
    if (!(Test-Path -path $destinationfolder))        
    {            
        $dest = New-Item $destinationfolder -type directory          
    }
    foreach ($attachment in $listItem.Attachments)    
    {        
        $file = $w.GetFile($listItem.Attachments.UrlPrefix + $attachment)        
        $bytes = $file.OpenBinary()                
        $path = $destinationfolder + "\" + $attachment
        Write "Saving $path"
        $fs = new-object System.IO.FileStream($path, "OpenOrCreate") 
        $fs.Write($bytes, 0 , $bytes.Length)    
        $fs.Close()    
    }
}

但我得到这个错误:

new-object : Cannot find type [Microsoft.SharePoint.SPSite]: verify that the assembly containing this type is loaded.
At C:\Users\C\Documents\SPListExtract.ps1:5 char:6
+ $s = new-object Microsoft.SharePoint.SPSite($webUrl)
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

You cannot call a method on a null-valued expression.
At C:\Users\C\Documents\SPListExtract.ps1:6 char:1
+ $w = $s.OpenWeb()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Cannot index into a null array.
At C:\Users\C\Documents\SPListExtract.ps1:7 char:1
+ $l = $w.Lists[$library]
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

所以我编辑了一下代码,但我只在列表中得到了该项目,而不是项目中的附件。 我写的代码如下:

Connect-PnPOnline -Url 'https://mm.sharepoint.com/teams/pj-b0000' -UseWebLogin
$tempLocation = "C:\Users\C\Documents\temp"
$list = Get-PnPListItem -List 'Photos'

foreach ($listItem in $list)
{
    Write-Host "    Content: " $listItem.ID 
    $destinationfolder = $tempLocation + "\" + $listItem.ID          
    if (!(Test-Path -path $destinationfolder))        
    {            
        $dest = New-Item $destinationfolder -type directory          
    }
    foreach ($attachment in $listItem.Attachments)    
    {        
        $file = $w.GetFile($listItem.Attachments.UrlPrefix + $attachment)        
        $bytes = $file.OpenBinary()                
        $path = $destinationfolder + "\" + $attachment
        Write "Saving $path"
        $fs = new-object System.IO.FileStream($path, "OpenOrCreate") 
        $fs.Write($bytes, 0 , $bytes.Length)    
        $fs.Close()    
    }
}

我看到我的问题是我认为$ file变量的内部foreach循环。 有人可以帮助我吗?

提前非常感谢。

错误的第一行表示您没有加载程序集:

new-object : Cannot find type [Microsoft.SharePoint.SPSite]: verify that the assembly containing this type is loaded.

这些程序集仅安装在SharePoint服务器上:

https://social.technet.microsoft.com/Forums/en-US/4a78ed2c-efde-40fa-800c-c4ecfa68a7c4/cannot-find-type-microsoftsharepointspsite-when-running-sharepoint-powerscript-in-a-windows- 10?=论坛sharepointdevelopment

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM