Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't parse valid RegEx PoSh Function #63

Open
falloutphil opened this issue Apr 24, 2021 · 1 comment
Open

Can't parse valid RegEx PoSh Function #63

falloutphil opened this issue Apr 24, 2021 · 1 comment

Comments

@falloutphil
Copy link

Hi,

The following function returns the error below. Any ideas?

The function works perfectly well in PoSh 5.1 - I got it from an MS blog so it should be "good" PoSh:
https://devblogs.microsoft.com/scripting/use-powershell-to-work-with-any-ini-file/

Thanks,
Phil.

function Get-IniContent ($filePath)
{
    $ini = @{}
    switch -regex -file $FilePath
    {
        ^\[(.+)\] # Section
        {
            $section = $matches[1]
            $ini[$section] = @{}
            $CommentCount = 0
        }
        ^(;.*)$ # Comment
        {
            $value = $matches[1]
            $CommentCount = $CommentCount + 1
            $name = Comment + $CommentCount
            $ini[$section][$name] = $value
        }
        (.+?)\s*=(.*) # Key
        {
            $name,$value = $matches[1..2]
            $ini[$section][$name] = $value
        }
    }
    return $ini
}
Invoke-TokenizeSourceScriptContent : An error occurred; is there invalid PowerShell or some formatting / syntax issue in the script? See error record below.
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
 
Invoke-TokenizeSourceScriptContent : Missing statement block in switch statement clause. Content: , line: 57, column: 13
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
 
Invoke-TokenizeSourceScriptContent : Missing statement block in switch statement clause. Content: , line: 57, column: 17
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
 
Invoke-TokenizeSourceScriptContent : Missing statement block in switch statement clause. Content: , line: 63, column: 11
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
 
Invoke-TokenizeSourceScriptContent : An expression was expected after '('. Content: , line: 63, column: 12
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
 
Invoke-TokenizeSourceScriptContent : Missing closing ')' in expression. Content: , line: 63, column: 12
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
 
Invoke-TokenizeSourceScriptContent : Missing statement block in switch statement clause. Content: , line: 63, column: 12
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
 
Invoke-TokenizeSourceScriptContent : Missing statement block in switch statement clause. Content: , line: 63, column: 15
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
 
Invoke-TokenizeSourceScriptContent : Missing condition in switch statement clause. Content: , line: 63, column: 15
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
 
Invoke-TokenizeSourceScriptContent : Missing closing '}' in statement block or type definition. Content: {, line: 53, column: 1
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
 
Invoke-TokenizeSourceScriptContent : Unexpected token ')' in expression or statement. Content: ), line: 63, column: 15
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
 
Invoke-TokenizeSourceScriptContent : Unexpected token '}' in expression or statement. Content: }, line: 75, column: 5
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
 
Invoke-TokenizeSourceScriptContent : Unexpected token '}' in expression or statement. Content: }, line: 77, column: 1
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
@DTW-DanWard
Copy link
Owner

When I cut and paste the code directly from that web page (saved with VS Code) it works on my machine (tested in Win PowerShell 5.1). If you want to try the tokenize API all by itself (to confirm the error is/isn't happening), there's an example buried in the docs:
https://github.com/DTW-DanWard/PowerShell-Beautifier/blob/master/docs/TokenizeExample.md

I'm guessing you have some type funky hidden character in that page/your copy of the source that it's choking on. If you use VS Code, there's a great extension you should install: Highlight Bad Chars. It does exactly what it sounds like - highlights in red any funky characters, usually Unicode characters like non-breaking white space, etc.
https://marketplace.visualstudio.com/items?itemName=wengerk.highlight-bad-chars

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants