Unresolved reference during step with powershell.
Completed
I
have the next step on one of my pipelines on 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()
}
}
The output I get when running the pipeline is:
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
The script is working perfectly in powershell, is there any extra steps I have to do to run this on the pipeline?
Please sign in to leave a comment.
In Kotlin, $something will be treated as a string template: https://kotlinlang.org/docs/reference/basic-types.html#string-templates


Therefore, PowerShell variables are treated as references to existing variables which are never defined in the script body.
There are two workarounds I would like to suggest:
1) you could store the script in file and set the step up to use file as a script source;
2) you could escape the $ character with ${'$'} - on compilation this will ensure end script is saved as intended. TeamCity already does it when versioned settings are saved from server to VCS:
UI representation
DSL code