NinjaRMM - A PowerShell module for NinjaRMM software
I am Mikey O'Toole (@homotechsual) the founder and director for a UK-based managed IT services provider with a passion for automation and good quality MSP software tools.
This is the code for a PowerShell module for the Ninja RMM platform.
The module is written for PowerShell 7. It is not compatible with Windows PowerShell 5.1 and never will be.. This module is licensed under the MIT license.
NinjaRMM provides a PowerShell wrapper around the NinjaRMM API. The module can retrieve and send information to the NinjaRMM API.
This module is published to the PowerShell Gallery and can be installed from within PowerShell with Install-Module
Install-Module NinjaRMM -AllowPrerelease
The first and probably most important requirement for this module is getting it connected to NinjaRMM instance.
-
In your NinjaRMM tenant head to Configuration > Integrations > API.
-
Click on Client App IDs
All going well you should be atconfiguration/integrations/api/client
. -
Click on Add to add a new API application.
-
Set the Application Platform to Web (PHP, Java, .Net Core, etc.)
-
Enter the Name.
For example NinjaRMM API PS Module. -
In the Redirect URIs box enter
http://localhost:9090
You can use any port number here just make sure you remember it and adjust theConnect-NinjaRMM
command below appropriately. -
Set the Scopes appropriately. If you want to be able to do anything using the API select all three scopes. Otherwise select them as appropriate for your needs:
- Monitoring - This provides access to most of the
Get-*
commands. - Management - This provides access to most of the management functionality like agent installer generation.
- Control - This provides access to control settings and make changes to the configuration of you Ninja tenant.
- Monitoring - This provides access to most of the
-
Set the Allowed Grant Types to Authorization Code and Refresh Token. Authorization code is used for the initial login, the refresh token is used to maintain access on subsequent logins.
-
Click on Save and Complete the MFA challenge.
-
Store the Client Secret securely and close the small popup window.
-
Click on Close and on the Client App IDs screen copy the Client ID for your application. Store this with your Client Secret.
-
Install the NinjaRMM PowerShell module on PowerShell 7.0 or above.
Install-Module NinjaRMM -AllowPrerelease
-
Make sure you have all the information you need to connect:
- Your NinjaRMM instance this could be:
- EU
https://eu.ninjarmm.com
- OC
https://oc.ninjarmm.com
- US
https://app.ninjarmm.com
- EU
- Your client ID and client secret.
- The port you chose for your redirect URI.
- Your NinjaRMM instance this could be:
-
Connect to the NinjaRMM API with
Connect-NinjaRMM
# Splat the parameters - easier to read! $ConnectionParameters = @{ Instance = 'eu' ClientID = 'ABCDEfGH-IjKLmnopqrstUV1w23x45yz' ClientSecret = 'abc123abc123def456def456ghi789ghi789lmn012lmn012' Port = 9090 UseWebAuth = $True ShowTokens = $True } Connect-NinjaRMM @ConnectionParameters
- Let's disect those last two parameters:
UseWebAuth
tells the module that we need to login to NinjaRMM to get an authorisation code. This uses a local.NET
Kestrel based webserver that listens on the port you pass and handles the reponse that Ninja sends tohttp://localhost:9090
- the Redirect URI we configured earlier.ShowTokens
tells the module that you want to see the generated refresh token - so that when you next authenticate you can use that refresh token instead of logging in again and generating a new set of tokens.
# Pass the parameters individually - more familiar! Connect-NinjaRMM -Instance 'eu' -ClientID 'ABCDEfGH-IjKLmnopqrstUV1w23x45yz' -ClientSecret 'abc123abc123def456def456ghi789ghi789lmn012lmn012' -Port 9090 -UseWebAuth -ShowTokens
- Let's disect those last two parameters:
-
Connect to the NinjaRMM API with
Connect-NinjaRMM
# Splat the parameters - easier to read! $ReconnectionParameters = @{ Instance = 'eu' ClientID = 'ABCDEfGH-IjKLmnopqrstUV1w23x45yz' ClientSecret = 'abc123abc123def456def456ghi789ghi789lmn012lmn012' RefreshToken = 'abc123abc123defABCDEfGH-IjKLmnopqrstUV1w-23x45yz456def456ghi789ghi7-89lmn012lmn012' UseTokenAuth = $True } Connect-NinjaRMM @ReconnectionParameters
- Let's disect those last two parameters:
RefreshToken
this is the token we get from the initial authorisation code flow login.UseTokenAuth
tells the module that we have a refresh token so we want to bypass the authorisation code steps and just exchange it inside the module for an access token the module can use to authenticate to NinjaRMM.
# Pass the parameters individually - more familiar! Connect-NinjaRMM -Instance 'eu' -ClientID 'ABCDEfGH-IjKLmnopqrstUV1w23x45yz' -ClientSecret 'abc123abc123def456def456ghi789ghi789lmn012lmn012' -RefreshToken 'abc123abc123defABCDEfGH-IjKLmnopqrstUV1w-23x45yz456def456ghi789ghi7-89lmn012lmn012' -UseTokenAuth
- Let's disect those last two parameters: