Using Windows Powershell to Accsess Cisco Meeting Server Rest-API

Using google Postman to access Cisco Meeting Server Rest-API is the most common way to configure the server.
But it can be time consuming with lot's of cut and paste of ID's and such.

So I though I'd try to acess the CMS api via powershell.
It was actually surprisingly simple.

First I found a function to ignore self signed certificates.

function Ignore-SelfSignedCerts
{
    try
    {
        Write-Host "Adding TrustAllCertsPolicy type." -ForegroundColor White
        Add-Type -TypeDefinition  @"
        using System.Net;
        using System.Security.Cryptography.X509Certificates;
        public class TrustAllCertsPolicy : ICertificatePolicy
        {
             public bool CheckValidationResult(
             ServicePoint srvPoint, X509Certificate certificate,
             WebRequest request, int certificateProblem)
             {
                 return true;
            }
        }
"@

        Write-Host "TrustAllCertsPolicy type added." -ForegroundColor White
      }
    catch
    {
        Write-Host $_ -ForegroundColor "Yellow"
    }

    [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
}

Ignore-SelfSignedCerts

I then ran this command to set my credentials for the CMS API-user.
$basicAuth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("apiuser:MyVerySecurePassword")))

Editors note: It seems somehow that the next line was needed to get the proper autorisation
$Cred = Get-Credential

Just to test the output I ran a GET to find cospaces in CMS

$response = Invoke-RestMethod -Method GET -Uri "https://meetingserver.mylab.com/api/v1/cospaces" -Credential $Cred -headers @{Authorization=("Basic {0}" -f $basicAuth)}

These few lines can be used as an example to automate configuration etc via Powershell so that you don't have to do this manually in Postman.

An example of use is to export all the cospaces to file:
Not beautiful, but it will do the job.
Note that the CMS only returns max 20 cospaces (rows) at a time so that is why I added the offset parameter.

Invoke-RestMethod -Method GET -Uri "https://myserver.test.com/api/v1/cospaces" -Credential $Cred -headers @{Authorization=("Basic {0}" -f $basicAuth)} -Outfile cospace_0.xml
foreach($i in 1..50){
    $step =20*$i
Write-Host $step
Invoke-RestMethod -Method GET -Uri "https://198.18.16.10/api/v1/cospaces?offset=$step" -Credential $Cred -headers @{Authorization=("Basic {0}" -f $basicAuth)} -Outfile "cospace_$step.xml"
}
Get-Content cospace*.xml | Set-Content cospace_export.xml

Hope it can be useful for somebody out there.

You can read more about the Invoke-RestMethod here.

Comments

Popular Posts