lipkau / PsIni

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

Fix random Add-Content : Stream was not readable #44

Closed michaelPf85 closed 5 years ago

michaelPf85 commented 5 years ago

We met some random exceptions while using PsIni. Googling for a while, I found that many people have these exceptions with Add-Content and Set-Content. Replacing it with piping the string into Out-File -Append, the exception does not appear again.

Typical exception :

Add-Content : Stream was not readable.
At C:\Users\...\PsIni\2.0.7\Functions\Out-IniFile.ps1:158 char:29
+ ...                           Add-Content -Encoding $Encoding -Path $Path
+                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (C:\...:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : GetContentWriterArgumentError,Microsoft.PowerShell.Commands.AddContentCommand
EliaSaSe commented 5 years ago

Dear Michael

When you rename the parameter from $Path to $FilePath, you bring in a breaking change. To prevent this, please add the attribute [Alias("Path")] to the parameter $FilePath.

Best Regards Elia

michaelPf85 commented 5 years ago

Hello Elia

Thank you for your feedback. I thought it was only some internal API and would not break anything, but I was obviously mistaken.

I've added the Alias as you suggested.

Best regards Michael

lipkau commented 5 years ago
michaelPf85 commented 5 years ago

Hello @lipkau ,

I renamed Path into FilePath because I didn't know of another way to make the calls with "@parameters" work. Add-Content takes a "Path" parameter but Out-File takes a "File-Path" parameter. If you have another idea I'll change to whatever works best.

Regarding the Strict mode it's a mistake when I edited my last commit, I'm fixing this.

mklement0 commented 5 years ago

As an aside re Set-StrictMode -Version Latest: I suggest avoiding it in production code - see https://github.com/MicrosoftDocs/PowerShell-Docs/issues/4042

lipkau commented 5 years ago

thank you for the input, @mklement0

I am aware of how much harder Set-StrictMode -Version Latest makes things. I will remove it, but not in the context of this PR.