Hallo,
ich entwickle schon einige Jahre mit VB(A), PL/SQL, JScript, VB-Script und Batch. Nun fange ich an mich mit PowerShell zu beschäftigen. Ich habe nun eine Funktion erstellt, mit Werte aus einer INI-Datei zurückliefern soll:
function Get-ConfigINIValue([string]$sINIFilename, [string]$sSection, [string]$sKey){
[string]$sPatternComment = ";*"
[string]$sPatternSectionElse = "[*]*"
[string]$sPatternSectionThis = "[" + $sSection + "]*"
[string]$sPatternKeyThis = $sKey + "=*"
[boolean]$InSection = $false
Write-Output "- IN-Parameters ------"
Write-Output "INIFile: $sINIFilename"
Write-Output "Section: $sSection"
Write-Output "Key : $sKey"
Write-Output "----------------------"
Write-Output "- Pattern ------------"
Write-Output "Comment: $sPatternComment"
Write-Output "SecElse: $sPatternSectionElse"
Write-Output "SecThis: $sPatternSectionThis"
Write-Output "KeyThis: $sPatternKeyThis"
Write-Output "----------------------"
$sContent = Get-Content $sINIFilename
$sRetVal = "not found" # NUR ZU TESTZWECKEN
ForEach ($s in $sContent)
{
Write-Output "$s"
If ($s -like $sPatternComment)
{
# Kommentare ignorieren
Write-Output "... something to ignore"
}
ElseIf ($s -like $sPatternSectionThis)
{
Write-Output "... found my section"
$InSection = $true
}
ElseIf ($s -like $sPatternSectionElse)
{
Write-Output "... found other section"
$InSection = $false
}
ElseIf ($InSection -eq $true)
{
Write-Output "... check the key"
If (($s -like $sPatternKeyThis))
{
Write-Output "... yes, it is"
$sRetVal = $s.SubString($s.IndexOf("=") + 1)
}
}
Else
{
Write-Output "... nothing to do???"
}
}
return $sRetVal
}
Leider verhält sich das If-Konstrukt nicht so, wie ich es möchte. Es wird immer eine Element zu spät in eine If-Block verzweigt?!? Zur Veranschaulichung folgendes Beispiel:
Inhalt INI-Datei:
; abweichender DB-Standort
[Data Source]
SourceDir=\\Test\my\location
[End]
Ausgabe des Scripts:
- IN-Parameters ------
INIFile: C:\Dokumente und Einstellungen\Ifm001\Desktop\Test_Reg.ini
Section: Data Source
Key : SourceDir
----------------------
- Pattern ------------
Comment: ;*
SecElse: [*]*
SecThis: [Data Source]*
KeyThis: SourceDir=*
----------------------
; abweichender DB-Standort
... something to ignore
[Data Source]
... nothing to do???
SourceDir=\\Test\my\location
... found my section
[End]
... check the key
Erwartet hätte ich:
; abweichender DB-Standort
... something to ignore
[Data Source]
... found my section
SourceDir=\\Test\my\location
... check the key
... yes, it is
[End]
... found other section
Findet wer von hier meinen Denkfehler?
Vielen Dank im Vorraus ...