ServiceMesh AgilityPlatform: Login via PowerShell

Here is a quick script on how to login to ServiceMesh AgilityPlatform. Never heard of the product? don’t bother. It works nevertheless …

function Enter-AgilityPlatform {
PARAM (
  [Parameter(Mandatory = $false, Position = 0)]
  [Alias('Uri')]
  [Uri] $UriBase = 'https://dfch.biz:8443/agility/'
  ,
  [Parameter(Mandatory = $true)]
  [PSCredential] $Credential
) # PARAM
$OutputParameter = $null;
try {
  [Uri] $UriLogin = ("{0}j_spring_security_check" -f $UriBase.AbsoluteUri)
  $Body = 'product={0}&j%5Fusername={1}&j%5Fpassword={2}' 
    -f 'AgilityPlatform', [System.Web.HttpUtility]::UrlEncode($cred.Username), 
	[System.Web.HttpUtility]::UrlEncode($cred.GetNetworkCredential().Password);
  
  $r = Invoke-RestMethod -Uri $UriLogin -Method POST -SessionVariable s 
    -Body $Body -ContentType 'application/x-www-form-urlencoded';

  $r = Invoke-RestMethod -Uri ("{0}connection" -f $Uri.AbsoluteUri ) -WebSession $s;
  if($r) {
    $OutputParameter = $s;
  } # if
} # try
catch {
  Write-Error ("Login to '{0}' with Username '{1}' FAILED [{2}]." 
    -f $UriBase, $cred.Username, $_)
} # catch
finally {
  # Cleanup
  # N/A
} # finally
return $OutputParameter;
} # function

Subsequent calls to AgilityPlatform are then execeuted like this (eg list all projects on the platform):

# User to login
$cred = Get-Credential "UberAdmin";
# Specify base URI
[Uri] $UriBase = 'https://example.com/agility/'
# Select API version
[Uri] $Uri = "{0}api/current/" -f $UriBase.AbsoluteUri;
$s = Enter-AgilityPlatform -UriBase $UriBase -Credential $cred;
$r = Invoke-RestMethod -Uri ("{0}project" -f $Uri.AbsoluteUri ) -WebSession $s;
$r.Linklist.link

An easy way to check if the login session is still valid is to issue the following request:

$r = Invoke-RestMethod -Uri ("{0}connection" -f $Uri.AbsoluteUri ) -WebSession $s;
$r.Linklist.ns1

You should receive a namespace in ‘ns1’ like ‘http://servicemesh.com/agility/api’ or an exception if the login session is already expired.

If you happen to get an exception upon using UrlEncode() make sure you issue the following statement in your PowerShell session first:

Add-Type -AssemblyName System.Web;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: