Export Dynamics 365 finops data entity from power shell part 1
It is possible to call Dynamics 365 finops OData data entity endpoints using power shell. Invoke-restmethod commandlet can be used to make an OData call and get the result in JSON format. After we have a JSON in PowerShell, it’s easy to convert JSON to a PowerShell object. After converting json to object we can create csv, add query filters, drop files anywhere in the file system or azures storage, call another azure web api, export to excel etc. This is powerful.
Below is an example to fetch the token, read a data entity result and store the result as csv.
#########Get token####################################
$headers = @{"grant_type"="client_credentials"
"client_id"="AADClientId"
"client_secret"="SecretKey"
"resource"="ops url till .com and no extra / at the end"
"tenant_id"="Tenant id"}
$url="https://login.microsoftonline.com/microsoft.onmicrosoft.com/oauth2/token"
$Token = Invoke-RestMethod -Uri $url -Method `
POST -body $headers -ContentType "application/x-www-form-urlencoded"
$accessToken = $Token.access_token
########Read customer####################################
$url="https://aosserver.cloudax.dynamics.com/data/CustomersV2?$filter=dataAreaId%20eq%20%27usmf%27&cross-company=true"
$response =Invoke-RestMethod -Uri $url -Headers @{ "Authorization" = "Bearer " + $accessToken } -Method Get -ContentType "application/json"
$data = $response.value[0]
$data | Format-Table -property CustomerAccount,FullPrimaryAddress,NameAlias
######Export as csv########################################
$data | Select-Object -Property CreditLimit,CreditLimitIsMandatory,CustomerGroupId,SalesCurrencyCode, `
NameAlias,DataAreaId,CustomerAccount | Export-Csv -Path 'data.csv' -Encoding ascii -NoTypeInformation

Like
Report
*This post is locked for comments