Scripts/UpdateVersion.ps1

# UpdateVersion.ps1
function Set-DataverseSolutionVersion {
    try {
        $message = "Updating Solution version for $global:devops_SolutionName"
        Write-Host $message
        $Date = Get-Date
        $Date = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($Date, "UTC")
        $patchVersions = @()
        Write-Host ""
        try {
            $versionsFile = Get-Content -Path $global:devops_projectLocation\$global:devops_SolutionName\$global:devops_SolutionName.version -ErrorAction SilentlyContinue | ConvertFrom-Json
            $versionsFile = @($versionsFile[0])  
        }
        catch {

        }

        # If there are no Patches
        if (!$PatchQuery.CrmRecords) {
            # Major.Minor.Build.Revision = TargetProductionDrop.Year+DayofYear.PatchNumber.BuildTime
            $theVersion = [version]$SolutionVersion
            $newVersion = "{0}.{1}{2}.{3}.{4}" -f $theVersion.Major, (Get-Date($Date) -UFormat %y), (Get-Date($Date) -UFormat %j).PadLeft(3, '0'), $theVersion.Build , (Get-Date($Date) -UFormat %H%M)
            $global:devops_newVersion = $newVersion
            $verUpdated = Set-CrmSolutionVersionNumber -conn $conn -SolutionName $SolutionName -VersionNumber $newVersion
            $solutionTracker = @([ordered]@{SolutionName = $SolutionName; Version = $newVersion.ToString() ; })
            ConvertTo-Json -Depth 4 $solutionTracker | Format-Json | Out-FileUtf8NoBom $global:devops_projectLocation\$global:devops_SolutionName\$global:devops_SolutionName.version
            Write-Host $verUpdated
        }
        else {

            if ($global:devops_projectFile.IncrementLatestPatchOnExport -eq "True") {
                $LatestPatch = $PatchQuery.CrmRecords[$PatchQuery.CrmRecords.Count-1]
                $newVersion = "{0}.{1}{2}.{3}.{4}" -f ([version]$LatestPatch.version).Major, (Get-Date($Date) -UFormat %y), (Get-Date($Date) -UFormat %j).PadLeft(3, '0'), ([version]$LatestPatch.version).Build , (Get-Date($Date) -UFormat %H%M)
                $verUpdated = Set-CrmSolutionVersionNumber -conn $conn -SolutionName $LatestPatch.uniquename -VersionNumber $newVersion
                $PatchQuery.CrmRecords[$PatchQuery.CrmRecords.Count-1].version = $newVersion
                Write-Host "Updated version number of the latest patch," $LatestPatch.uniquename
            }

            foreach ($PatchSolution in $PatchQuery.CrmRecords){
                $SolutionId = $PatchSolution.solutionid
                $SolutionName = $PatchSolution.uniquename
                $SolutionVersion = $PatchSolution.version
                Write-Host "Patch found:" $SolutionId "-" $SolutionName "-" $SolutionVersion
                $theVersion = [version]$SolutionVersion

                $newVersion = "{0}.{1}.{2}.{3}{4}" -f $theVersion.Major, $theVersion.Minor, $theVersion.Build, $theVersion.MajorRevision, $theVersion.MinorRevision

                $global:devops_newVersion = $newVersion
                $patchTracker = @([ordered]@{SolutionName = $SolutionName; Version = $newVersion.ToString() ; })
                $patchVersions += $patchTracker
            }
            $versionsFile += $patchVersions
            ConvertTo-Json -Depth 4 $versionsFile | Format-Json | Out-FileUtf8NoBom $global:devops_projectLocation\$global:devops_SolutionName\$global:devops_SolutionName.version
        }
    }
    catch {
        Write-Host $_
        pause
    }
    finally {

    }
}