Dynamics 365/2016 CRM: Using PowerShell to Manage user Identity

Dynamics 365 has a bunch of PowerShell commandlets that can be used to manage the PowerShell server. There is a PowerShell tool that comes with the SDK which can used to make a connection to the CRM organization, that tool can be found here

The nice thing about PowerShell is that it is best buddies with C# so you can literally take your C# code and translate easily to PowerShell. In this blog post, I am going to use the Dynamics 365 SDK library and show how to do the following things

  1. Connect to Dynamics CRM SOAP
  2. Get User list
  3. Get Role list

Prerequisites

  1. You need PowerShell version 3.0 or later.
  2. Set the execution policy to run the PowerShell scripts.
  3. You should have downloaded the Dynamics 365 SDK here. The following files should be copied to any location. Note: there is no  MyOrganizationCrmSdkTypes.dll in the package, there is a MyOrganizationCrmSdkTypes.cs. I took that file and compiled it as a dll. See my posting on how to do that
  • Microsoft.Crm.Sdk.Proxy.dll
  • Microsoft.IdentityModel.dll
  • Microsoft.Xrm.Sdk.dll
  • System.ServiceModel.dll
  • MyOrganizationCrmSdkTypes.dll

The Code

Add-Type -Path “C:\dynamics\Microsoft.Crm.Sdk.Proxy.dll”

Add-Type -Path “C:\dynamics\Microsoft.IdentityModel.dll”

Add-Type -Path “C:\dynamics\Microsoft.Xrm.Sdk.dll”

Add-Type -Path “C:\dynamics\System.ServiceModel.dll”

Add-Type -Path “C:\dynamics\MyOrganizationCrmSdkTypes.dll”

function GetUserList()

{

# Retrieve specified system user and write out to console.

$query = new-object -TypeName Microsoft.Xrm.Sdk.Query.QueryExpression “systemuser”

$query.EntityName = “systemuser”

$query.ColumnSet = new-object -TypeName Microsoft.Xrm.Sdk.Query.ColumnSet($true)

$retrieved = $null;

ForEach ($en In $_service.RetrieveMultiple($query).Entities)

{

Write-Host “Id =” $en.Id “, First Name = ” $en.FirstName “, Last Name = ” $en.LastName

}

}

function GetRoleList()

{

$_roleId;

try

{

# Lets declare some queries

$query = new-object -TypeName Microsoft.Xrm.Sdk.Query.QueryExpression “role”

$query.EntityName = “role”

$query.ColumnSet = new-object -TypeName Microsoft.Xrm.Sdk.Query.ColumnSet($true)

# Get the role.

#$roles = $_service.RetrieveMultiple($query);

ForEach ($en In $_service.RetrieveMultiple($query).Entities)

{

Write-Host “Role Id =” $en.Id “, Name = ” $en.Name

}

}

# Catch any service fault exceptions that Microsoft Dynamics CRM throws.

catch

{

# You can handle an exception here or pass it back to the calling method.

Write-Host $_.Exception.Message

}

}

##############********* END of Funtions *******************############################

##############******* *************** *******************############################

##############********** Main Program *******************############################

$_service = $null

$UserName = “tlkDynamicsadmin”

$Password = “Password1”

$Domain = “tlkdynamics”

$SoapOrgServiceUri = “http://tlkcrmserver:5555/TLKCRMORG/XRMServices/2011/Organization.svc”

try

{

$credentials = New-Object System.ServiceModel.Description.ClientCredentials

$credentials.Windows.ClientCredential = New-Object System.Net.NetworkCredential($UserName, $Password, $Domain);

$credentials.UserName.UserName = $UserName;

$credentials.UserName.Password = $Password;

$serviceUri = New-Object Uri($SoapOrgServiceUri);

$proxy = New-Object Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy($serviceUri, $null, $credentials, $null);

$proxy.EnableProxyTypes();

$_service = $proxy

}

catch

{

Write-Host “Error while connecting to CRM ” + $_.Exception.Message

}

GetUserList

GetRoleList