Posted On April 13, 2021

PowerShell: Microsoft Dynamics Update All Organizations

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Microsoft Dynamics Update All Organizations
# updateCrmOrgs.ps1

function updateCrmOrgs{
  try{
    Add-PSSnapin Microsoft.Crm.Powershell
    $servers=Get-CrmServer
    $highestVersion=($servers.Version|measure-object -Maximum).Maximum
    $lowVersionServers=$servers|?{[version]$_.Version -lt [version]$highestVersion}
    if($lowVersionServers){
      write-warning "Program cannot continue because these servers have NOT been upgraded to version $highestVersion`:`r`n$($lowVersionServers|select FullName,Version)"
    }else{
      $orgsToUpgrade=(Get-CrmOrganization|?{[version]$_.Version -lt [version]$highestVersion}).UniqueName
      if($orgsToUpgrade){
        $orgsToUpgrade|%{Update-CrmOrganization -Name $_}
        $operationIds=(get-crmoperationstatus).Id
        $completed=0
        do{
          $status=$operationIds|%{get-crmoperationstatus -operationid $_}
          $currentCompleted=($status.State -match 'Failed|Completed').count
          if($completed -ne $currentCompleted){
              write-host "$currentCompleted of $($operationIds.count) completed"
          }else{
              write-host '.' -nonewline
          }
          $completed=$currentCompleted
          sleep 1
        } until($completed -eq $operationIds.count)
      }else{
        write-host "All Orgs are already at version $highestVersion" -ForegroundColor Green
      }
    }
    return $true
  }catch{
    write-warning $_
    return $false
  }
}
updateCrmOrgs

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Remove an A-Host Record within Active Directory Integrated DNS Domain

Warning: this code is NOT 'production ready'. Please review and test on DEV environments carefully…

JavaScript: Show the Local Weather

Demo: https://blog.kimconnect.com/wp-content/projects/showLocalWeather.html HTML Code: <html><body><h3 class="text-center">A Free Code Camp JavaScript Intermediate Level Project</h3><div class="container"><div class="text-center">…

Front End Web Development Menu 2019

Deployment Register a domain name: Google Domains, GodaddyManaged or Shared hosting: AWS, Hostgator, InmotionFTP/SFTP: Filezilla,…