Here is a quick example.
Set up the connection details.
# SERVER CONNECTION DETAILS
$API = @{};
$APIHost = 'localhost:9998';
$HTTPS = $false;
$API = @{
'authUserName' = 'admin'
'authPassword' = 'admin'
'Method' = 'POST'
'ContentType' = 'application/json'
'URI' = 'http' + $(if ($HTTPS -eq $true ) {'s'}) + '://' + $APIHost + '/'
};
I set up several functions that get used over and over. Here are the main ones
function UpdateURImethod ( $a, $b ) { $API.uri = 'http' + $(if ($HTTPS -eq $true ) {'s'}) + '://' + $APIHost + '/' + $b; $API.Method = $a; }
function PrimaryAuth {
$APIAuth = @{};
$APIAuth = @{
'Uri' = 'http' + $(if ($HTTPS -eq $true ) {'s'}) + '://' + $APIHost + '/api/v1/auth/authenticate-user';
'Body' = '{"username":"' + $API.authUserName + '","password":"' + $API.authPassword + '","language":null,"twoFactorCode":""}';
};
$Auth = Invoke-RestMethod -Uri $APIAuth.Uri -ContentType $API.ContentType -Body $APIAuth.Body -Method $API.Method;
# SERVER ADMIN AUTH TOCKEN
$API.Remove('Headers');
$API.Add('Headers', @{ 'Authorization' = "Bearer $($Auth.accessToken)" });
# REFRESH
$API.Remove('Refresh');
$API.Add('Refresh' , @{"token" = $($Auth.refreshToken)});
# EXPERATION
$API.Remove('accessTokenExpiration');
[DateTime]$ATExp = $Auth.accessTokenExpiration;
$API.Add('accessTokenExpiration', $ATExp);
return $Auth;
};
function ImpersonateUser ($user_email) {
$URI = $('http' + $(if ($HTTPS -eq $true ) {'s'}) + '://' + $APIHost + '/api/v1/settings/domain/impersonate-user/' + $user_email );
$Impersonate = Invoke-RestMethod -Method POST -Uri $URI -ContentType $API.ContentType -Headers $API.Headers;
# IMPERSONATE AUTH TOCKEN
$API.Remove('HeaderImpersonate');
$API.Add('HeaderImpersonate', @{ 'Authorization' = "Bearer $($Impersonate.impersonateAccessToken)" });
$API.ImpersonateObject=$Impersonate;
if ($API.ShowAuthData) {return $Impersonate;};
};
function ConnectPrimary ( $body ) {
if ( $body ) {
$call = Invoke-RestMethod -Uri $API.Uri -ContentType $API.ContentType -Body $body -Method $API.Method -Headers $API.Headers;
} ELSE {
$call = Invoke-RestMethod -Uri $API.Uri -ContentType $API.ContentType -Method $API.Method -Headers $API.Headers;
};
return $call;
};
function ConnectImpersonate ( $body ) {
if ( $body ) {
$call = Invoke-RestMethod -Uri $API.Uri -ContentType $API.ContentType -Body $body -Method $API.Method -Headers $API.HeaderImpersonate;
} ELSE {
$call = Invoke-RestMethod -Uri $API.Uri -ContentType $API.ContentType -Method $API.Method -Headers $API.HeaderImpersonate;
};
return $call;
};
Here is an example call grabbing a list of EAS ( ActiveSync ) accounts.
PrimaryAuth;
UpdateURImethod 'GET' 'api/v1/settings/sysadmin/active-sync-mailboxes';
$EAS=ConnectPrimary;
$EAS.activeSyncAccounts;
Here is an example adding a new doamin
PrimaryAuth
$dn = 'domain.tld';
$un = 'admin';
$pw = '12weDFGtr5^7yuhJU89';
UpdateURImethod 'POST' '/api/v1/settings/sysadmin/domain-put';
$b='{
"domainData":{
"name":"' + $dn + '",
"path":"C:\\SmarterMail\\Domains\\' + $($dn).ToLower() + '",
"hostname":"mail.' + $($dn).ToLower() + '",
"isEnabled": true,
"userLimit": 101,
"aliasLimit": 101,
"listLimit": 101,
"maxSize": ' + $(1024 * 1024 * 5) + ',',#-> bytes * kilabytes * megabytes
'"size": 123,
"sizeMb": 135
},
"domainLocation":0,
"domainLocationAddress":"",
"deliverLocallyForExternalDomain":false,
"adminUsername":"' + $($un).ToLower() + '",
"adminPassword":"' + $($pw) + '"
}';
$results=ConnectPrimary $b;
$results;
And an example adding a new user ( this API call is limited to domain admins )
PrimaryAuth;
ImpersonateUser 'admin@ascholz.local';
UpdateURImethod "POST" "api/v1/settings/domain/user-put" # Domain Admin Call
$u = "API";
$p = "12weDFGtr5^7yuhJU89";
$b = @('{
"userData": {
"userName": "' + $u + '",
"password": "' + $p + '",
"adUsername": null,
"securityFlags": {
"authType": 0,
"authenticatingWindowsDomain": null,
"isDomainAdmin": false
},
"isPasswordExpired": false
}
}');
$newUser=ConnectImpersonate $b;
$newUser;
I hope this helps you get started.
Thank you