Automatisering av utrullning av lösningsfiler Dynamics CRM

För att automatisera utrullning kan man använda sig av det fina biblioteket AdxStudio ALM Toolkit.

Här kommer ett exempel på ett PowerShell-script som man kan använda för att rulla ut sina lösningar:

param (
	[string]
	$environment
)

function Get-ScriptDirectory
{
	if ($script:MyInvocation.MyCommand.Path) { Split-Path $script:MyInvocation.MyCommand.Path } else { $pwd }
}

$scriptPath = Get-ScriptDirectory
& $scriptPath\ImportAlmModule.ps1

$crmenvs = @(
	[PSCustomObject]@{ Environment = "TEST"; Url = "https://crmhostname.tst/CRMORGANIZATION" },
	[PSCustomObject]@{ Environment = "UAT"; Url = "https://crmhostname.uat/CRMORGANIZATION" },
	[PSCustomObject]@{ Environment = "PROD"; Url = "https://crmhostname.prd/CRMORGANIZATION" }
)

if (-not $environment)
{
    $crmenvs | Format-Table

    Write-Host "Usage: " $script:MyInvocation.MyCommand.Name "-environment Environment"
}
else 
{

    $ImportSolutions = @(
        "$scriptPath\Customizations\clickdimensions.zip",
        "$scriptPath\Customizations\Solution_$Env:BUILD_BUILDNUMBER.zip",
        "$scriptPath\Customizations\SolutionSilverlight_$Env:BUILD_BUILDNUMBER.zip",
        "$scriptPath\Customizations\SolutionPlugins_$Env:BUILD_BUILDNUMBER.zip"
    )

    $crmenv = $crmenvs.Where{ $_.Environment -like $environment}

    Try 
	{
        $credential = New-Object System.Management.Automation.PSCredential -ArgumentList "username", (ConvertTo-SecureString -String “password” -AsPlainText -Force)

        $srcconn = Get-CrmConnection -Url "https://crmhostname.utv/CRMORGANIZATION" -Credential $credential
        $dstconn = Get-CrmConnection -Url $crmenv.Url -Credential $credential

        Write-Host "Exporting solution:"
        $res = Export-CrmSolution -Connection $srcconn -SolutionName "Solution" -OutputPath "$scriptPath\Customizations\Solution_$Env:BUILD_BUILDNUMBER.zip" -Managed -TargetVersion "6.1.0.0" -Verbose
        $res = Export-CrmSolution -Connection $srcconn -SolutionName "SolutionSilverlight" -OutputPath "$scriptPath\Customizations\SolutionSilverlight_$Env:BUILD_BUILDNUMBER.zip" -Managed -TargetVersion "6.1.0.0" -Verbose
        $res = Export-CrmSolution -Connection $srcconn -SolutionName "SolutionPlugins" -OutputPath "$scriptPath\Customizations\SolutionPlugins_$Env:BUILD_BUILDNUMBER.zip" -Managed -TargetVersion "6.1.0.0" -Verbose

        Write-Host "Importing solution:"
        Import-CrmSolution -Connection $dstconn -CustomizationPath $ImportSolutions -OverwriteUnmanagedCustomizations -PublishWorkflows -Verbose
    }
    Catch
    {
        $errmsg = "Error: " + $_.Exception.Message # $_.Exception.ItemName " Message: "
        Write-Error -Message $errmsg
        Break
    }
}