powershell 阅读INI文件/正则表达式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 阅读INI文件/正则表达式相关的知识,希望对你有一定的参考价值。

$file = "D:\Powershell\MAUV\config.ini"
Function Parse-IniFile ($file)
{
	$file
	$ini = @{ }
	$section = "_NO_SECTION"
	$ini[$section] = @{ }
	switch -regex (Get-Content $file)
	{
		'^\[(.+)\]$'
		{
			$section = $matches[1].Trim()
			if (-not $ini.ContainsKey($section)) { $ini[$section] = @{ } }
		}
		'^\s*([^#\[;].+?)\s*=\s*(.*)'
		{
			$name, $value = $matches[1 .. 2]
			$ini[$section][$name] = $value.Trim()
		}
		'^\s*([^;\[#][^=]+?)$'
		{
			$name = $matches[1].Trim()
			$value = ''
			$ini[$section][$name] = $value.Trim()
		}
	}
	$ini
}

以上是关于powershell 阅读INI文件/正则表达式的主要内容,如果未能解决你的问题,请参考以下文章