我正试图将一个文件上传到Sharepoint (M365)库子文件夹中,但它总是给我带来错误。我试过很多剧本。这篇文章是关于使用Microsoft.SharePoint.PowerShell的(我已经发布了关于其他脚本的问题,希望有人能帮助我处理其中的任何一个)。
这是代码:‘
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Write-Host "Adding PSSnapin"
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$url="https://mydomanin.sharepoint.com/sites/mySite/"
Write-Host "Connecting to URL..."
$web=Get-SPWeb -Identity $url
if($web)
{
try
{
$list = $web.Lists.TryGetList("myLibrary/subfolder")
$files = Get-Item -Path "C:\MyFolder\myFile.csv" -Force
foreach ($file in $files)
{
$stream = $file.OpenRead()
$done= $list.RootFolder.Files.Add($file.Name, $stream, $true)
Write-Host $done.Name "Uploaded into the Site" -BackgroundColor Green
}
}
catch
{
$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage
}
}
else
{
Write-Host "Site Doesn't exist"
}
$list.Update()它从一开始就失败了:
外接程序:没有为Windows PowerShell版本5注册任何插件
我试过使用安装模块:
Install-Module "Microsoft.Sharepoint.Powershell"但它找不到它:
PackageManagement\Install-Package :未找到指定搜索条件和模块名称“Microsoft.Sharepoint.Powershell”的匹配
我想脚本也需要凭证。如果你能帮我,我也会很感激的。
谢谢
发布于 2021-07-16 09:04:39
Microsoft.Sharepoint.Powershell只用于服务器端脚本。它依赖于同一台计算机上的本地农场。
您应该看看PnP PowerShell模块,它封装了客户端API (CSOM)。
也就是说,您必须了解SSOM (服务器端对象模型)和CSOM (客户端对象模型)之间的区别。特别是两点:
你应该以类似于
Import-Module PnP.Powershell
Connect-PnPOnline https://yourtenant.sharepoint.com/sites/yoursite -Interactive
Add-PnPFile c:\myfolder\myfiles.csv -Folder "YourLibrary"参考文献:Add-PnPFile
https://stackoverflow.com/questions/68406178
复制相似问题