Daft PowerShell Error: MissingEndCurlyBrace

I made a fairly silly Powershell mistake this morning. I blame low levels of caffeine.

I was amending an old function I’d written which lists all the sqlservers, prompts me to ask which one I want to work on and then sets the $ServerInstance variable. It’s essentially a Powershell/Sqlserver equivalent to the Unix/Oracle .oraenv.

Anyway the lines I wanted to add in were:

$Global:ComputerName = $Global:ServerInstance.split("")[0]
write-verbose "`$Global:ComputerName: $Global:ComputerName"
write-output "`$Global:ComputerName: $Global:ComputerName"
write-output "`$Global:ServerInstance: $Global:ServerInstance"

When I tried to load the function this happened

PS C:Windowssystem32> . \server01d$dbaworkmattfunctionsfunction-sqlenv.ps1
At \server01d$dbaworkmattfunctionsfunction-sqlenv.ps1:15 char:1
+ {
+ ~
Missing closing '}' in statement block.
At \server01d$dbaworkmattfunctionsfunction-sqlenv.ps1:52 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingEndCurlyBrace

I did the usual stuff I’d do if I get ‘missing bracket’ error in any language, i.e.

  • checked all the brackets are matched up

  • checked I haven’t left an un-closed quote or double quote

..but it was neither of these.

What I’d actually done is put code between the ‘Process’ segment and the ‘End’ segment.

I don’t usually use the Begin-Process-End construct, to be honest, and it’s not appropriate here anyway, but I did learn something!

The code, WITH THE MISTAKE is below. My working version of the code is here.

<#
.Synopsis
   Sets $ServerInstance and $ComputerName
.DESCRIPTION
   Sets $ServerInstance and $ComputerName

   Todo: Could append default if appropriate

.EXAMPLE
   Example of how to use this cmdlet
.EXAMPLE
   Another example of how to use this cmdlet
#>
function sqlenv
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Param1 help description
        $ServerInstance = "$Global:ServerInstance"
    )

    Begin
    {
    }
    Process
    {
        write-verbose "`$Global:ServerInstance: $Global:ServerInstance"
    smi
        write-verbose "Calling read-host"
    $NewServerInstance = read-host "ServerInstance = [$ServerInstance]"
        write-verbose "Called read-host"
        write-verbose "`$NewServerInstance: $NewServerInstance"
    if ($NewServerInstance)
        {
            write-verbose "In if..."
            $Global:ServerInstance = "$NewServerInstance"
            write-verbose "`$Global:ServerInstance: $Global:ServerInstance"
        }
        write-verbose "`$Global:ServerInstance: $Global:ServerInstance"


    }
    $Global:ComputerName = $Global:ServerInstance.split("")[0]
    write-verbose "`$Global:ComputerName: $Global:ComputerName"

    write-output "`$Global:ComputerName: $Global:ComputerName"
    write-output "`$Global:ServerInstance: $Global:ServerInstance"
    End
    {
    }
}
set-alias oraenv sqlenv

Image: Detail from Perspective View, from the N.E. of the Cathedral and Close of Salisbury showing the Old Belfry. Drawn by Gio Beatt Jackson. Engraved by John Fougerson. Published by Edward Easton, Bookseller at Salisbury Aug 8th 1761 Wiltshire archaeological and natural history ma…

Advertisements