Difference between revisions of "Management API - Command Line Client"

[unchecked revision][unchecked revision]
Line 62: Line 62:
  
 
<source lang="powershell" smart-tabs="true" toolbar="false" gutter="false">
 
<source lang="powershell" smart-tabs="true" toolbar="false" gutter="false">
 +
#This script send an email when there are less then a certain amount of successful jobs during a given timeperiod
 +
 +
#path to MailStoreManagementCmd.exe
 +
$cmdpath = "C:\Program Files\MailStore Infrastructure\MailStoreManagementCmd.exe"
 +
#IP address or hostname of SPE management server
 +
$speserver = "localhost"
 +
#admin credentials
 +
$speuser = "admin"
 +
$spepassword = "password"
 +
#time zone of the server
 +
$spetimezone = "W. Europe Standard Time"
 +
#instance id
 +
$instanceID = "test"
 +
#profile id of the archiving profile to monitor, press Ctrl+Shift+P when the profile is selected to se its details
 +
#use the API command GetProfiles or import-list in the management shell to get the same
 +
$profileID = 1
 
#time in hours, results of the last half hour are evaluated
 
#time in hours, results of the last half hour are evaluated
 
$timespan = 0.5
 
$timespan = 0.5
 +
#amount of jobs that must be succesful
 +
$jobcount = 5
 +
 +
#email server settings for sending notifications
 +
$EmailFrom = “mailstore@domain.eu”
 +
$EmailTo = “administrator@domain.eu”
 +
$Subject = “MailStore SPE Error”
 +
$Body = “Please check 'Recent Results' of MailStore SPE instance '” + $instanceID +"'"
 +
$SMTPServer = “localhost”
 +
$SMTPPort = 25
 +
$SMTPusessl = $false
 +
 +
$SMTPuser = "username"
 +
$SMTPPassword = "password"
 +
 +
#usually no changes should be made below this line
 +
 
$format = "yyyy-MM-ddTHH:mm:ss"
 
$format = "yyyy-MM-ddTHH:mm:ss"
 
+
 
$end = Get-Date -Format $format
 
$end = Get-Date -Format $format
 
$start = (Get-Date).AddHours(-$timespan).ToString($format)
 
$start = (Get-Date).AddHours(-$timespan).ToString($format)
 
+
 
#echo $start $end
 
#echo $start $end
 
+
$resultset =  &"C:\Program Files\MailStore Infrastructure\MailStoreManagementCmd.exe" -s localhost -u admin -p password -t * -c GetWorkerResults --instanceID=test01 --fromIncluding=$start --toExcluding=$end --timeZoneID="W. Europe Standard Time"
+
$resultset =  &$cmdpath -s $speserver -u $speuser -p $spepassword -t * -c GetWorkerResults --instanceID=$instanceID --fromIncluding=$start --toExcluding=$end --timeZoneID=$spetimezone
 
+
 
$resultobj = (ConvertFrom-Json ([String]$resultset))
 
$resultobj = (ConvertFrom-Json ([String]$resultset))
#filter for profile ID 9 and successful jobs only
+
#filter for profileID and successful jobs only
$reduced = ($resultobj.result | where { $_.result -eq "succeeded" -and $_.profileId -eq 9 } )
+
$reduced = ($resultobj.result | where { $_.result -eq "succeeded" -and $_.profileId -eq $profileID } )
 
+
#when there are more than 5 jobs...
+
#when there are more than $jobcount jobs...
if ($reduced.length -gt 5){
+
if ($reduced.length -gt $jobcount){
 
     echo "succesful"}
 
     echo "succesful"}
 
else{
 
else{
     echo "not succesful"
+
     $SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
 +
    $SMTPClient.EnableSsl = $SMTPusessl
 +
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SMTPuser, $SMTPPassword);
 +
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
 
     }
 
     }
 
</source>
 
</source>

Revision as of 15:34, 27 January 2015

Besides the option to develop your own client for the Management API in any arbitrary programming or scripting language, MailStore Service Provider Edition provides a simple to use command line client in the form of the MailStoreManagementCmd.exe executable to be used in simple batch scripts or by backup software to remote control instances.

The syntax of the MailStoreManagementCmd.exe looks as follows:

  MailStoreManagementCmd.exe -s <server> -u <user> -p <password> -t <thumbprint> -c <command> [-param1 <value> -param2 <value> ...]

The below command line parameters are required followed by additional API command parameters if necessary.

Parameter Description
-s IP address or host name of the Management Server to connect to.
-t Thumbprint of the SSL certificate used by the Management Server. Use * to accept any certificate.
-u Username of a MailStore Service Provider Edition Administrator
-p Password of the MailStore Service Provider Edition Administrator
-c API command to be executed.

The output of the executed API command is returned to the standard output (STDOUT) comparable to the HTTP bodies as described in Using the API. Therefore a JSON parser is recommended for further processing of the output.

MailStoreManagementCmd.exe itself exits with error code 0 if no errors occurred, otherwise the exit code 1 is returned.

Examples

Backup Pre- and Post-Scripts

MailStoreManagementCmd.exe -s localhost -u admin -p password -t 46AD1743B61B1F7B4AD3FBB8DC24498953BA3DA7 -c FreezeInstances --instanceFilter="instanceFilter"
MailStoreManagementCmd.exe -s localhost -u admin -p password -t 46AD1743B61B1F7B4AD3FBB8DC24498953BA3DA7 -c ThawInstances --instanceFilter="instanceFilter"

String Definition

instanceFilter

Value Affects
instance01 instance02 instance03 instance01, instance02 and instance03
* All instances
*instancehost01 *instancehost02 All instances on instancehost1 and instancehost2
* .running All running instances
* .safeMode All instances running in Safe Mode
* .safeMode .running All instances running in regular or Safe Mode
* .stopped All stopped instances

Other Examples

  MailStoreManagementCmd.exe -s localhost -u admin -p password -t 46AD1743B61B1F7B4AD3FBB8DC24498953BA3DA7 -c GetEnvironmentInfo
 MailStoreManagementCmd.exe -s localhost -u admin -p password -t 46AD1743B61B1F7B4AD3FBB8DC24498953BA3DA7 -c GetProfiles -instanceID test01 -raw true

Powershell

You are able to parse the result in the Powershell by converting the returned JSON values into Powershell objects

(ConvertFrom-Json ([String](.\MailStoreManagementCmd.exe -s localhost -u admin -p password -t 46AD1743B61B1F7B4AD3FBB8DC24498953BA3DA7 -c CreateLicenseRequest))).result.instanceData | where { $_.usedLicenseCount }

Monitoring archiving jobs

#This script send an email when there are less then a certain amount of successful jobs during a given timeperiod

#path to MailStoreManagementCmd.exe
$cmdpath = "C:\Program Files\MailStore Infrastructure\MailStoreManagementCmd.exe"
#IP address or hostname of SPE management server
$speserver = "localhost"
#admin credentials
$speuser = "admin"
$spepassword = "password"
#time zone of the server
$spetimezone = "W. Europe Standard Time"
#instance id
$instanceID = "test"
#profile id of the archiving profile to monitor, press Ctrl+Shift+P when the profile is selected to se its details
#use the API command GetProfiles or import-list in the management shell to get the same
$profileID = 1
#time in hours, results of the last half hour are evaluated
$timespan = 0.5
#amount of jobs that must be succesful
$jobcount = 5

#email server settings for sending notifications
$EmailFrom = mailstore@domain.eu
$EmailTo = administrator@domain.eu
$Subject = MailStore SPE Error
$Body = Please check 'Recent Results' of MailStore SPE instance '” + $instanceID +"'"
$SMTPServer = “localhost”
$SMTPPort = 25
$SMTPusessl = $false

$SMTPuser = "username"
$SMTPPassword = "password"

#usually no changes should be made below this line

$format = "yyyy-MM-ddTHH:mm:ss"
 
$end = Get-Date -Format $format
$start = (Get-Date).AddHours(-$timespan).ToString($format)
 
#echo $start $end
 
$resultset =  &$cmdpath -s $speserver -u $speuser -p $spepassword -t * -c GetWorkerResults --instanceID=$instanceID --fromIncluding=$start --toExcluding=$end --timeZoneID=$spetimezone
 
$resultobj = (ConvertFrom-Json ([String]$resultset))
#filter for profileID and successful jobs only
$reduced = ($resultobj.result | where { $_.result -eq "succeeded" -and $_.profileId -eq $profileID } )
 
#when there are more than $jobcount jobs...
if ($reduced.length -gt $jobcount){
    echo "succesful"}
else{
    $SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
    $SMTPClient.EnableSsl = $SMTPusessl
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SMTPuser, $SMTPPassword);
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
    }