Function 1: Set CRM Tracing

# crmTraceLogSettings.ps1
 
$traceDirectory="L:\crmTraceLogs"
$enable=$false
$categories='*:\Verbose'
$fileSize=10
$resetIis=$true
 
 
function setCrmTrace{
    param ($enable=$false,
    $traceDirectory,
    $categories,
    $fileSize,
    $resetIis=$false,
    $verbose=$true)
    
        $ErrorActionPreference='stop'
        # Source: https://learn.microsoft.com/en-us/previous-versions/troubleshoot/dynamics/crm/how-to-enable-tracing-in-dynamics-crm
        $serverTracingRegistry='Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\MSCRM'
        # Ignore these for newer CRM versions
        #$clientForOutlookTracingRegistry='Registry::HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\MSCRMClient'
        #$ssrsConnectorTracingRegistry='Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\MSCRMBidsExtensions'
        #$dataMigrationTracingRegistry='Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\DATA MIGRATION WIZARD'
    
        # Ensure that the plugin is available prior to proceeding
        try{
            Add-PSSnapin Microsoft.Crm.PowerShell -ErrorAction Stop
        }catch{
            Write-Error $_
            return $false      
            }
    
        try{
            $callStack=$enable
            $setting = Get-CrmSetting TraceSettings
            $setting.Enabled=$enable
            if($callStack){$setting.CallStack=$callStack}
            if($categories){$setting.Categories=$categories}
            if($traceDirectory){
                if(!(test-path $traceDirectory)){mkdir $traceDirectory -Force}
                $setting.Directory=$traceDirectory
                }
            if($fileSize){$setting.FileSize=$fileSize}
            Set-CrmSetting $setting
            $currentTraceDirectory=(Get-ItemProperty -path $serverTracingRegistry -name TraceDirectory -ErrorAction Ignore).TraceDirectory
            if($traceDirectory -and ($currentTraceDirectory -ne $traceDirectory)){
                Set-Itemproperty -path $serverTracingRegistry -Name 'TraceDirectory' -value $traceDirectory
                }
            if($enable){
                Set-Itemproperty -path $serverTracingRegistry -Name 'Enabled' -value 1
                Write-OutPut "The Crm Tracing is now ENABLED"
            }else{
                Set-Itemproperty -path $serverTracingRegistry -Name 'Enabled' -value 0
                Write-OutPut "The Crm Tracing is now DISABLED"          
            }
            if($verbose){write-host "$((Get-CrmSetting TraceSettings|out-string).trim())"}
            if($resetIis){
                write-host "Now resetting Internet Information Service..."
                $null=iisreset
            }else{
                Write-Warning 'IISReset must be invoked for changes to become fully effective.'
                }
            return $true
        }catch{
            Write-Warning $_
            return $false
            }
}

setCrmTrace $enable $traceDirectory $categories $fileSize $resetIis

Function 2: Turn On Logging (only)

# Set common variables
$serverTracingRegistry='Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\MSCRM'
 
# Enable CRM Tracing
Add-PSSnapin Microsoft.Crm.PowerShell
$setting = Get-CrmSetting TraceSettings
$setting.Enabled = $True
$setting.Directory = "D:\crmTraceLogs"
Set-CrmSetting $setting
$traceRefreshCurrentValue=(Get-ItemProperty -path $serverTracingRegistry).'TraceRefresh'
Set-Itemproperty -path $serverTracingRegistry -Name 'TraceRefresh' -value $($traceRefreshCurrentValue++)

Function 3: Turn Off Logging (only)

# Disable Tracing
Add-PSSnapin Microsoft.Crm.PowerShell
$setting = Get-CrmSetting TraceSettings
$setting.Enabled = $False
Set-CrmSetting $setting
Set-Itemproperty -path $serverTracingRegistry -Name 'TraceRefresh' -value 0

# Check current settings
Get-CrmSetting TraceSettings
Get-ItemProperty -path $serverTracingRegistry -Name 'TraceRefresh'

Troubleshooting:

Error message from Event Viewer:

Invalid Trace Directory. Additional Info:[ Invalid Trace Directory (Reporting Process:Trace Diretory is Null. TraceSettings: {Filename: ,FileCountSuffix:1 ,TraceFileSize:10485760 ,TraceDirectory: ,TracingCallStack:No ,IsTracingOff:Yes ,LoadState:LoadSuccessfulUnreported ,RefreshTraceInt:-1 ,SiteWideRefreshTraceInt:-2 ,RegistryRefreshTraceInt:-1 ,Precedence:Default} ] , AppDomain:CrmAsyncService)

Two registry keys at the path ‘HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\MSCRM’ must be set following the Set-CrmSetting command:

  1. Enabled: 0 or 1
  2. TraceDirectory: valid directory path required