INI file parsing in PowerShell

前端 未结 7 1153
[愿得一人]
[愿得一人] 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:14

    One possibility is to use a .NET ini library. Nini for example.

    I've translated the Simple Example from the Nini docs into PowerShell below. You need to put nini.dll into the same directory as the script.

    $scriptDir = Split-Path -parent $MyInvocation.MyCommand.Definition
    Add-Type -path $scriptDir\nini.dll
    
    $source = New-Object Nini.Config.IniConfigSource("e:\scratch\MyApp.ini")
    
    $fileName = $source.Configs["Logging"].Get("File Name")
    $columns = $source.Configs["Logging"].GetInt("MessageColumns")
    $fileSize = $source.Configs["Logging"].GetLong("MaxFileSize")
    
    0 讨论(0)
提交回复
热议问题