-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathListItemAttachment.ps1
34 lines (30 loc) · 971 Bytes
/
ListItemAttachment.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$siteUrl = "https://chunlong.sharepoint.com/sites/Test"
$listName = "29392471"
$fileWithPath = "C:\Users\chunlonl\Desktop\Tools\ulsviewer.exe"
Connect-PnPOnline -Url $siteUrl -Interactive
$item = Get-PnPListItem -List 29392471 # Use Id, UniqueId, Query, PageSize etc parameters to get your own item(s)
$attch = Get-PnPProperty -ClientObject $item -Property AttachmentFiles
#
# Add list item attchment
#
$memoryStream = New-Object IO.FileStream($fileWithPath,[System.IO.FileMode]::Open)
$fileName = Split-Path $fileWithPath -Leaf
$attachInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation
$attachInfo.FileName = $fileName
$attachInfo.ContentStream = $memoryStream
$attch.Add($attachInfo)
Invoke-PnPQuery
#
# Remove list item attchment
#
$file = $attch.GetByFileName($fileName)
$file.DeleteObject()
Invoke-PnPQuery
#
# Remove all attchments
#
$files = $attch.GetEnumerator()
$files | % {
$_.DeleteObject()
Invoke-PnPQuery
}