Skip to main content

This article provides practical PowerShell examples for interacting with the OpCon API. These examples are based on real-world use cases discussed in the OpCon community and training sessions.

Getting API Version

Use this script to retrieve the current version of the OpCon API. This endpoint does not require authentication.

Invoke-RestMethod -Uri "https://<your-server>/api/version"

 

Authenticating and Storing Tokens

Authenticate with the OpCon API and store the token for future use. 


$body = @{
    username = "yourUsername"
    password = "yourPassword"
}
$response = Invoke-RestMethod -Uri "https://<your-server>/api/tokens" -Method Post -Body $body -ContentType "application/json"
$token = $response.token

 

 

Evaluating Expressions

Evaluate property expressions using the API to test date formats and dependencies.


$headers = @{ Authorization = "Token $token" }
$body = @{ expression = "DATEADD(DATE(), -1, 'D')" }
Invoke-RestMethod -Uri "https://<your-server>/api/expressions/evaluate" -Method Post -Headers $headers -Body ($body | ConvertTo-Json)
 

 

Running Scripts from the Script Repository

Execute a script stored in the OpCon Script Repository.


$headers = @{ Authorization = "Token $token" }
$body = @{
    scriptName = "MyScript"
    parameters = @("param1", "param2")
}
Invoke-RestMethod -Uri "https://<your-server>/api/scripts/run" -Method Post -Headers $headers -Body ($body | ConvertTo-Json)

 

Handling Self-Signed Certificates

If your OpCon server uses a self-signed certificate, you may need to bypass certificate validation in PowerShell. 


[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
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

Be the first to reply!