Created
March 8, 2024 12:41
-
-
Save synesthesia/c7876ae6639c52784d10d70bc6f9cf5d to your computer and use it in GitHub Desktop.
Download Dataverse REST API metadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$environmentUrl = 'https://yourtenant.crmX.dynamics.com/' # change this | |
$writeFileTo = 'C:\temp\dynamics-metadata.xml' # change this | |
$azTenantId = "Insert your AD tenant ID (GUID)" #change this | |
## Login if not already logged in | |
if ($null -eq (Get-AzTenant -ErrorAction SilentlyContinue)) { | |
Connect-AzAccount -TenantId $azTenantId| Out-Null | |
} | |
# Get an access token | |
$token = (Get-AzAccessToken -ResourceUrl $environmentUrl).Token | |
# Common headers | |
$xmlHeaders = @{ | |
'Authorization' = 'Bearer ' + $token | |
'Accept' = 'application/xml' | |
'OData-MaxVersion' = '4.0' | |
'OData-Version' = '4.0' | |
} | |
$doc = [xml](Invoke-WebRequest ` | |
-Uri ($environmentUrl + 'api/data/v9.2/$metadata?annotations=true') ` | |
-Method 'Get' ` | |
-Headers $xmlHeaders ).Content | |
$StringWriter = New-Object System.IO.StringWriter | |
$XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter | |
$xmlWriter.Formatting = 'indented' | |
$xmlWriter.Indentation = 2 | |
$doc.WriteContentTo($XmlWriter) | |
$XmlWriter.Flush() | |
$StringWriter.Flush() | |
Set-Content -Path $writeFileTo -Value $StringWriter.ToString() | |
code $writeFileTo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment