Skip to content

Instantly share code, notes, and snippets.

@MayMeow
Created October 27, 2022 13:45
Show Gist options
  • Save MayMeow/62d3b53b6052914c7e96e297bf5813cb to your computer and use it in GitHub Desktop.
Save MayMeow/62d3b53b6052914c7e96e297bf5813cb to your computer and use it in GitHub Desktop.
Execute script over API on Mikrotik router
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
$Body = @{
number = "script-name";
}
$user = "user"
$password = "pass"
$apiEndpoint = "https://mikrotik-IP/rest/system/script/run" # You need to have the www-ssl service set up correctly
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($user):$($password)"))
$Header = @{
Authorization = "Basic $base64AuthInfo"
}
$Parameters = @{
Header = $Header
Method = "POST"
Uri = $apiEndpoint
ContentType = "application/json"
# SkipCertificateCheck = $true
Body = ($Body | ConvertTo-Json)
}
Invoke-RestMethod @Parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment