Skip to content

Instantly share code, notes, and snippets.

@RylandDeGregory
Created April 12, 2023 21:28
Show Gist options
  • Save RylandDeGregory/c467cc5ed28407c686df86fc700d018d to your computer and use it in GitHub Desktop.
Save RylandDeGregory/c467cc5ed28407c686df86fc700d018d to your computer and use it in GitHub Desktop.
Connect to and query Dataverse from PowerShell
#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