Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Monday, March 16, 2020

Useful Powershell scripts

To Connect to CRM

1. Connect to CRM from Powershell Script using Command-line parameters(Ideal for using Script for automation purposes.)
# This PS snippet retreieves the Active Duplicate DEtection Rules frmo the crmDevOrg and activates them in crmTestOrg.
# The User  Name, Password and the Organization Name are to be updated before executing this.

## pass arguments OrganizationURL, UserName, Password
    [CmdletBinding()]
    param(
        [Parameter(Position=0, Mandatory=$true)] [string]$SourceOrganizationName,
        [Parameter(Position=1, Mandatory=$true)] [string]$SourceUserName,
        [Parameter(Position=2, Mandatory=$true)] [string]$SourcePassword,


Install-Module -Name Microsoft.Xrm.Data.Powershell -RequiredVersion 2.8.7 -Scope CurrentUser
$password = ConvertTo-SecureString $SourcePassword -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($SourceUserName,$password)
$crmDevOrg = Get-CrmConnection -Credential $credentials -DeploymentRegion NorthAmerica -OnlineType Office365 -OrganizationName $SourceOrganizationName  -MaxCrmConnectionTimeOutMinutes 5
 
2. Using interactive mode

Install-Module -Name Microsoft.Xrm.Data.Powershell -RequiredVersion 2.8.7 -Scope CurrentUser
$crmDevOrg = Get-CrmConnection –InteractiveMode 


3.Activate Duplicate detection rules with Powershell shell Script depending on the activatesd Rules in another Orgs - say - check form Source Org and ACtivate in the Destination Org

 This PS snippet retreieves the Active Duplicate DEtection Rules frmo the crmDevOrg and activates them in crmTestOrg.
# The User  Name, Password and the Organization Name are to be updated before executing this.

## pass arguments OrganizationURL, UserName, Password
    [CmdletBinding()]
    param(
        [Parameter(Position=0, Mandatory=$true)] [string]$DevOrganizationName,
        [Parameter(Position=1, Mandatory=$true)] [string]$DevUserName,
        [Parameter(Position=2, Mandatory=$true)] [string]$DevPassword,
 [Parameter(Position=3, Mandatory=$true)] [string]$TestOrganizationName,
        [Parameter(Position=4, Mandatory=$true)] [string]$TestUserName,
        [Parameter(Position=5, Mandatory=$true)] [string]$TestPassword
    )

Install-Module -Name Microsoft.Xrm.Data.Powershell -RequiredVersion 2.8.7 -Scope CurrentUser
$password = ConvertTo-SecureString $DevPassword -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($DevUserName,$password)
$crmDevOrg = Get-CrmConnection -Credential $credentials -DeploymentRegion NorthAmerica -OnlineType Office365 -OrganizationName $DevOrganizationName  -MaxCrmConnectionTimeOutMinutes 5
 
$duplicaterules = Get-CrmRecords -conn $crmDevOrg 'duplicaterule' -FilterAttribute statecode -FilterOperator eq -FilterValue Active
$duplicateruleIds = $duplicaterules['CrmRecords'] | select -ExpandProperty duplicateruleid

$password = ConvertTo-SecureString $TestPassword -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($TestUserName,$password)
$crmTestOrg = Get-CrmConnection -Credential $credentials -DeploymentRegion NorthAmerica -OnlineType Office365 -OrganizationName $TestOrganizationName  -MaxCrmConnectionTimeOutMinutes 5
 
foreach($ruleId in $duplicateruleIds)
        {
            write-host "publishing rule id: " $ruleId
            $ddRule_toPublish = New-Object Microsoft.Crm.Sdk.Messages.PublishDuplicateRuleRequest            
            $ddRule_toPublish.DuplicateRuleId= $ruleId
            $crmTestOrg.ExecuteCrmOrganizationRequest($ddRule_toPublish,$trace)  
            Write-Host "Rule Published"       
        }