![](/img/trans.png)
[英]Upload file to Sharepoint Online (Microsoft 365) using Powershell (Option 4 - Using Microsoft.SharePoint.Client.ClientContext)
[英]Upload file to Sharepoint Online (Microsoft 365) using Powershell (Option 2 - Using Microsoft.SharePoint.PowerShell)
我正在嘗試將文件上傳到 Sharepoint Online (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()
它從一開始就失敗了:
Add-PSSnapin:沒有為 Windows PowerShell 版本 5 注冊管理單元
我曾嘗試使用安裝模塊:
Install-Module "Microsoft.Sharepoint.Powershell"
但它找不到它:
PackageManagement\\Install-Package :未找到指定搜索條件和模塊名稱“Microsoft.Sharepoint.Powershell”的匹配項
我想腳本還需要憑據。 如果你能幫我解決這個問題,我也將不勝感激
謝謝
Microsoft.Sharepoint.Powershell
僅用於服務器端腳本。 它取決於同一台計算機上的本地服務器場。
您應該查看包裝客戶端 API (CSOM) 的PnP PowerShell 模塊。
也就是說,您必須了解 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.