Home

Friday, March 13, 2020

Deactivate views in dynamics 365 using power-shell scripts

Install-Module -Name Microsoft.Xrm.Data.Powershell -RequiredVersion 2.8.7 -Scope CurrentUser
#$crmOrg = Get-CrmConnection –InteractiveMode
$password = ConvertTo-SecureString "***" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential("username@domain.com",$password)
$crmOrg = Get-CrmConnection -Credential $credentials -DeploymentRegion NorthAmerica -OnlineType Office365 -OrganizationName "dev-dev"  -MaxCrmConnectionTimeOutMinutes 5

Write-Host '------------------------------------------------------------'
Write-Host 'Information: Connection established successfully....'
Write-Information -MessageData "Connection established successfully...." -Tags success
$viewNames = @( "Accounts being Followed",
"Accounts I Follow",
"Won Project Service Opportunities"
);

$viewSb = [System.Text.StringBuilder]::new()

for($i = 0; $i -lt $viewNames.length; $i++)
{
 $viewSb = $viewSb.Append("<condition attribute='name' operator='eq' value='" + $viewNames[$i] +"'/>" )
}

$viewFetchXml = "<fetch>
     <entity name='savedquery'>
                       <attribute name='statecode' />
                        <attribute name='name' />
                        <attribute name='returnedtypecode' />
                        <attribute name='statuscode' />
                        <attribute name='isdefault' />
                        <filter type='or'>"+ $viewSb.ToString() +
      "</filter>       
       <order attribute='name' />
                    </entity>
                </fetch>";
      
$viewsToDeactivate = Get-CrmRecordsByFetch -Fetch $viewFetchXml -conn $crmOrg

$success = 0
$failure = 0

foreach($viewRecord in $viewsToDeactivate['CrmRecords'])
{
   Set-CrmRecordState -CrmRecord $viewRecord -StateCode 1 -StatusCode 2 -conn $crmOrg -ErrorAction SilentlyContinue -ErrorVariable viewError
   if($viewError){
   Write-Warning -Message "$($viewRecord.name) - Deactivation Failed"
   Write-Information -MessageData "$($viewRecord.name) ' - Deactivation Failed" -Tags success
   #Write-Error -Message $viewError[0].Exception.Message
   $failure++
   }
   else {
   Write-Information -MessageData "$($viewRecord.name) ' - Deactivated Successfully" -Tags failure
   Write-Host 'Information: '$viewRecord.name ' - Deactivated Successfully'
   $success++
   }
}

Write-Host '------------------------------------------------------------'
Write-Host 'Information: Total count of views to be Deactivated - ' $viewsToDeactivate.Count
Write-Information -MessageData "Total count of views to be Deactivated - &($viewsToDeactivate.Count)" -Tags success
Write-Host 'Information: Total count of views Deactivated Successfully- ' $success
Write-Information -MessageData "Total count of views Deactivated Successfully - $($success)" -Tags success
Write-Host 'Information: Total count of views failed to Deactivate - ' $failure
Write-Information -MessageData "Total count of views failed to Deactivate - $($failure)" -Tags failure
Write-Host '------------------------------------------------------------'

No comments:

Post a Comment

Convert subgrid to Comments