Skip to main content

Requesting saved searches via oData and Excel is successful.
But for the final goal of converting the saved searches to csv-files I would like to get rid of the middleware (Excel) and do the job by PowerShell.

First trap is authentication.
I have an username and an api-token, but have no idea how to authenticate by PowerShell.

Any idea? Thank you!

 

Hi ​@mark2hepster,

$subdomain = "Your Subdomain"
$apiToken = "Your Api token"
$authHeader = =Convert]::ToBase64String(gText.Encoding]::ASCII.GetBytes("apitoken:$apiToken"))

# Make the POST request to get the access token
$response = Invoke-RestMethod -Uri "https://$subdomain.leanix.net/services/mtm/v1/oauth2/token" `
                              -Method Post `
                              -Headers @{Authorization=("Basic {0}" -f $authHeader)} `
                              -Body @{grant_type="client_credentials"}

# Extract the access token from the response
$accessToken = $response.access_token
Write-Output "Access Token: $accessToken"

Best regards,
Carsten


UPDATE: I got one step further. Succeeded with get-command, too. But only header (like requested).

Hi ​@Carsten ,

thanks a lot for Your answer.

Succeeded with requesting the access Token.

Requesting data I try with

$response = Invoke-RestMethod -Uri $odataUrl -Method Get -Headers @{
"Authorization" = "Bearer $accessToken"
}

results in

Invoke-RestMethod : ErrorFailed while retrieving oauth token: Unauthorized
In Zeile:1 Zeichen:13
+ $response = Invoke-RestMethod -Uri $odataUrl -Method Get -Headers @{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) tInvoke-R
estMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRest
MethodCommand

No matter if keep “Bearer” or opening and closing “.

Is the command itself a correct one?


Hi,
I am usually using Python. So I’d use use the graphql api and covert the result to CSV. I never tried to call the ODATA interface directly.

Best regards,

Carsten


After successful authorization I queried with

 $response = Invoke-RestMethod -Uri $odataUrl -Method Get -Headers @{ Authorization = "Basic $base64AuthInfo" }

Converting resulted in

$data = $response | ConvertFrom-Json

ConvertFrom-Json : Ungültiger JSON-Primitiv: .
In Zeile:1 Zeichen:21
+ $data = $response | ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) ConvertFrom-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJson
Command

but without converting I got the headers:

foreach ($item in $response.value) {
>> Write-Output "ID: $($item.Id), Name: $($item.Name)"
>> }
ID: , Name: export_contracts
ID: , Name: export_organizations
ID: , Name: export_budget-Initiatives
ID: , Name: export_applications
ID: , Name: export_provider

Trying to get the data:

$response = Invoke-RestMethod -Uri $odataUrl -Method Get -Headers @{ Authorization = "Basic $base64AuthInfo" } -Verbose

AUSFÜHRLICH: GET with 0-byte payload
AUSFÜHRLICH: received 494-byte response of content type application/json;odata.metadata=minimal

no headers, error on requesting data:

$headers=$response.Headers

Write-Output "Headers:"$headers

Headers:

$data=$response.Content | ConvertFrom-Json

ConvertFrom-Json : Das Argument kann nicht an den Parameter "InputObject" gebunden werden, da es
NULL ist.
In Zeile:1 Zeichen:27
+ $data=$response.Content | ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) :ConvertFrom-Json], ParameterBindingValidationExcepti
on
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Co
mmands.ConvertFromJsonCommand

# and without '| ConvertFrom-Json'
$response = Invoke-RestMethod -Uri $odataUrl -Method Get -Headers @{ Authorization = "Basic $base64AuthInfo" }

$data = $response.Content

Write-Output "Data:" $data

Data:

Other try:

$response = Invoke-WebRequest -Uri $odataUrl -Method Get -Headers @{
>> "Authorization" = "Basic $base64AuthInfo"
>> }

resulted in

$headers = $response.Headers

Write-Output "Headers:"$headers

Headers:

Key Value
--- -----
Connection keep-alive
Server-Timing intid;desc=4ffd8edea0efaa60
odata-version 4.0
x-envoy-upstream-service-time 1476
Strict-Transport-Security max-age=31536000; includeSubdomains
X-XSS-Protection 1; mode=block
Content-Length 494
Content-Type application/json;odata.metadata=minimal
Date Fri, 22 Nov 2024 09:20:48 GMT


$data = $response.Content | ConvertFrom-Json

Write-Output "Data:"$data

Data:

@odata.context value
-------------- -----
https://de.leanix.net/services/import-export/v1/odata/BookmarkService.svc/$metadata {@{name=export...

So it’s a bit like “Potting” for me :(
But I could understand if this would be beyond Your playground.


Reply