Functions/Audit/Get-CdsAuditHistory.ps1
<#
.SYNOPSIS Retrieve audit for given record. #> function Get-CdsAuditHistory { [CmdletBinding()] param ( [Parameter(Mandatory = $false, ValueFromPipeline)] [Microsoft.Xrm.Tooling.Connector.CrmServiceClient] $XrmClient = $Global:XrmClient, [Parameter(Mandatory = $true, ValueFromPipeline)] [Microsoft.Xrm.Sdk.EntityReference] $RecordReference, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $RecordAttributeName ) begin { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); } process { $retrieveRecordChangeHistoryRequest = New-Object -TypeName Microsoft.Crm.Sdk.Messages.RetrieveRecordChangeHistoryRequest; $retrieveRecordChangeHistoryRequest.Target = $RecordReference; $retrieveRecordChangeHistoryReponse = Protect-CdsCommand -ScriptBlock { $XrmClient.Execute($retrieveRecordChangeHistoryRequest) }; foreach ($auditDetail in $retrieveRecordChangeHistoryReponse.AuditDetailCollection.AuditDetails) { foreach ($attribute in $auditDetail.NewValue.Attributes) { $auditLog = New-Object -TypeName PSObject; $auditLog | Add-Member -MemberType NoteProperty -Name Object -Value $auditDetail.AuditRecord.FormattedValues["objecttypecode"]; $auditLog | Add-Member -MemberType NoteProperty -Name Id -Value $auditDetail.AuditRecord.Attributes["objectid"].Id; $auditLog | Add-Member -MemberType NoteProperty -Name Key -Value $record[$RecordAttributeName]; $auditLog | Add-Member -MemberType NoteProperty -Name CreatedOn -Value $auditDetail.AuditRecord.FormattedValues["createdon"]; $auditLog | Add-Member -MemberType NoteProperty -Name User -Value $auditDetail.AuditRecord["userid"].Name; $auditLog | Add-Member -MemberType NoteProperty -Name Operation -Value $auditDetail.AuditRecord.FormattedValues["operation"]; $auditLog | Add-Member -MemberType NoteProperty -Name Action -Value $auditDetail.AuditRecord.FormattedValues["action"]; $attributeName = $attribute.Key; $auditLog | Add-Member -MemberType NoteProperty -Name AttributeName -Value $attributeName; $oldValue = ""; if ($auditDetail.OldValue.Contains($attributeName)) { if ($auditDetail.OldValue.FormattedValues.ContainsKey($attributeName)) { $oldValue = $auditDetail.OldValue.FormattedValues[$attributeName]; } else { $oldValue = $auditDetail.OldValue[$attributeName]; } } $auditLog | Add-Member -MemberType NoteProperty -Name OldValue -Value $oldValue; $newValue = ""; if ($auditDetail.NewValue.Contains($attributeName)) { if ($auditDetail.NewValue.FormattedValues.ContainsKey($attributeName)) { $newValue = $auditDetail.NewValue.FormattedValues[$attributeName]; } else { $newValue = $auditDetail.NewValue[$attributeName]; } } $auditLog | Add-Member -MemberType NoteProperty -Name NewValue -Value $newValue; $auditLogs += $auditLog; } } $auditLogs; } end { $StopWatch.Stop(); Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch; } } Export-ModuleMember -Function Get-CdsAuditHistory -Alias *; |