Using powershell to make API calls

Solved
D
Douglas
FGL Sports Ltd

Looking to create device groups (a few hundred) from a list in a text file.

I'm most familiar with powershell, but I've not worked with API's or security tokens before.

Does anyone have a template or sample scripts that they would be willing to share?

7 years ago
SOTI MobiControl
ANSWERS
TB
TJ Bukoski
7 years ago (edited 7 years ago)

Sure,

$MCUsername = "MobiControl username goes here"
$MCPassword = "MobiControl Password goes here"
$MCFQDN = "MobiControl FQDN Goes here"
$ClientID = "MobiControl API Client ID Goes here"
$ClientSecret = "MobiControl Client Secret Goes here"
# You may want to figure out a way to encrypt the Password and the Secret, that's best practice

$IDSecret = $ClientID + ":" + $ClientSecret 
$EncodedIDSecret = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($IDSecret))

$Body = @{}
$Body["grant_type"]="password"
$Body["username"]=$MCUsername
$Body["password"]=$MCPassword

$Header = @{}
$Header["Authorization"] = "Basic " + $EncodedIDSecret

try
{
$response = Invoke-restmethod -Uri  https://$MCFQDN/mobicontrol/api/token -Method POST -Headers $Header -Body $Body
}
catch
{
$($_.Exception.Message)
}

$Token = $response.access_token

Write-Host "My MobiControl API Token is: $Token"

#Now you can use the Token in other API calls by refering to the Token Variable.

SOTI takes no responsibility for any malignant API calls you make against your own system. Use any script on the forums at your own risk.

Solution
D
Douglas
7 years ago

With a great deal of help from TJ Bukoski, I was able to hack together three scripts to create directories in mass.

This is NOT a safe, robust, or "good" example of powershell scripting, and should not be used in production. I am only posting it so that people who would like to learn more have a place to start from. I hope to update it soon with an extra script that will ask for the passwords and client secrets in a secure fashion. NEVER hard code these into your scripts.

The main script that calls the other two, much of it borrowed from someone on stackoverflow.com, MakeIt.ps1:

#Call the Token generation script
. .\Token.ps1
#returns the $Token variable that can be used in this and other child scripts
GetToken

# File with list of new folder names
$txtFile="Path to text file holding the folder names";
# Pattern that lines must match   
$pattern="\d+";                                                             

#loop through each line in the text file and use the value to call the script that makes the calls to the API. Passes the token key and directory name
get-content $txtFile | %{

    if($_ -match $pattern)
    {
        $Name = $_
        #Call the API Script
        . .\AddDirs.ps1
        #Exicute the function with the 2 required variables
        CreateDir $Name $Token
       
    }
}

The Token script that TJ supplied as a function, Token.ps1:

function GetToken
{

   $MCUsername = "MobiControl username goes here"
   $MCPassword = "MobiControl Password goes here"
   $MCFQDN = "MobiControl FQDN Goes here"
   $ClientID = "MobiControl API Client ID Goes here"
   $ClientSecret = "MobiControl Client Secret Goes here"


    # You may want to figure out a way to encrypt the Password and the Secret, that's best practice

    $IDSecret = $ClientID + ":" + $ClientSecret
    $EncodedIDSecret = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($IDSecret))

    $Body = @{}
    $Body["grant_type"]="password"
    $Body["username"]=$MCUsername
    $Body["password"]=$MCPassword

    $Header = @{}
    $Header["Authorization"] = "Basic " + $EncodedIDSecret

    try
    {
    $response = Invoke-restmethod -Uri  https://$MCFQDN/mobicontrol/api/token -Method POST -Headers $Header -Body $Body
    }
    catch
    {
    $($_.Exception.Message)
    }

    $Token = $response.access_token

    Write-Host "My MobiControl API Token is: $Token"
}

And the function that calls the API to create the folders, AddDirs.ps1:

function CreateDir ([string]$Name, [string]$Token)
{
    $Path = "Root path to where you want to create the folders" #eg. "\\root\subfolder\"
    $devicegroup = @{}
    $devicegroup["Name"] = $Name
    $devicegroup["Path"] = $Path + $Name
    $devicegroup["Icon"] = "Red"
    $devicegroup["Kind"] = "Regular"
    $devicegroup = $devicegroup | ConvertTo-Json

    $Header2 = @{}
    $Header2["Authorization"] = "Bearer " + $Token

    try
    {
    $response2 = Invoke-restmethod -Uri  https://$MCFQDN/mobicontrol/api/devicegroups -ContentType "application/json" -Method POST -Body $devicegroup -Headers $Header2
    }
    catch
    {
    $($_.Exception.Message)
    }
}

BL
Benedict Lumabi
7 years ago

I'm new to powershell, I tried the script on our instance using powershell.

I copied/paste the script, but i receive error when i run it:

Write-Host "My MobiControl API Token is: $Token"
The underlying connection was closed: An unexpected error occurred on a receive.
My MobiControl API Token is:



I'm new to powershell . can you help me?

D
Douglas
7 years ago

Sorry Benedict, that's not enough to go on.

You literally just cut and pasted them? The scripts are just examples, you need to edit them to fit your own environment along with the proper APK agent credentials.  Have you installed the APK agent and tested using the web GUI?