Skip to main content

We want to keep data in a reference system and in LeanIX in synch.  The administrators of the reference system are proficient in powershell, and the easiest path is a powershell script that uses the LeanIX APIs.

 

Does anyone have a powershell script that uses the LeanIX APIs that we could use as a reference model?

Hi ​@Nick Bowler ,
I use Python. Anyway I ask copilot to translate the original LeanIX example:
 

# Get environment variables

$LEANIX_API_TOKEN = $env:LEANIX_API_TOKEN

$LEANIX_SUBDOMAIN = $env:LEANIX_SUBDOMAIN

$LEANIX_GRAPHQL_URL = "https://$LEANIX_SUBDOMAIN.leanix.net/services/pathfinder/v1/graphql"

$LEANIX_OAUTH2_URL = "https://$LEANIX_SUBDOMAIN.leanix.net/services/mtm/v1/oauth2/token"

 

function Get-LeanIXAccessToken {

    if (-not $LEANIX_API_TOKEN) {

        throw "A valid token is required"

    }

 

    $body = @{

        grant_type = "client_credentials"

    }

 

    $response = Invoke-RestMethod -Method Post -Uri $LEANIX_OAUTH2_URL -Body $body -Authentication Basic -Credential (New-Object System.Management.Automation.PSCredential("apitoken", (ConvertTo-SecureString $LEANIX_API_TOKEN -AsPlainText -Force)))

 

    return $response.access_token

}

 

function Invoke-LeanIXQuery {

    $access_token = Get-LeanIXAccessToken

 

    $graphql_query = @"

{

    allFactSheets(factSheetType: Application) {

        totalCount

        edges {

            node {

                id

                name

            }

        }

    }

}

"@

 

    $headers = @{

        Authorization = "Bearer $access_token"

        Content-Type  = "application/json"

    }

 

    $body = @{

        query = $graphql_query

    } | ConvertTo-Json -Depth 3

 

    $response = Invoke-RestMethod -Method Post -Uri $LEANIX_GRAPHQL_URL -Headers $headers -Body $body

    $response | ConvertTo-Json -Depth 5

}

 

Invoke-LeanIXQuery


I got usually good results, So I learned pyspark. Anyway it is worth as try.
Best regards,
Carsten


Reply