Created
April 12, 2023 21:28
-
-
Save RylandDeGregory/c467cc5ed28407c686df86fc700d018d to your computer and use it in GitHub Desktop.
Connect to and query Dataverse from PowerShell
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
#requires -Version 5.1 | |
# Install required module from PSGallery | |
Install-Module -Name Microsoft.Xrm.Tooling.CrmConnector.PowerShell | |
# Interactive Sign-in | |
$CRMConn = Get-CrmConnection -InteractiveMode | |
# Unattended Sign-in | |
$Cred = New-Object System.Management.Automation.PSCredential ($UserName, $(ConvertTo-SecureString -String $Password -AsPlainText -Force)) | |
$CRMConn = Get-CrmConnection -Credential $Cred -DeploymentRegion NorthAmerica –OnlineType Office365 –OrganizationName $OrgName | |
# Build OAuth headers | |
$Headers = @{ | |
Authorization = "Bearer $($Conn.CurrentAccessToken)" | |
Accept = 'application/json' | |
} | |
# Query Dataverse endpoint | |
$OrgUrl = 'https://<orgID>.crm.dynamics.com' | |
Invoke-RestMethod -Method GET -Uri "$OrgUrl/api/data/v9.2/accounts?`$select=name&`$top=3" -Headers $Headers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment