Hi SM devs/guys, I'm trying to automate some things with SM using Powershell 5.1 via the REST API, I can do most of the things I'm attempting, but when I try and add white-listed domains, the command succeeds (i.e. no error), but the white-listed domains aren't added.
Below is a cut down version of the Powershell script (use Visual Studio Code), switch out the url, username and password and step through the code. What am I doing wrong, or is this a bug in the API?
Thanks, TJ
# working vars, params & constants
$vendorTrustedDomains = 'trusteddomain1.com, trusteddomain2.com '
New-Variable -Name 'SMbaseurl' -Value 'https://mail.mydomain.com/api/v1'; -Option Constant
$emailLoginUsername = 'admin@mydomain.com'
$emailLoginPwd ='MyPassword123'
$global:Header = ""
$vendorTrustedDomains = $vendorTrustedDomains.split(',').Trim() | Where-Object {$_}
# Functions
function AddTrustedEmailDomain($trustedVendorDomains)
{
# Update Mailing list system messages
$SMurl = "$SMbaseurl/settings/domain/domain"
$response = Invoke-RestMethod -Method GET -Header $Header -ContentType "application/json" -uri $SMurl
if ($response.success = $true)
{
$domainSettings = $response
foreach ($trustedVendorDomain in $trustedVendorDomains)
{
if (-not $domainSettings.domainSettings.whitelistDomains.Contains($trustedVendorDomain))
{
#add new vendor domain to whitelist - NOTE use of -InputObject for adding a single value!
write-host "Vendor Domain ($trustedVendorDomain) not setup on Domain as a trusted domain. Adding it now..."
#type:1 = domain
$vendorDomain = @{ "id" = ""
"value" = "$trustedVendorDomain"
"type" = "1"
"description" = ""
} | ConvertTo-Json
$domainSettings.domainSettings.whitelistDomains += (ConvertFrom-Json -InputObject $vendorDomain)
}
}
$domainSettings = $domainSettings | ConvertTo-Json -Depth 10
$SMurl = "$SMbaseurl/settings/domain/domain"
$response = Invoke-RestMethod -Method POST -Header $Header -ContentType "application/json" -uri $SMurl -Body $domainSettings
if ($response.success = $true)
{
return 1
}
else {
return -1
}
}
else {
return -1
}
}
# Adding certificate exception and TLS 1.2
add-type @"
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;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#-----------------
#Authenticate and get an authorization token...
#-----------------
$SMurl = "$SMbaseurl/login-settings/"
$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("${emailLoginUsername}:${emailLoginPwd}"))}
$SMurl = "$SMbaseurl/auth/authenticate-user"
$response = Invoke-RestMethod -Method POST -Header $Header -ContentType "application/json" -uri $SMurl -Body $params
#Write-Host $response.accessToken
if ($response.resultCode = 200)
{
$accesstoken = $response.accessToken
$refreshtoken = $response.refreshToken
$Header = @{Authorization = "Bearer "+$accesstoken}
}
else {
exit
}
# Now lets give it a go...
AddTrustedEmailDomain($vendorTrustedDomains)