您可以像使用散列一樣處理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"][$_]}
* for app1我需要將此應用程序的所有數據加載到數據庫中*什麼是「所有數據」?來自哪裏?進入什麼數據庫?哪張桌子?解析一個INI文件很簡單,但是你沒有提供足夠的信息給任何人甚至猜測如何從那裏繼續。 –