Proper working with attachment using Powershell (Add-RDMEntryAttachment x Add-RDMSessionAttachment)

Backlog

Proper working with attachment using Powershell (Add-RDMEntryAttachment x Add-RDMSessionAttachment)

avatar

Greetings everyone,
I would like to ask for clarification on the proper use of Add-RDMEntryAttachment and Add-RDMSessionAttachment for adding session/entry attachments.

I have been using Add-RDMSessionAttachment for years. It was not ideal, but it worked.

It seems that some changes were made to Add-RDMSessionAttachment, and it no longer accepts the -Title and -Description parameters.
I tried using Add-RDMEntryAttachment — it does add an attachment, yes, but that’s all. There is no result, sometimes it freezes, and there is no reference returned to the added attachment.

Even the example in the documentation for Add-RDMEntryAttachment does not make sense to me. When using the -PassThru parameter, it returns references to all attachments, so it updates the title and description on multiple items, not only on the most recently added one:

$entry = Get-RDMEntry -Name EntryA
$attachment = Add-RDMEntryAttachment -InputObject $entry -Filename 'filename.txt' -PassThru
Set-RDMEntryAttachment -InputObject $attachment -Title 'Filename' -Description 'This is an important file'


I would simply like to add an attachment with a Title and Description to an RDM session using PowerShell.

Many thanks to anyone who can clarify this.

Best regards,
Ivo Pastyrik

All Comments (3)

avatar

Hello pastyrik,

Thank you for reaching out to the Devolutions support team.

I was able to reproduce this issue.

I will open an internal ticket.

I'll keep you posted.

Best regards,

Patrick Ouimet

avatar

Hello,

Could you try this one instead:

[CmdletBinding(SupportsShouldProcess)]
param(
    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string]$DataSourceName,

    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string]$vault
)

$ds = Get-RDMDataSource -Name $DataSourceName
Set-RDMCurrentDataSource $ds

$repo = Get-RDMRepository | Where-Object {$_.Name -eq $vault}
Set-RDMCurrentRepository $repo

$entry = Get-RDMsession -Name '<entry name>'

Add-RDMEntryAttachment -InputObject $entry -Filename '<full path of the file with file name>'

$attachment = Get-RDMEntryAttachment -InputObject $entry -Filename '<file name>' |
    Select-Object -Last 1

Set-RDMEntryAttachment -InputObject $attachment `
    -Title 'Essential' `
    -Description 'A description of the attachment' `
    -Refresh


Best regards,

Patrick Ouimet

avatar

Hello Patrick,
thank you for your reply.
I actually did not test your code—I assume it will work—but unfortunately it is not usable for my use case.
In my opinion, it is only a kind of workaround for a poorly designed procedure Add-RDMEntryAttachment with the -PassThru argument, which returns not only the newly added attachment but all attachments with the same filename.
I found my own workaround by including a timestamp in the filename, which makes each filename unique. This way, Add-RDMEntryAttachment -PassThru returns a reference to only one attachment—the most recently added one. In my situation, this approach is more reliable.
Best regards,
Ivo