lipkau / PsIni

Work with INI files in PowerShell using hashtables
http://lipkau.github.io/PsIni
MIT License
151 stars 50 forks source link

File with [ ] in path doesn't seem to work as expected. #56

Open IzaacJ opened 4 years ago

IzaacJ commented 4 years ago

Hello.

Writing a PS script and I seem to be unable to get it to load an ini file when the path to it contains [ ].

Ie, my PS script and the INI file lies in the path Z:\[Scripts]\[PS].

And my script:

$iniFile = ($PSScriptRoot + "\config.ini")
$ini = Get-IniContent $iniFile
$ini["main"]["debug"] = "false"
$ini | Out-IniFile -FilePath $iniFile -Force

I however get an error that the file wasn't found.

No files matching 'Z:\[Scripts]\[PS]\config.ini' were found.
At C:\Users\izz0j\Documents\WindowsPowerShell\Modules\PsIni\3.1.2\Functions\Get-IniContent.ps1:109 char:29
+         switch -regex -file $FilePath {
+                             ~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Z:\[Scripts]\[PS]\config.ini:String) [], RuntimeException
    + FullyQualifiedErrorId : FileNotFound

It then fails to do modifications on the file, and $ini | Out-IniFile -FilePath $iniFile -Force successfully writes an empty file, since it's empty.

EliaSaSe commented 4 years ago

Hello IzaacJ,

the chars [, ], ?, * are interpreted as wildcards, see about_wildcards.

For cases where the path contains wildcards, many cmdlets are providing the -LiteralPath parameter. -LiteralPath takes the path as it is without any wildcard processing.

Look at this example:

PS C:\> Set-Location -Path Z:\[Scripts]
Set-Location : Cannot find path 'Z:\[Scripts]' because it does not exist.
At line:1 char:1
+ Set-Location -Path Z:\[Scripts]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Z:\[Scripts]:String) [Set-Location], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
PS C:\> Set-Location -LiteralPath Z:\[Scripts]
PS Z:\[Scripts]>

Unfortunately Get-IniContent does not provide such a parameter. I see this solutions:

  1. Do not use wildcards in our path
  2. Add a parameter set to Get-IniContent (and other commands in this module) to support -LiteralPath
  3. Add a new command to allow to delegate the "content loading" to the user. Something like that: Get-Content -LiteralPath 'Z:\[Scripts]\[PS]\config.ini' | ConvertTo-IniContent (and as versa ConvertTo-IniString?)

If @lipkau finds the 2nd or 3rd idea sensible, I can help to suggest an implementation via pull request.

Edit: The 3rd idea duplicates issue #47 (Abstract away the concept of converting to/from INI format from that of reading/writing an INI file)