读INI文件参数
我有以下配置INI文件:读INI文件参数
[Applications]
app1= aplication1
app2= aplication2
[BBDD]
server_bbdd = name_bbdd
[DATA]
data_dir = c:\data\
[LOGS]
data_log = c:\data\log\
我需要阅读,并与我的PowerShell脚本加载应用程序数据。例如,对于app1,我需要将此应用的所有数据加载到数据库(是SQL-SERVER BBDD),然后对app2执行相同的操作。
我可以通过PowerShell读取和加载应用程序数据吗?
您可以像使用散列一样处理ini文件,并创建一个散列表并将它们放入变量中。 例如 $ Applications = @ {“app1”=“aplication1”;“app2”=“aplication2”}
这个只读函数将读取ini文件并将这些部分分开, 我们将它读到内存中的散列,您将需要它稍后输出。 “switch -regex”用于处理多个if语句。 -regex将匹配子句(如果字符串)作为正则表达式(正则表达式,即RegEx字符:^。[] - g G?+ * p P w W s s d D $)处理。
function Get-IniContent ($filePath)
{
$ini = @{}
switch -regex -file $FilePath
{
"^\[(.+)\]" # Find Sections
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
"^(;.*)$" # Find Comments
{
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount
$ini[$section][$name] = $value
}
"(.+?)\s*=(.*)" # Find Keys
{
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
之后,你可以读取它的变量,因此,输出这些变量到一个文件或东西。
$iniContent = Get-IniContent “c:\temp\file.ini”
$iniContent[“Applications”]
$value = $iniContent[“Applications”][“app1”]
$iniContent[“Applications”].Keys | %{$iniContent["Applications"][$_]}
https://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91 –
是的。你应该在信贷到期时给予信贷。 – Matt
* for app1我需要将此应用程序的所有数据加载到数据库中*什么是“所有数据”?来自哪里?进入什么数据库?哪张桌子?解析一个INI文件很简单,但是你没有提供足够的信息给任何人甚至猜测如何从那里继续。 –