INI file parsing in PowerShell

前端 未结 7 1219
[愿得一人]
[愿得一人] 2020-11-27 05:16

I\'m parsing simple (no sections) INI file in PowerShell. Here code I\'ve came up with, is there any way to simplify it?

convertfrom-stringdata -StringData (         


        
7条回答
  •  青春惊慌失措
    2020-11-27 06:05

    I'm not exactly sure what your source data looks like, or what your goal is. What exactly are you parsing for? Can you post a sample of the file? As-is, it looks like you're just concatenating carriage returns to the existing lines of the file and replacing \ with \.

    Nor certain why you're using $_.ToString() since $_ is already a string object output by Get-Content.

    Is the goal just to take a file containing a bunch of name=value pairs, and convert that to a hashtable? That's what ConvertFrom-StringData does, although that cmdlet is only available in the preview of PowerShell v2.

    If your file looks like...

    key1=value1
    key2=value2
    key3=value3
    

    Then all you should need is

    ConvertFrom-StringData (Get-Content .\deploy.ini)
    

    I'm not sure I understand why you're tacking on extra carriage returns. There's also no need to use the -Begin and -End parameters, at least not as far as I can see from what you've posted.

提交回复
热议问题