缺少语句块切换导致脚本错误输出

时间:2015-05-21 21:07:16

标签: powershell tfs

我正在编写这个PowerShell脚本,用于自动化TFS部署。以下是在switch语句子句中抛出错误Missing语句块的脚本部分。语法对我来说是正确的,但我似乎无法解决错误。有什么想法吗?

function core ([string]$EnVar)
    {

# Set the build parameters
    $params="environment="+$EnVar+";SQLServer="+$SQLServer+";IISServer="+$IISServer+";DBName="+$DBName

    write-output $params >> $OutputFile

    C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe $DeploymentScript /p:$params >> $OutputFile 2>&1

    write-output $params >> $OutputFile
    write-output $LastExitCode >> $OutputFile

if ($LastExitCode -ne 0)
{
    write-output "An error has occured." >> $OutputFile
    $outline = "Updating build quality to Failed " + $Environment + " Deployment." 
    write-output $outline >> $OutputFile

    switch ($EnVar)
    {
        "Test" {$build.Quality = "Failed Test Deployment"}
        "Stage" {$build.Quality = "Failed Stage Deployment"}
        "Prod" {$build.Quality = "Failed Production Deployment"}
        default {$build.Quality = "Rejected"} 
    }
    $build.Save()

     $cmdLine = "/ID 2 /L ""Operation Logs"" /T ERROR /SO $tfsProject.DeployTo$EnVar /D ""($buildNumber) deployed by ($requestedby) FAILED! Deployment log file: $OutputFile """
    invoke-expression "$tool $cmdline"
    write-output "Sending failure email." >> $OutputFile
    $to = $emailRequestedBy
    $body = "<html>Deployment log file: """+ $OutputFile + """</html>"
    $subject = $tfsProject + " " + $EnVar + " Deployment failed"
    send-SMTPmail -to $to -from "tfsservice@vistex.com" -subject $subject -html -body $body
    exit(1)
}
else 
{
    $outline = "Successfully deployed to " + $EnVar + "."
    write-output $outline >> $OutputFile
    $outline = "Updating build quality to Deployed to " + $EnVar + "."
    write-output $outline >> $OutputFile

    switch ($EnVar)
    {   
        "Test" ($build.Quality = "Deployed to Test"}
        "Stage" {$build.Quality = "Deployed to Stage"}
        "Prod" {$build.Quality = "Deployed to Production"}
        default {$build.Quality = "Rejected"} 
    }

    $build.Save()
    $cmdLine = "/ID 1 /L ""Operation Logs"" /T SUCCESS /SO $tfsProject.DeployTo$EnVar /D ""($buildNumber) deployed by $requestedby successfully finish. Deployment log file: $OutputFile """
    invoke-expression "$tool $cmdline"
    write-output "Sending success email." >> $OutputFile
    $to = $emailRequestedBy
    $body = "<html>Deployment log file: """+ $OutputFile + """</html>"
    $subject = $tfsProject + " " + $EnVar + " Deployment successfully completed"
    send-SMTPmail -to $to -from "tfsservice@vistex.com" -subject $subject -html -body $body
} 
}

1 个答案:

答案 0 :(得分:2)

发现了这个问题。这是一个错字...在&#34;测试&#34;中有一个(而不是{。情况下:

else 
{
    $outline = "Successfully deployed to " + $EnVar + "."
    write-output $outline >> $OutputFile
    $outline = "Updating build quality to Deployed to " + $EnVar + "."
    write-output $outline >> $OutputFile
    switch ($EnVar)
    {   
        "Test" ($build.Quality = "Deployed to Test"}
        "Stage" {$build.Quality = "Deployed to Stage"}
        "Prod" {$build.Quality = "Deployed to Production"}
        default {$build.Quality = "Rejected"} 
    }

将该行更新为:

        "Test" {$build.Quality = "Deployed to Test"}