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/2023/10/RESTful-API-User-Guide-v2.2.0.pdf