Gmail.ps icon indicating copy to clipboard operation
Gmail.ps copied to clipboard

Command Save-Attachment path not working as intended.

Open johntyf opened this issue 9 years ago • 1 comments

Hi, I have been playing with gmail.ps these last few hours and I have been having issues with the save-attachment command. When I define a $folder = "c:/gmail/" $gmail | Get-Mailbox -Label "Important" | Get-Message -Unread -HasAttachment -Prefetch | Save-Attachment $folder

the attachments go to the location of the ps1 script under the folder "c". script location "c:/scripts/gmail.ps1" location of attachments "c:/scripts/c/" I was expecting the attachments to be located under "c:/gmail/". Is this a bug or am I missing something?

Thanks in advance. Johnty

johntyf avatar Apr 22 '15 05:04 johntyf

Hi,

I was having the same issue but have identified the issue. This issue is caused by line 815 in Gmail.ps.psm1, this is part of the Save-Attachment function.

You need to change this line from "$paths = ($LiteralPath + $Path | Where { $_ })" to "$paths = @($LiteralPath + $Path | Where { $_ })"

This forces the results to be stored as an array, without this, if the $paths variable only contains one value then it is treated as a string, then using "$destPath = $paths[$i]" as per line 818 will return single characters in position $i rather than the full path.

Line 818 originally shows as $destPath = $paths[$i], I imagine this variable is expected to contain more than one results, so you would select the results one at a time with $i and iterate through one after another.

I have pasted below my working version for you;

function Save-Attachment { [CmdletBinding(DefaultParameterSetName = "Path")] param ( [Parameter(Mandatory = $false, ValueFromPipeline = $true)] [AE.Net.Mail.MailMessage]$Message,

    [Parameter(Position = 0, ParameterSetName = "Path", Mandatory = $true)]
    [string[]] $Path,

    [Parameter(ParameterSetName = "LiteralPath", Mandatory = $true)]
    [string[]] $LiteralPath,

    [Parameter(Mandatory = $false)]
    [switch] $PassThru
)

process {
    $paths = @($LiteralPath + $Path | Where { $_ })

    for ($i = 0; $i -lt $paths.Count; $i++) {
        $destPath = $paths[$i]

        if (!(Test-Path -Path $destPath -PathType Container)) {
            New-Item -Path $destPath -ItemType Container | Out-Null
        }

        $destPath = $destPath | Resolve-Path

        foreach ($a in $Message.Attachments) {
            $filename = ($Message.Uid + "_" + $a.Filename)
            $fileDest = Join-Path $destPath $filename

            if ($i -eq 0) {
                $a.Save($fileDest)
            } else {
                $fileLoc = Join-Path $paths $filename
                Copy-Item $fileLoc $fileDest
            }

            if ($PassThru) {
                Get-ItemProperty $fileDest
            }
        }
    }
}

I hope this helps.

Toby

Buenno avatar Oct 20 '15 12:10 Buenno