Opened a case with Microsoft TrackingID#2408270040008802.
Resolution:
· There is not a simple way using the built-in cmdlets to rename an existing file attachment
· I suggested you would need to download the bytes for the attachment, delete the original attachment, then add a new one with the new name.
You were able to get it to work using the SpListItem.Attachments.Add and SpListItem.Attachments.Delete methods in SharePoint Management Shell by writing a PowerShell script.
Here are a few snippets of the code:
$webURL = "
$listName = "{ListName}"
#Get the SPWeb object and save to variable
$listWeb = Get-SPWeb $webURL
#Get the SPList object to retrieve the list
$list = $listWeb.Lists[$listName]
$listItems = $list.Items
$intRenamedCounter = 0
$intRenamedErrorCounter = 0
foreach ($listItem in $listItems)
{
[string]$theAttachmentFlag = $listItem["Attachments"]
if ($theAttachmentFlag.Equals("True"))
{
$intRenamedForThisItem = 0
$AttachmentsColl = @($listItem.Attachments) #This was needed to prevent an error on the foreach attachment loop because the attachments were being changing by this script.
Foreach ($attachment in $AttachmentsColl)
{
Try
{
$file = $listWeb.getfile($listitem.attachments.UrlPrefix + $attachment)
$newfile = $listWeb.GetFile($listItem.Attachments.UrlPrefix + $strNewAttachmentName)
$bytes = $file.OpenBinary()
$listItem.Attachments.Add($strNewAttachmentName,$bytes)
$listItem.Attachments.Delete($attachment)
$intRenamedCounter = $intRenamedCounter + 1
$intRenamedForThisItem = $intRenamedForThisItem + 1
} #End of Try
Catch
{
Write-All " ERROR. A problem occurred renaming the attachment above. Check the hazard attachments for duplicate names. Contact IT for assistance, if needed."
Write-Warning "Caught an exception:"
Write-Warning "Exception Message: $($_.Exception.Message) - Attachment:$($attachment) NewName: $($strNewAttachmentName) "
$intRenamedErrorCounter = $intRenamedErrorCounter + 1
}
}# End of ForEach Attachment
if ($intRenamedForThisItem -gt 0)
{
Retry{
$listItem.Update() #Attachments aren't changed without this
}
Catch
{
Write-All " ERROR. A problem occurred updating the list item above. Contact IT for assistance, if needed."
Write-Warning "Caught an exception:" Write-Warning "Exception Message: $($_.Exception.Message) - List Item:$($strIssueNo) "
$intRenamedErrorCounter = $intRenamedErrorCounter + $intRenamedForThisItem
$intRenamedCount = $intRenamedCounter - $intRenamedForThisItem
}
}
} # End of Attachment = True (Item has an attachment)
} #end of Item Loop