Finding PowerShell Script Modules within the Octopus API

I use the Octopus API on a regular basis so I’m fairly familiar with the layout. We have an upcoming project that will inject a deployment step in a process (at the very end) and call a custom script module within Octopus to check and validate security settings within web servers as part of the deployment process. Since we have now over 375 Octopus instances, I’m not going to do anything manually whatsoever. The API is the only way to make this change happen for the masses.
Strangely enough, when I started looking at the top-level API, there’s nothing to indicate a script module within Octopus. To get to the API for any Octopus instance is really simple – go to the Octopus URL (and without the /app# portion) and tack on “/api” and you should get a high-level view of all the items available for the API itself. Now, of course, this requires some permissions to view and ultimately access lower levels. Gaining access to the root API is really not that important since Octopus’s permissions apply to the lower levels of the API (which is totally awesome). Below is a sample of what the API looks like for v4.0.3 which I’m currently testing on my laptop.

Um, okay – so what’s the big deal? It’s a weird looking page with a bunch of URLs. Well, these URLs allow you to read (HTTP GET) or add/delete (HTTP POST) via REST with just about anything in the specific Octopus Deploy instance. Anything? Yeah, just about anything. I haven’t had a problem finding anything until today. What am I looking for again? Script Modules – PowerShell modules of code that can be referenced and called in any Octopus deployment process. Here’s what the script modules page looks like in v4.0.3 below.

Hmmmm. Well, this seems simple enough. But.. where.. is.. this.. script.. module.. eh?

So to make a long story short, script modules are part of the LibraryVariables API – specifically the LibraryVariableSet. It wasn’t intuitive from reading the high-level API items. Within that LibraryVariables – both variable sets and script modules live here. There’s a “ContentType” that identifies as “ScriptModule” that has the link to the actual script module itself. Note the “Links” – the Variables link:

So going to /api/variables/variableset-LibraryVariableSets-1 will show me the Script Module in detail. And sure enough, it’s there. You can see in the “Value” field the actual content of my PowerShell script module.

So this little adventure of mine didn’t take too long, but it was certainly a rare instance when I couldn’t find the seemingly obvious thing that I wanted within Octopus. The better way to find API items is to just view the documentation of the API wiki in github here: Specifically, for variablesets, the documentation doesn’t specifically mention Script Modules so at first glance it’s a little confusing.
So I had to hunt down through our actual API to validate our understanding of where the Script Module actually resides within the API, but the behavior and required and optional parameters are clearly spelled out within the documentation here: The documentation spells out what’s required to add/delete (POST) or read (GET) in terms of variablesets which applies to script modules as well – you just needed to know the content type (“ScriptModule”) to query for.
There’s more to view, but nothing to indicate “script” or “module”.



Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment