microsoft / ReportingServicesTools

Reporting Services Powershell Tools
MIT License
456 stars 213 forks source link

Bug in using reports with square brackets in its names #391

Open Bloodboy1986 opened 1 year ago

Bloodboy1986 commented 1 year ago

What is the current behavior? When the function Write-RsFolderContent trys to load a report with square brackets ([ or ]) in the name, we get the following error:

Failed to create catalog item from 'E:\report square bracket test\personen [alle].rdl' in '/M42/DE': 
System.Management.Automation.RuntimeException: No item found at the specified path: E:\report square  
bracket test\personen [alle].rdl!
In E:\ReportingServicesTools-0.0.7.3\ReportingServicesTools\Functions\CatalogItems\Write-RsFolderCont 
ent.ps1:155 Zeichen:21
+ ...             throw (New-Object System.Exception("Failed to create cata ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], Exception
    + FullyQualifiedErrorId : Failed to create catalog item from 'E:\report square bracket test\pers  
   onen [alle].rdl' in '/M42/DE': System.Management.Automation.RuntimeException: No item found at t   
  he specified path: E:\report square bracket test\personen [alle].rdl!

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Try to load a report with square brackets in its name Write-RsFolderContent -ReportServerUri "https://matrix-test.oberhausen.de/ReportServer" -Path "E:\report square bracket test\" -Destination '/M42/DE' -Recurse -OverWrite

What is the expected behavior? No error when using report with square brackets in its names

Which versions of Powershell and which OS are affected by this issue? Did this work in previous versions of our scripts? ReportingServicesTools 0.0.5.2 - 0.0.7.3 PSVersion 5.1.19041.1682 Windows 10

Periscope-NZ commented 1 year ago

I have experienced the exact same error today with braces, also called square brackets - characters are [ and ] . A quick and simple workaround is to simply rename any reports to remove the square brackets from the filename, but this is not ideal.

The issue is actually with Powershell built-in functions such as Test-Path and Convert-Path, which by default treat square brackets like a regular expression instead of literally. Obviously this messes up handling of filenames with literal brackets.

The core of the problem seems to exist in Write-RsCatalogItem around lines 99-128 , where a block of code tests a file's presence and gets its extension.

I think I managed to code a fix, which is not very elegant but seems to work in Powershell 5.1

On line 99, change if (!(Test-Path $item)) to if (!(Test-Path $item.replace('[','``[') )) This escapes the opening square bracket, forcing Test-Path to treat it literally. Note in PowerShell 5.1 Test-Path does not have the -LiteralPath parameter, so we are forced to escape it.

On line 104, change $EntirePath = Convert-Path $item to $EntirePath = Convert-Path -LiteralPath $item Similarly on line 105, change $item = Get-Item $EntirePath to $item = Get-Item -LiteralPath $EntirePath

This seems to work, forcing those functions to treat the path parameter literally

After those changes, I was able to successfully upload files with square brackets in the filename without any errors.

However, this will prevent the use of square brackets as a wildcard / regular expression. Is that something people use?