Copy template step via powershell api fails

I’m attempting to add a step template to all processes, the “Microsoft Teams - Post a message” template, and I appear to have some kind of encoding problem with the API.
The step is getting added, however when it runs I get a parsing error:

ParserError: At C:\Octopus\Work\20201001142844-70398-335\Script.ps1:1 char:36 
+ [int] = \nif(-not [int]==TryParse(['Timeout'], [ref])) {  = 60 }\n\n  ... 
+                                              ~ 

The ~ is indicating the 1st , in the line.

I’ve attached the entire script- it runs through all projects, removes the named step if it exists, and then adds it back in, here’s what I think are the relevant parts:

I pull the step template via code:
$stepTemplate = (Invoke-RestMethod -Method Get -Uri “$octopusURL/api/actiontemplates/$stepTemplateID” -Headers $header)

Then add the step:
$steps += @{
Actions = @(
Properties = @{
“Octopus.Action.Script.ScriptBody”= “$($stepTemplate.Properties.‘Octopus.Action.Script.ScriptBody’)”

Then I encode and send:
$deploymentProcess.Steps = $steps
$jsonPayload = $deploymentProcess | ConvertTo-Json -Depth 10
Invoke-RestMethod -Method Put -Uri “$octopusURL/api/$($space.Id)/deploymentprocesses/$($project.DeploymentProcessId)” -Headers $header -Body $jsonPayload

I think the problem is not encoding or escaping something in the ScriptBody?

Octopus mass add step example.ps1 (4.3 KB)

Hi @seanl,

Would you by chance be able to either attach a screenshot of the step in the portal with all of its parameters or privately message it to me?

Thanks,
Jeremy

Screenshot sent.

Hi @seanl,

I’m still working on this but I think I need to reach out to someone to get a second pair of eyes. Please feel free to reach out in the meantime. I will update you as the issue progresses.

Thanks,
Jeremy

Hi @seanl,

I have a few questions when you get time.

How did you get the step JSON that you’re attempting to add to the project? Did you add it to one project manually and inspect the payload that was sent to the server?

Is the action template you’re trying to add ActionTemplates-1 when you go and look at the ID?

What does the script body of the step look like once its been imported? Can you take a look at that and let me know?

Thanks,
Jeremy

Yes, I added the template step manually, and inspected the step returned by the API. When I got what looked like a parsing error, I tried retrieving the ScriptBody for the template via API, but that’s not working either.

When I look at the step template in the UI, ActionTemplates-1 is the ID in the URL.


http://SERVER/app#/Spaces-1/library/steptemplates/ActionTemplates-1?activeTab=step

Here’s what the step looks like in the project:

I’ll DM you the project config json.

Hi,

Looking at your deployment process JSON, the script body seems to be heavily truncated.

Would you be able to write-host the JSON you’re putting to the deployment process with your script, then compare that to the JSON payload that is created when you’re manually adding the step via the portal and see if there’s any differences?

Please let me know.

Thanks,
Jeremy

So the short story is it’s fixed, and you can close this ticket.

A previous rendition of my script had the JSON hard coded, and that’s what created the truncated/non-escaped step that you’re seeing.

I then updated the script to retrieve the template step from the API, and use the ScriptBody as a direct copy, and that did fix the step. However, I also consolidated the “delete existing step” logic, and I didn’t notice an error being returned on projects that now only had one step. Powershell conveniently “unboxes” single array items to an object, so while the API was expecting an array of steps, it was getting a single step object.

This broke the script, so some of the projects didn’t get the updated ScriptBody.

Thanks for looking at this.

1 Like

Hey @seanl,

Thanks for the update and the resolution. I could definitely see myself getting tripped up by that logic with 1 step as well. I’m glad to hear you got it going.

I hope you have a great rest of your week.

Thanks,
Jeremy