In ausgewählten Hashtable d Ata Modell, müssen Sie wissen, Abschnitt Name (n) zu machen Eigenschaftswert (e) für eine gegebene Eigenschaft Schlüssel; Dieses Datenmodell ist wie folgt:
$y = @{ 'sectionname' = @{ 'propertykey' = 'propertyvalue' } }
Sie könnten einfacher erhalten Sie Zugriff auf Eigenschaftswert (e) für einen bestimmten Eigenschaftsschlüssel mit umgekehrt hashtable (I aus erster Hand zu sagen wagen) Datenmodell:
$x = @{ 'propertykey' = @{ 'sectionname' = 'propertyvalue' } }
Parse-FileIni
Funktion im nächsten Skript-Schnipsel zeigt mögliche Implementierung Letztere (kommentierte zum besseren Verständnis):
Function Parse-FileIni ($file)
{
Function add_ini ($auxadd)
{
if (-not $ini.ContainsKey($name))
{
$ini[$name] = @{}
}
$ini[$name][$section] = $value.Trim()
if ($auxadd) { $aux[$section]++ }
}
$ini = @{} # create output object
$aux = @{} # create properties counter for sections
$section = '[]' # section default name for out-of-sectionproperties
#switch -regex -file $file # garbles ANSI characters above 0x7F
# works for (plain) ASCII, UTF-8 and files with BOM
switch -regex (Get-Content $file)# -Encoding Default (i.e. ANSI)
{
'^\[(.+)\]$' # [section]
{
$section = $matches[1].Trim()
if (-not $aux.ContainsKey($section)) {
$aux[ $section ] = [int] 0
}
}
'^\s*([^#\[;].+?)\s*=\s*(.*)' # 'name=value'; works as well for 'name='
{
$name,$value = $matches[1..2]
add_ini $true
}
'^\s*([^;\[#][^=]+?)$' # name (omitted '=' or '=value')
{
$name = $matches[1].Trim()
$value = ''
add_ini $true
}
}
$name,$value = '','' # add empty sections
$aux.Keys | ForEach-Object {
if ($aux[$_] -eq 0) {
$section = $_
add_ini $false
}
}
$ini # return an object
}
Function Parse-IniFile ($file)
{
$ini = @{}
# Create a default section if none exist in the file. Like a java prop file.
$section = "_NO_SECTION"
$ini[$section] = @{}
#switch -regex -file $file
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
}
'-iniFile-'
$iniFile = ''
$iniFile = Parse-IniFile "d:\test\Google.ini"
$iniFile | Format-Table -AutoSize -Wrap
'-iniFili-'
$iniFili = ''
$iniFili = Parse-FileIni "d:\test\Google.ini"
$iniFili
ich einige Änderungen an der ursprünglichen Parse-IniFile
Funktion richtig gemacht notwendig, um meine Prüfung .ini
Datei zu analysieren; zur Erklärung siehe Kommentare in oben Skript und nächste Google.ini
Datei:
; PSPad configuration file for Google interface - adapted for testing
;; fake properties out of sections
foo="bar"
bubu=
; Search only pages in selected language
[Language]
Czech=lang_cz
English=lang_en
;; misunderstood (fake) use
UK=English
; For search use selected Google server
[GoogleServer]
Česká Republika=www.google.cz
Österreich=www.google.at
UK=www.google.co.uk
;; multiple section declaration: simply merge their properties together
[Language]
German=lang_de
;; second occurrence of a property name: discard and override the first occurrence
Czech=lang_cs
;; fake empty section - somewhere in middle
[Client]
; Search only selected server
[ServerSearch]
;; only keys, no values, missing or present =
pspad.com
php.net=
w3.org
;; fake empty section - at the foot of file
[Protocol]
Ausgabe zeigt den Unterschied:
PS D:\PShell> D:\PShell\SO\41138597_Parse-IniFile.ps1
-iniFile-
Name Value
---- -----
_NO_SECTION {bubu, foo}
GoogleServer {UK, Česká Republika, Österreich}
Client {}
Language {English, German, UK, Czech}
ServerSearch {w3.org, pspad.com, php.net}
Protocol {}
-iniFili-
Name Value
---- -----
bubu {[]}
UK {GoogleServer, Language}
Czech {Language}
w3.org {ServerSearch}
php.net {ServerSearch}
Österreich {GoogleServer}
English {Language}
Česká Republika {GoogleServer}
German {Language}
foo {[]}
pspad.com {ServerSearch}
{Client, Protocol}
PS D:\PShell> $iniFili['UK']
Name Value
---- -----
GoogleServer www.google.co.uk
Language English
PS D:\PShell> $iniFili['UK']['GoogleServer']
www.google.co.uk