MS CRM list user access via SQL

During my CRM Solution import debugging yesterday, I also wanted to see which user had been logged in to CRM the last few days. After some googling og trying I came up with this SQL statement for listing all users and last time they accessed CRM during the last 3 days.

NB! You have to change the “OrgName” to get this working on you CRM database server. It is tested for CRM 2011 and CRM 2016.

USE MSCRM_Config
SELECT O.FriendlyName, SU.FullName as Name, SUO.LastAccessTime
FROM SystemUserOrganizations SUO
       LEFT JOIN SystemUserAuthentication SUA ON SUO.UserId = SUA.UserId 
                                    AND LEFT(AuthInfo, 1)='C'
       LEFT JOIN Organization O ON SUO.OrganizationId=O.Id
       INNER JOIN OrgName_MSCRM.dbo.SystemUser SU ON SUO.CrmUserId = SU.systemuserid
WHERE LastAccessTime IS NOT NULL 
  AND O.FriendlyName = 'OrgName' 
  AND datediff(DAY,Lastaccesstime, getutcdate()) < 3
ORDER BY lastaccesstime
Advertisements

Enable Dynamics CRM Tracing with powershell

If you have some problems with Dynamics CRM On-premise you are able to enable tracing with PowerShell. In my case, I needed to get debug information on why my solution import is failing when I’m going to move it to a new organization.

Open the powershell prompt and use the Add-PSSnapin command shown in 1). Thereby, You can list the trace setting with the command shown in 2). Before you start the tracing, you show determine the timeline for when the error occurs and just enble it as close as the error as possible. Run the command in 3) to start the tracing. You should stop the tracing immediately after the error has occured. Use command in 4) to stop the tracing.

# 1) add
Add-PSSnapin Microsoft.Crm.PowerShell 

# 2) get crm trace settings
Get-CrmSetting TraceSettings 

# 3) enable tracing
$Setting = Get-CrmSetting TraceSettings
$Setting.Enabled = $True 
$Setting.CallStack=$True
$Setting.Categories="*:Verbose"
$Setting.Directory="C:\temp\crmtrace"
Set-CrmSetting $setting

# 4) disbale tracing
$Setting = Get-CrmSetting TraceSettings
$setting.Enabled = $False
Set-CrmSetting $setting

When you have tons of log file, the trace tool CRM Trace reader is nice to use for searing and filtering.