Skip to content

Instantly share code, notes, and snippets.

@jlattimer
Last active February 1, 2019 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlattimer/deee92b33d2d38a4c9ce7c9e771d37c6 to your computer and use it in GitHub Desktop.
Save jlattimer/deee92b33d2d38a4c9ce7c9e771d37c6 to your computer and use it in GitHub Desktop.
Given a solution uniquename this sets build variables for the uniquename & version of the latest patch of a solution or the base solution's uniquename & version if no patches exist #blog
# Install Microsoft.Xrm.Data.Powershell if not found
if (!(Get-Module "Microsoft.Xrm.Data.Powershell")) {
Install-Module -Name Microsoft.Xrm.Data.Powershell -AcceptLicense -AllowClobber -Force -Scope AllUsers
}
$SearchSoltionName = "SolutionName"
$ReturnSolutionName = $SearchSoltionName
$ReturnSolutionVersion = "_1_0_0_0"
$User = "$(D365Username)"
$PWord = "$(D365Password)" | ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $PWord
$Conn = Connect-CrmOnline -Credential $Cred -ServerUrl $(D365Url)
# Get parent solution and by name
$Solutions = Get-CrmRecords -EntityLogicalName solution -conn $Conn -Fields 'solutionid','friendlyname','version' -FilterAttribute uniquename -FilterOperator eq -FilterValue $SearchSoltionName
if ($Solutions.CrmRecords.Count -eq 1) {
$ParentSolutionId = $Solutions.CrmRecords[0].solutionid
$ReturnSolutionVersion = "_" + $Solutions.CrmRecords[0].version.Replace(".", "_")
Write-Host "Found:" $ParentSolutionId "-" $Solutions.CrmRecords[0].friendlyname "-" $Solutions.CrmRecords[0].version
# Get most recent patch solution based on created on date
$PatchSolutions = Get-CrmRecordsByFetch -Fetch "<fetch><entity name='solution'><attribute name='solutionid' /><attribute name='friendlyname' /><attribute name='version' /><attribute name='uniquename' /><filter><condition attribute='parentsolutionid' operator='eq' value='$parentSolutionId' /></filter><order attribute='createdon' descending='true' /></entity></fetch>" -conn $Conn -TopCount 1
if ($PatchSolutions.CrmRecords.Count -eq 1) {
$ReturnSolutionName = $PatchSolutions.CrmRecords[0].uniquename
$ReturnSolutionVersion = "_" + $PatchSolutions.CrmRecords[0].version.Replace(".", "_")
Write-Host "Found:" $PatchSolutions.CrmRecords[0].solutionid "-" $ReturnSolutionName "-" $PatchSolutions.CrmRecords[0].version
}
}
Write-Host "Returing:" $ReturnSolutionName
Write-Host "Returing:" $ReturnSolutionVersion
# Create an empty variable with this name (D365SolutionName) - Select option to set at queue time
Write-Host "##vso[task.setvariable variable=D365SolutionName;]$ReturnSolutionName"
# Create an empty variable with this name (D365SolutionVersion) - Select option to set at queue time
Write-Host "##vso[task.setvariable variable=D365SolutionVersion;]$ReturnSolutionVersion"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment