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: How To Test A Server Ephemeral Port

# Setup a listening port on server # This session will automatically terminates after a…

PowerShell: Get Quorums of All Clusters in the Domain

# getClusterQuorum.ps1 function getClusterQuorum{ $allClusters=(get-cluster -domain $env:USERDNSDOMAIN).Name $results=@{} foreach ($cluster in $allClusters){ $quorum=Get-ClusterQuorum -Cluster $cluster…

PowerShell: Script to Search Scheduled Tasks for a Service Account

#$jumpBox=$env:COMPUTERNAME$servers="WEB01"$runas="Network Service"# Admin$who = whoami if ($who.substring($who.length-5, 5) -eq "-admin"){$username=$who;} else {$username=$who+"-admin";}#$password = Read-Host -Prompt…