【发布时间】:2019-09-09 23:45:08
【问题描述】:
我正在尝试改进原始解决方案 (INI file parsing in PowerShell),以便我可以解析一个带有条目的 INI 文件,如下例所示。
[proxy]
; IP address and port number
server = 192.168.0.253
port = 8080
logfile=session.log ; log session
[user]
; default username and settings
name=J. Doe ;name
address="377 Sunrise Way;Santa Monica;CA" ; address
[program files]
root="C:\Program Files\Windows " ; path name
path="C:\Program Files\Windows;%windir" ; path name
;
[program]
root=C:\Program Files\Windows ; path name
path=C:\Program Files\Windows;%windir ; path name
我正在使用以下 powershell 代码填充嵌套哈希表(如果这是正确的描述),其中包含每个部分的名称/值对。
我在处理以注释结尾的第一部分或第二部分中包含空格的值没有问题,但是当我尝试混合引用字符串和 cmets 时出现问题。
鉴于一个字符串以双引号开头和结尾,我认为应该可以获得我想要的结果,但我显然在某处遗漏了一些东西(我对此有点陌生)。
function Parse-INI-File() {
Param ([parameter()][string]$_file = '')
# Don't prompt to continue if '-Debug' is specified.
If ($DebugPreference -eq "Inquire") {$DebugPreference = "Continue"}
$_settings=@{}
switch -Regex -file $_file {
'(?:^ ?\[\s*(?<section>[^\s]+[^#;\r\n\[\]]+)\s*\])' {
$_section = $Matches.section.trim()
$_settings[$_section] = @{}
}
'(?:^\s*?(?<name>[^\[\]\r\n=#;]+))(?: ?=\s*"?(?<value>[^;#\\\r\n]*(?:\\.[^"#\\\r\n]*)*))' {
$_name, $_value = $Matches.name.trim(), $matches.value.trim()
$_settings[$_section][$_name] = $_value
Write-Debug "/$_section/ /$_name//$_value/" # Debug
}
}
$_settings
}
$_file='./ini-example.ini'
$_output=Parse-INI-File -Debug ($_file)
我希望解析示例 ini 文件以生成以下名称/值对:
DEBUG: /proxy/ /server//192.168.0.253/
DEBUG: /proxy/ /port//8080/
DEBUG: /proxy/ /logfile//session.log/
DEBUG: /user/ /name//J. Doe/
DEBUG: /user/ /address//377 Sunrise Way;Santa Monica;CA/
DEBUG: /program files/ /root//C:\Program Files\Windows/
DEBUG: /program files/ /path//C:\Program Files\Windows;%windir/
DEBUG: /program/ /root//C:\Program Files\Windows/
DEBUG: /program/ /path//C:\Program Files\Windows/
我不介意引用的字符串是否包含原始引号。
谢谢。
2019 年 9 月 10 日更新 - 我尝试了 psini 模块中的 Get-IniContent 函数,但它不会忽略行尾的 cmets。
PS C:\> $_output = Get-IniContent (".\ini-example.ini")
PS C:\> $_output["program files"]
Name Value
---- -----
root "C:\Program Files\Windows "' ; path name
path "C:\Program Files\Windows;;%windir" ; path name
Comment1 ;
PS C:\>
【问题讨论】:
-
您可能想要使用 Powershell 库中的 PSIni 模块。
标签: regex powershell