Basic API example for CPM login and retrieving the list of CPM users using powershell

Basic API example for CPM login and retrieving the list of CPM users using powershell

The below script provides a basic example for the usage of CPM API with Powershell.

Copy the below text into a .ps1 file, replace the text in blue with your relevant text and run the script.

 

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

 

$ApiKey = "Enter_your_API_Key_here"

$your_cpm_address = "https://enter_you_cpm_address"

$tokenobtainurl = -join($your_cpm_address,"/api/token/obtain/api_key/")

$path_to_file = "Insert_full_path_to_where_you_want_to_print_the_users_list_to\users.txt"

 

#The below command stores the access key and refresh key received in the response from CPM

$Response = Invoke-WebRequest -Uri $tokenobtainurl -Method "POST" -Headers @{"Authorization" = "Bearer $NO_AUTH" } -ContentType "application/json" -Body "{`"api_key`":`"$ApiKey`"}" | ConvertFrom-Json

Write-Host "Your Access key is:" $Response.access

Write-Host "Your Refresh key is:" $Response.refresh

 

#Building the Authorization header

$Authorization = -join("Bearer ", $Response.access)

Write-Host "The Authorization Header is:" $Authorization

 

#Example below receives the list of users and stores them into a file on your PC

$userspath = -join($your_cpm_address,"/api/users/")

Invoke-WebRequest -Uri $userspath -Method "GET" -Headers @{"Authorization"="$Authorization"; "Accept"="application/json; version=1.0" } -ContentType "application/json" -OutFile $path_to_file | ConvertFrom-Json

 

For full API documentation, please refer to CPM’s API user guide-

https://n2ws.com/wp-content/uploads/2018/05/RESTful-API-User-Guide-v1.0.pdf


    • Related Articles

    • RestAPI example files

      Background: CPM contain rich RESTful API interface that enable you to do everything you do manually in the UI in an automated way, but it can be difficult to start using the RESTful API. We created this examples to assist customers in understanding ...
    • CPM RESTful API guide for CPM v2.6.0

      Attached to this article is the user guide for CPM RESTful API v1.3 (for CPM v2.6.0 and up). For CPM CLI information see here: https://support.n2ws.com/portal/kb/articles/cli-guide-and-software-download-for-cpm-v2-6-0 You can find the latest version ...
    • How to configure IDP users to have Root/Admin Account Permissions in CPM

      Background: Often administrators are asked to configure logins of  an IDP solution with Cloud Protection Manager CPM. This document will discuss the required configuration steps needed to allow IDP users to easily access CPM GUI and be able to see ...
    • Restful API examples

      The purpose of this article is to help you get started with N2WS Restful API and provide you some examples on how to use it. However, it is not meant to replace the official documentation - for full and accurate information on all the APIs and ...
    • Login to the CPM using an IDP may fail with the "Invalid issuer" error message

      If you're using an IDP, login to the CPM may fail with the following error message: login failed. reason: Invalid issuer in the Assertion/Response (invalid_response): It also may be found in the cpm_server.log: ERROR:  ...