使用PowerShell从SharePoint Online中的列表中提取附件 [英] Extracting attachments from a list in SharePoint Online using PowerShell

查看:181
本文介绍了使用PowerShell从SharePoint Online中的列表中提取附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


不确定我是否将它放在正确的位置,但我正在尝试创建一个powershell脚本,以便从SharePoint上的列表中提取附件。我在下面找到了以下代码:

 $ webUrl =" https://mottmac.sharepoint.com/teams/pj-b0000" 
$ library ="照片"
#Local文件夹转储文件
$ tempLocation =" C:\ Users \CHI \Documents\ temp"
$ s = new-object Microsoft.SharePoint.SPSite($ webUrl)
$ w = $ s.OpenWeb()
$ l = $ w.Lists [$ library]
foreach($ l.Items中的$ listItem)
{
Write-Host"内容:" $ listItem.ID
$ destinationfolder = $ tempLocation +" \" + $ listItem.ID
if(!(Test-Path -path $ destinationfolder))
{
$ dest = New-Item $ destinationfolder -type directory
}
foreach($ listItem.Attachments中的$ attachment)
{
$ file = $ w.GetFile($ listItem.Attachments.UrlPrefix + $ attachment)
$ bytes = $ file.OpenBinary( )
$ path = $ destinationfolder +" \" + $ attachment
写下"Saving $ path"
$ fs = new-object System.IO.FileStream($ path," OpenOrCreate")
$ fs.Write($ bytes,0,$ bytes.Length)
$ fs。关闭()
}
}

但我得到的错误如下:

 new-object:找不到类型[Microsoft.SharePoint.SPSite]:验证是否已加载包含此类型的程序集。 
在行:7 char:6
+ $ s = new-object Microsoft.SharePoint.SPSite($ webUrl)
+ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:InvalidType :( :) [New-Object] ,PSArgumentException
+ FullyQualifiedErrorId:TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

您无法在空值表达式上调用方法。
行:8字符:1
+ $ w = $ s.OpenWeb()
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo:InvalidOperation:(:) [],RuntimeException
+ FullyQualifiedErrorId:InvokeMethodOnNull

无法索引到空数组。
行:9字符:1
+ $ l = $ w.Lists [$ library]
+ ~~~~~~~~~~~~~~~~~~~ ~~~~~
+ CategoryInfo:InvalidOperation:(:) [],RuntimeException
+ FullyQualifiedErrorId:NullArray

所以我将其修改为如下所示:

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

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

但我在本地临时文件夹中的所有内容都是文件夹的数量在SharePoint中,但该文件夹中没有附件。有人能告诉我我做错了吗?


提前谢谢。



解决方案

你从哪里获取这个?是否有任何用户发布的帖子或评论要检查?


当我在家时,我会尝试测试这个,看看我能解决什么问题。


Hi,

Not sure if i put this in the right place but I am trying to create a powershell script to extract attachments from a list on SharePoint online. I found this code below:

$webUrl = "https://mottmac.sharepoint.com/teams/pj-b0000"
$library = "Photos" 
#Local Folder to dump files
$tempLocation = "C:\Users\CHI\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()    
    }
}
 

but i get errors that read as follows:

new-object : Cannot find type [Microsoft.SharePoint.SPSite]: verify that the assembly containing this type is loaded.
At line:7 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 line:8 char:1
+ $w = $s.OpenWeb()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Cannot index into a null array.
At line:9 char:1
+ $l = $w.Lists[$library]
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

so i modified it to read like this:

Connect-PnPOnline -Url 'https://mottmac.sharepoint.com/teams/pj-b0000' -UseWebLogin
$tempLocation = "C:\Users\CHI\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()    
    }
}

but all i get in my local temp folder is the amount of folder as in SharePoint but no attachments are in the folder. Can someone tell me what I am doing wrong?

Thank you in advance.

解决方案

Where did you source this from? Is there any user posted threads or commentary to check?

I'll try and test this when I'm at home to see what I can work out too.


这篇关于使用PowerShell从SharePoint Online中的列表中提取附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆