zl程序教程

您现在的位置是:首页 >  大数据

当前栏目

PnP PowerShell获取SharePoint数据

数据 获取 Sharepoint PowerShell
2023-09-27 14:21:57 时间

  前言

  分享一个批量上传文档到SharePoint文档库的脚本吧,come on

  正文

  1.在使用脚本的时候,可能会报错,需要开一个设置

无法加载文件 C:\Source Code\PowerShell\PnP PowerShell.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
    + CategoryInfo          : SecurityError: (:) [],ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnauthorizedAccess

  错误的截图,如下图:

  2.开启PowerShell的策略,如下图:

  3.编写的脚本,如下图:

Connect-PnPOnline -Url "https://linyus.sharepoint.com/sites/DevelopSite" -Credentials (Get-Credential)

$filePath = "C:\Documents"
$files = Get-ChildItem -Path $filePath -Name

foreach($file in $files){
    $docPath = $filePath + "/" + $file
    Add-PnPFile -Path $docPath -Folder "SiteCollectionDocuments"
}

Write-Host "Done"

  4.执行的结果,如下图:

 

  5.上传文档的截图,如下图:

 

   结束语

  一个批量上传文档的功能,只需要短短几行代码就可以,是不是非常方便?来吧,用起来!