powershell: [BUG] Get-PnPFile throws "Does Not Exist" error when source path contains "+" (plus symbol)

Reporting an Issue or Missing Feature

When using Get-PnPFile to write a file from SharePoint to NTFS the cmdlet throws a <UNC> does not exist error when any portion of the path or filename contains a “+”

Early versions of SharePoint PnP didn’t have this issue as I seeded the data I’m trying to update with the exact code that’s failing now.

Expected behavior

I expect Get-PnPFile to treat source and destination paths as -LiteralPath (or include -LiteralPath in the cmdlet).

Actual behavior

In the image below my Write-Host output shows “+” in the file path. The Get-PnPFile error shows spaces where the “+” should be. image

Steps to reproduce behavior

Code generating the above screenshot:

$serverrelativeurl = $item.FieldValues.FileRef

       `$destinationfolder = $prefix + $item.FieldValues.FileDirRef.Replace("/","\")
        $serverrelativeurl = $item.FieldValues.FileRef 
        write-host "Writing:" $targetpath -ForegroundColor Green
        $result = Get-PnPFile -ServerRelativeUrl $serverrelativeurl -Path $destinationfolder -FileName $filename -AsFile -Force
        if (Test-Path -LiteralPath $targetpath) {
            (Get-Item -LiteralPath $targetpath -ErrorAction Continue).LastWriteTime = $modified
            $acl = Get-Acl -LiteralPath $targetpath
            $acl.SetOwner([System.Security.Principal.NTAccount] "Domain Admins")
            Set-Acl -LiteralPath $targetpath -AclObject $acl
            $global:writes += 1
        }
        else {
            write-host "ERROR Writing:" $targetpath -ForegroundColor Red`

What is the version of the Cmdlet module you are running?

PnP.PowerShell 1.10.24 PS5.1 Server 2016

Which operating system/environment are you running PnP PowerShell on?

  • [ x] Windows
  • Linux
  • MacOS
  • Azure Cloud Shell
  • Azure Functions
  • Other : please specify

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 22 (2 by maintainers)

Most upvoted comments

What’s missing here is -LiteralPath. It used to be a feature of most PnP calls. With the later 1.2x versions of PnpFile I have stopped pre-encoding. “+” logic was included in the function (1.14??), encoding screws that up.

jbw

The Get-PnPFile is a bit of a mess already with all the different variants it tries to support, not really in favor of adding yet another parameter to it. What’s the problem with doing it this way? If you already encode them yourself, it should still work fine, no?

@milanholemans, can’t remember the exact scenario, but I remember we had a couple of people reporting issues on it that lead to the decoding thing. Perhaps if you copy/copied the URL or a file somewhere it was encoded? Can’t remember.

Got a repro here. Let me see what I can do to fix both scenarios.

The problem is that the specified URL is always decoded within the cmdlet by this piece of code:

https://github.com/pnp/powershell/blob/f2c41de2fbc6defce6e6cc304b184c3c2a776b8c/src/Commands/Files/GetFile.cs#L71-L72

Because a + sign is the encoded variant of a space, a path named one+two will become one two. Since this path is not valid, it throws a not found error. I’m not sure if we can provide a clean fix for this since it is quite hard to tell whether a provided URL is encoded or decoded.

What you can do to make your script work is pretty easy. If you encode all your URLs, your script will work just fine. Simply do something like this:

$encodedUrl = [System.Web.HttpUtility]::UrlEncode("/sites/HR/Shared Documents/Folder one+two/Document.pdf")
Get-PnPFile -ServerRelativeUrl $encodedUrl