Backup NinjaOne PowerShell Automation Scripts

If you’ve used NinjaOne RMM for any length of time, you’ve probably accumulated a large number of mission critical custom Powershell scripts and automations.  If you’ve ever looked into source control or backing up of these scripts, you’ve probably discovered that no solutions currently exist.  The closest you have is to set up something like Github Enterprise, host your scripts there and then set up stub scripts inside of Ninja.  This of course adds another layer of complexity and more importantly another point of failure.  Until such time as NinjaOne implements a proper source control solution, we’re left to fend for ourselves.

If nothing else, I want to be able to download all of our scripts on a regular basis for backups, a poor mans source control and to ensure we retain access to our intellectual property.

I sat down today to try and find a way to automate backing up the scripts and I’m pleased to report I found a solution.

There is an API endpoint for scripts but unfortunately it only tells you the NAME of script, not the body.  My workaround to this in the script below is to use the API to generate a list of all the scripts in our environment and their associated index IDs.  Then use invoke-webrequest to pretend to be a human and connect to one script URL after the other and copy the script contents.  To do this requires a valid and current session key.  You can grab this from the Dev tools of your favorite browser after signing in to Ninja.

The script below is rough around the edges as I just made it for me to solve an immediate problem.  But it does work so feel free to build on it as required.

The script will download all scripts and meta data and save them into a dated folder.  It will keep 10 folders and delete any older backups by default.

 

Leave a Comment