Author Archives: admin

PowerShell – Re-publish Deactivated Duplicate Check Rules


Write-Output "------------------------------------"
Write-Output "Re-publishing all duplicate detection rules"

$dupeCheckRules = Get-CrmRecords -EntityLogicalName duplicaterule -fields *
foreach ($dupeChkRule in $dupeCheckRules.CrmRecords)
{
    Write-output "State : $($dupeChkRule.statecode) Status : $($dupeChkRule.statecode)  -  $($dupeChkRule.name))"
    if ($dupeChkRule.statecode -eq "Inactive" -and $dupeChkRule.statuscode -eq "Unpublished")
    { 
        $pubDupeChkReq = New-Object Microsoft.Crm.Sdk.Messages.PublishDuplicateRuleRequest
        $pubDupeChkReq.DuplicateRuleId = $dupeChkRule.duplicateruleid
        Write-Output "    Publishing duplication check rule [$($dupeChkRule.duplicateruleid)]"
        $pubDupeChkResponse = $proxy.Execute($pubDupeChkReq)
        Write-Output "    Job started $($pubDupeChkResponse.JobId)"
    }
}

PowerShell – Retrieve CRM Entities with Auditing Enabled


# Connect to Dynamics CRM. Use Connect-CrmOnPremDiscovery for On-Premise
Connect-CrmOnlineDiscovery -InteractiveMode


$retrieveAllEntitiesRequest = new-object Microsoft.Xrm.Sdk.Messages.RetrieveAllEntitiesRequest
$retrieveAllEntitiesRequest.RetrieveAsIfPublished = $true
$retrieveAllEntitiesRequest.EntityFilters = New-Object Microsoft.Xrm.Sdk.Metadata.EntityFilters

$retrieveAllEntitiesResponse  = $conn.ExecuteCrmOrganizationRequest($retrieveAllEntitiesRequest)

$entities = $retrieveAllEntitiesResponse.EntityMetadata

foreach($item in $entities) {

    if ($item.IsAuditEnabled.value -eq $true) {
        write-output $item.LogicalName
    }

} 

PowerShell – Adding a specific user to a specific role, across a number of named environments

$ErrorActionPreference = "silentlycontinue"

$cred = get-credential
$username = "<CRM UserName>"
$role = "<CRM Security Role Name>"

$URLs = @("<CRM URL>",
    "<CRM URL>")

foreach ($serverUrl in $URLs) {

    # Connect to Instance
    $CRMConn = Connect-CrmOnline -Credential $cred -ServerUrl $serverUrl

    # Retrieve the specific CRM user
    $CRMUsers = Get-CrmRecords -EntityLogicalName systemuser -FilterAttribute domainname -FilterOperator eq -FilterValue $username -Fields systemuserid, fullname

    # Ensure we just got one CRM user back
    if ($CRMUsers.Count -ne 1) {
        Write-Host $serverUrl " -> " $username " does not exist"
        continue
    }

    # Retrieve the specific CRM Security Role
    $existingRoles = Get-CrmRecords -EntityLogicalName role  -FilterAttribute name -eq $role -Fields name

    # Ensure this CRM Security Role exists in the target
    if ($existingRoles.Count -eq 1) {
        Write-Host $serverUrl " does not include " $role
        continue
    }

    # Retrieve the user's current CRM Security Roles
    $userRoles = Get-CrmUserSecurityRoles -conn $CRMConn -UserId $CRMUsers.CrmRecords[0].systemuserid -WarningAction SilentlyContinue | Select-Object RoleId, RoleName

    # Check if the user already has the CRM Security Role
    if (!($userRoles.RoleName -contains $role)) {
        Add-CrmSecurityRoleToUser -conn $CRMConn -UserId $CRMUsers.CrmRecords[0].systemuserid -SecurityRoleName $role
        Write-Host $serverUrl " -> " " Added " $username " to " $role
    }
    else {
        Write-Host $serverUrl " -> " $username " is already in " $role
    }
}