pnp / powershell

PnP PowerShell
https://pnp.github.io/powershell
MIT License
684 stars 348 forks source link

[BUG] potential bug with files containing %20 in the file name #4370

Closed tiagoduarte closed 1 week ago

tiagoduarte commented 1 month ago

I have performed extensive testing to at least within reason, ensure that this is not something I am going.

System: Microsoft Windows Server 2019 Standard 10.0.17763 PS 7.4.5 PnP.PowerShell 2.12.0

Here are the exact steps to reproduce the issue:

create a file and ensure that in the name, it contains the literal characters "%20", for example "test%20file.txt" (which is something that is now allowed to happen)

pre-check 1) check the copied "path" from the properties, it will look like this, https://contoso.sharepoint.com/teams/testsite/testlibrary/test%2520file.txt the file will open, via UI, without any problems

pre-check 2) use site search to locate the document, which will show it and open correctly via search

now let's run some scripts and see how the different errors appear

error path 1: get-pnpfile aslistitem

connect-pnponline -url $site -Interactive get-pnpfile -url "https://contoso.sharepoint.com/teams/testsite/testlibrary/test%2520file.txt" -aslistitem

output: Get-PnPFile: The object specified does not belong to a list.

error path 2: search results

$searchQuery = '(IsDocument:True AND path:https://contoso.sharepoint.com/teams/testsite/testlibrary)' $searchResults = Submit-PnPSearchQuery -Query $searchQuery -MaxResults 500 -StartRow 0 write-host $searchResults.ResultRows.Path

output: https://contoso.sharepoint.com/teams/testsite/testlibrary/test%20file.txt

note: this is already a red flag. the path is not encoded properly and will not work

get-pnpfile $searchResults.ResultRows.Path

output: Get-PnPFile: File Not Found.

error path 3: file properties

connect-pnponline -url $site -Interactive $file = get-pnpfile -url "https://contoso.sharepoint.com/teams/testsite/testlibrary/test%2520file.txt"

note: so far, no errors

$context.load($file.ListItemAllFields) $context.ExecuteQuery()

output: MethodInvocationException: Exception calling "ExecuteQuery" with "0" argument(s): "The object id "site:REDACTED:web:REDACTED:file:/teams/testsite/https://contoso.sharepoint.com/teams/testsite/testlibrary/test%20file.txt" is invalid."

appreciate any help.

I'm not sure if this qualifies for a ticket, since it is likely related to the pnp commands and everything works within the user interface

koskila commented 3 weeks ago

I think this is because you're using an absolute URL for Get-PnpFile instead of server-relative URL. I suppose the commandlet should either validate that it's not given an absolute URL, or just change absolute URLs to relative ones.

Based on my quick testing, the latter works fine in this case, but I'm not sure the commandlet should take the responsibility. Any thoughts, @gautamdsheth ?