2
votes

i'm using the next function to parse one ini file.

I have one problem that i don't know how to solve:

If one match is found, break loop and read next line.

I know that i can put break to do this. But, break exit of all switch code. ¿?

I suppose it's a special scenario when using switch with regex to parse a file.

¿How can i do to not parse same line in two code blocks?

See in the output:

`Comment ; Parameters that are strings are specified parameter=string, like this:`
`Key ; Parameters that are strings are specified parameter=string, like this:`

and

`Comment ; Parameters that are numbers are specified parameter=number, like this:` 
`Key ; Parameters that are numbers are specified parameter=number, like this:`

Thanks

    function Get-IniContent ($filePath)  
    {  
        $ini = @{}   
        switch -regex -file $FilePath   
        {   
            "^\[(.+)\]$" # Section   
            {   
                $section = $matches[1]   
                $ini[$section] = @{}   
                $CommentCount = 0   
                Write-Host "Section $section"  
            }   
            "^(;.*)$" # Comment   
            {   
                if (!($section))   
                {   
                    $section = "No-Section"   
                    $ini[$section] = @{}   
                }   
                $value = $matches[1]   
                $CommentCount = $CommentCount + 1   
                $name = "Comment" + $CommentCount   
                $ini[$section][$name] = $value   
                Write-Host "Comment $value"  
            }    
            "(.+?)\s*=\s*(.*)" # Key   
            {   
                if (!($section))   
                {   
                    $section = "No-Section"   
                    $ini[$section] = @{}   
                }   
                $name,$value = $matches[1..2]   
                $ini[$section][$name] = $value   
                Write-Host "Key $name=$value"  
            }   
            default  
            {              
                # Next line causes NullArray error  
                #$line=$matches[1]  
                Write-Host "Strange line $line"  
            }  
        }   

        return $ini  
    }  

    $iniFile=Get-IniContent (Join-Path (Split-Path -parent $MyInvocation.MyCommand.Path) "test.config")   

This it's a sample ini file:

Independent Bad Line
timer=19
[Common]
; 4 - default value hard-coded in ntfs-hardlink-backup.ps1 script

; Blank lines also do no harm

; Parameters that are strings are specified parameter=string, like this:
backupDestination=X:\Backup
; Parameters that are numbers are specified parameter=number, like this:
backupsToKeep=20

[server-01.mycompany.example.org]
; Parameters that are specific to a particular server/computer go in a section for that computer.
; The section name is the fully-qualified domain name (FQDN) of the computer.
backupSources=D:\Shares\Admin,E:\Shares\Finance,E:\Shares\ICT,D:\Shares\Users
:Bad Line
=10 ; Also Bad Line

And this it's script output:

E:\Config\NtfsBackup>powershell -ExecutionPolicy unrestricted -file "E:\Config\NtfsBackup\nt1.ps1" 
Strange line 
Key timer=19
Section Common
Comment ; 4 - default value hard-coded in ntfs-hardlink-backup.ps1 script
Strange line 
Comment ; Blank lines also do no harm
Strange line 
Comment ; Parameters that are strings are specified parameter=string, like this:
Key ; Parameters that are strings are specified parameter=string, like this:
Key backupDestination=X:\Backup
Comment ; Parameters that are numbers are specified parameter=number, like this:
Key ; Parameters that are numbers are specified parameter=number, like this:
Key backupsToKeep=20
Strange line 
Section server-01.mycompany.example.org
Comment ; Parameters that are specific to a particular server/computer go in a section for that computer.
Comment ; The section name is the fully-qualified domain name (FQDN) of the computer.
Key backupSources=D:\Shares\Admin,E:\Shares\Finance,E:\Shares\ICT,D:\Shares\Users
Strange line 
Strange line 
1

1 Answers

3
votes

The answer is simple. You do not want to use Break but instead use Continue which will stop the loop for the current item and start the loop for the next item. Placing Continue at the end of all of your script blocks (probably on a new line just after the Write-Host line) for the switch will do what you need.

function Get-IniContent ($filePath)  
{  
    $ini = @{}   
    switch -regex -file $FilePath   
    {   
        "^\[(.+)\]$" # Section   
        {   
            $section = $matches[1]   
            $ini[$section] = @{}   
            $CommentCount = 0   
            Write-Host "Section $section"
            Continue
        }   
        "^(;.*)$" # Comment   
        {   
            if (!($section))   
            {   
                $section = "No-Section"   
                $ini[$section] = @{}   
            }   
            $value = $matches[1]   
            $CommentCount = $CommentCount + 1   
            $name = "Comment" + $CommentCount   
            $ini[$section][$name] = $value   
            Write-Host "Comment $value"  
            Continue
        }    
        "(.+?)\s*=\s*(.*)" # Key   
        {   
            if (!($section))   
            {   
                $section = "No-Section"   
                $ini[$section] = @{}   
            }   
            $name,$value = $matches[1..2]   
            $ini[$section][$name] = $value   
            Write-Host "Key $name=$value"  
            Continue
        }   
        default  
        {              
            # Next line causes NullArray error  
            #$line=$matches[1]  
            Write-Host "Strange line $line"  
        }  
    }   

    return $ini  
}  

$iniFile=Get-IniContent (Join-Path (Split-Path -parent $MyInvocation.MyCommand.Path) "test.config")