使用 PowerShell 将附件添加到 SharePoint 中的列表项
如何向 SharePoint 列表项添加附件?
在 SharePoint 中,您可以向列表项添加附件以提供更多信息或补充列表中的数据。附加文件可以是文档、图像或其他类型的文件。在本文中,我们将向您展示如何向列表项添加附件。
要将附件添加到 SharePoint 列表项,请执行以下操作:
PowerShell 将附件添加到 SharePoint 列表项
以下是用于以编程方式在 SharePoint 中添加附件以列出项目的 PowerShell 脚本:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Custom Function to Add attachment to SharePoint List Item
Function Add-Attachment($Item, $AttachmentPath)
{
$FileContent = [System.IO.File]::ReadAllBytes($AttachmentPath)
$Item.Attachments.Add([System.IO.Path]::GetFileName($AttachmentPath), $FileContent)
$Item.Update()
Write-host "Attachment Added to List Item Successfully!"
}
#Variables
$SiteURL="https://portal.crescent.com/sites/Deals"
$ListName="Tasks"
$ItemID=1
$AttachmentPath="c:\Scripts\ASI-LOG.docx"
$web = Get-SPWeb $SiteURL
$List = $web.Lists[$ListName]
$Item = $list.GetItemById($ItemID)
#Call the function to Add Attachment
Add-Attachment $Item $AttachmentPath
若要将附件添加到 SharePoint Online 列表项,请使用:SharePoint Online:使用 PowerShell 将附件添加到列表项
