Ignite 2017 Demonstration: PowerShell with the Dynamics 365 Online Management API

Today’s post contains the sample script we used to talk through our PowerShell demo’s at Ignite – to all those who watched remotely or in person: thank you!  The purpose of this script is to give admins a ready-to-run script that demonstrates some of the Online Management API features in PowerShell and uses an optional module (Microsoft.Xrm.Data.PowerShell GitHub link & PowerShell Gallery link) to also review and edit data in a given Dynamics 365 Customer Engagement instance.  This script may require you changing your execution policy (specifically the Xrm.Data.Powershell module as it’s not signed with a public cert at this point – I am looking to sign it hopefully in our next release, though it will be self-signed) but the online management PowerShell module is signed for your consumption.

For those looking for more content or an API reference for our new Online Management API you can find there here: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/online-management-api/get-started-online-management-api

And for those who just want to install the PowerShell Module – use the script below to get started.

Thanks for reading!

Sean McNellis | Twitter: @seanmcne

#Install the Online Management API to the current user profile
install-module Microsoft.Xrm.OnlineManagementAPI -Scope CurrentUser -force  

#interactive prompt
$  cred = Get-Credential 

$  apiUrl = "https://admin.services.crm.dynamics.com/" #Northamerica Service Instance 

Import-Module Microsoft.Xrm.OnlineManagementAPI -Verbose 

#get instance info 
$  instances = Get-CrmInstances -Credential $  cred -ApiUrl $  apiUrl -verbose 

#retrieve all the current deployed versions of Dynamics 365 
$  Versions = Get-CrmServiceVersions -ApiUrl $  apiUrl -Credential $  cred   

#find version 8.2 
$  v8dot2 = $  Versions | where Version -like "8.2"  

#now create new instance information for our new instance that we wish to create 
$  instanceInfo = New-CrmInstanceInfo -BaseLanguage 1033 `
    -ServiceVersionId $  v8dot2.Id `
    -InstanceType Sandbox `
    -DomainName "pfecrmonline.onmicrosoft.com" `
    -InitialUserEmail "user@tenantname.onmicrosoft.com" `
    -FriendlyName "Ignite 2017" 

#create that new instance using the info from above 
$  newInstance = New-CrmInstance -ApiUrl $  apiUrl -Credential $  cred -NewInstanceInfo $  instanceInfo

#now parse the resource ID (instance ID) 
$  resource = $  newInstance.ResourceLocation.Split("/")
$  instanceId = $  resource[$  resource.Count-1]

Write-Output "the instance ID is: $  instanceId" 
Get-CrmInstance -ApiUrl $  apiUrl -Credential $  cred -Id $  instanceId -Verbose

#now get backups for another instance by the instance's uniquename 
$  instance = Get-CrmInstance -ApiUrl $  apiUrl -Credential $  cred -Id ($  instances|where UniqueName -eq "uniqueinstancename").Id -Verbose
$  instance|Select UniqueName,version,state, ApplicationUrl, Id | Format-Table
$  backups = Get-CrmInstanceBackups -ApiUrl $  apiUrl -Credential $  cred -InstanceId $  instance.Id|ft

#next let's use Xrm Data PowerShell to explore data within a particular instance 

#install from PowerShell Gallery 
Install-Module Microsoft.Xrm.Data.PowerShell -Scope CurrentUser

#load the module
Import-Module Microsoft.Xrm.Data.PowerShell -Verbose 

Connect-CrmOnline -ServerUrl naosrtw.crm.dynamics.com -Cred $  cred


$  iam = Invoke-CrmWhoAmI

Get-CrmRecords -EntityLogicalName account 

Get-CrmEntityAttributes -EntityLogicalName systemuser | select logicalname,AttributeType | sort logicalname

Get-CrmRecord -EntityLogicalName systemuser -Id $  iam.UserId -fields personalemailaddress

Set-CrmRecord -EntityLogicalName systemuser -Id $  iam.UserId -Fields @{"personalemailaddress"="jim@outlook.com"}

$  accounts = Get-CrmRecords -EntityLogicalName account -Fields name -TopCount 400 

Let’s block ads! (Why?)

Dynamics CRM in the Field