TeamCity 在 powershell 步骤中来自 powershell 脚本的未解析引用

TeamCity Unresolved reference from powershell script on a powershell step

我在 Teamcity 上的一个管道上有下一步

powerShell {
                    name = "Start e2e-container-group build at Azure"
                    executionMode = BuildStep.ExecutionMode.ALWAYS
                    scriptMode = script {
                        content = """
                            try {
                               $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
                               $headers.Add("Authorization", "Basic token")
                               $headers.Add("Content-Type", "application/json")

                               $body = "{
                               `n  `"definitionId`": 7,
                               `n  `"description`": `"Release for E2E-UAT testing`"
                               `n}"

                               Invoke-RestMethod 'https://vsrm.dev.azure.com/company/projectname/_apis/release/releases?api-version=5.1' -Method 'POST' -Headers $headers -Body $body
                            } catch [Exception] {
                                Write-Output ${'$'}_.Exception.Message
                                exit 1
                            }
                        """.trimIndent()
                    }
                }

我在运行连接管道时得到的输出是:

 Kotlin DSL compilation errors
Compilation error Extra\buildTypes\E2ETestsBuild.kt[89:33]: Unresolved reference: headers
Compilation error Extra\buildTypes\E2ETestsBuild.kt[90:33]: Unresolved reference: headers
Compilation error Extra\buildTypes\E2ETestsBuild.kt[91:33]: Unresolved reference: headers
Compilation error Extra\buildTypes\E2ETestsBuild.kt[93:33]: Unresolved reference: body
Compilation error Extra\buildTypes\E2ETestsBuild.kt[98:172]: Unresolved reference: headers
Compilation error Extra\buildTypes\E2ETestsBuild.kt[98:187]: Unresolved reference: body
StackTrace
Load project model
Read build settings from revision c8c0bd16eb07f4165ca37f310d0ee25a69a3d448

该脚本在 powershell 中运行完美,我是否需要在管道上执行任何额外的步骤才能运行?

我知道这个问题已经有将近一年的历史了,但我正在为所有遇到同样情况的人发布一个解决方案。

解决此问题的一种方法是执行以下操作: 运行 来自 TeamCity 的 PowerShell 脚本对字符有一些限制,因此,为了使用这些字符,您需要在字符之间添加一个 ${''} 。通过这些更改,您的脚本可能看起来像这样:

powerShell {
                name = "Start e2e-container-group build at Azure"
                executionMode = BuildStep.ExecutionMode.ALWAYS
                scriptMode = script {
                    content = """
                        try {
                           ${'$'}headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
                           ${'$'}headers.Add("Authorization", "Basic token")
                           ${'$'}headers.Add("Content-Type", "application/json")

                           ${'$'}body = "{
                           `n  `"definitionId`": 7,
                           `n  `"description`": `"Release for E2E-UAT testing`"
                           `n}"

                           Invoke-RestMethod 'https://vsrm.dev.azure.com/company/projectname/_apis/release/releases?api-version=5.1' -Method 'POST' -Headers $headers -Body $body
                        } catch [Exception] {
                            Write-Output ${'$'}_.Exception.Message
                            exit 1
                        }
                    """.trimIndent()
                }
            }