Update apps installed from Appsource in Business Central Online with Powershell

Update apps installed from Appsource in Business Central Online with Powershell

This post will show you how you can update apps installed from the Marketplace in Business Central Cloud with Powershell.

In the example below, powershell will be used which will invoke the APIs needed to update the APPs installed from the marketplace. For convenience I added a couple of functions to the BCContainerhelper to make it more convenient, in this way we can take advantage of the token management already present in the module, placed in GitHub.

 

BC Administration APIs

Administration APIs Automation

  • Apps
  • AvailableUpdates
  • Install
  • Upgrade
  • Update
  • Unistall

https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/administration/administration-center-api

 

UPDATE AN APP STATEMENT

POST …/Apps/../update

Getting an access token

 

Getting a token – standard Powershell how-to

PowerShell example without prompt (silent)

$cred = [Microsoft.IdentityModel.Clients.ActiveDirectory.UserPasswordCredential]::new($UserName, $Password)

$ctx = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new(“https://login.windows.net/$TenantName”)

$token = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContextIntegratedAuthExtensions]::AcquireTokenAsync($ctx, “https://api.businesscentral.dynamics.com”, <Application ID>, $cred).GetAwaiter().GetResult().AccessToken

PowerShell example with prompt (interactive)

$ctx = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new(“https://login.windows.net/$tenantName”)

$redirectUri = New-Object -TypeName System.Uri -ArgumentList <Redirect URL>

$platformParameters = New-Object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters -ArgumentList ([Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Always)

$token = $ctx.AcquireTokenAsync(“https://api.businesscentral.dynamics.com”, <Application ID>, $redirectUri, $platformParameters).GetAwaiter().GetResult().AccessToken

 

HOW THE SYSTEM WORKS

The system allows (after Admin authentication) to access some functions exposed as APIs (below are some examples of calls made by the Admin console); calling them as APIs we can automate installation, update, uninstallation even for Appsource APPs.

InstalledApps

App Update Details

GetLastAvailableUpdates

 

Useful calls

Apps List

https://businesscentral.dynamics.com/tenantID/admin/v1.0/environments/BusinessCentral/Production/Apps

Get Latest available Apps

https://businesscentral.dynamics.com/tenantID/admin/v1.0/environments/BusinessCentral/Production/Apps/GetLatestAvailableUpdates

Update Apps

https://businesscentral.dynamics.com/tenantID/admin/v1.0/environments/BusinessCentral/Production/apps/AppID/update

 

INSTALL APPSOURCE APPS USING BCContainerhelper BcAuthContext –includeDeviceLogin

As previously mentioned, it is possible to use BCContainerHelper to access BC Cloud and install the Apps from APPSource; with the new function inserted it is also possible to update APPs it in a simple way.

I attach example script to update a single APP (using a little Powershell with string parsing and loops it is possible to update all available APPs including dependencies with all security checks on what we are updating)

Update-BCAppFromAppSource

 

Example – “Install_Unistall_Update_AppsBCCloud_FromMarktplace Demo.ps1″

#Based on APIs 2.0 Admin standard statements
#Content-Type: application/json - publish
#POST /admin/v2.6/applications/{applicationFamily}/environments/{environmentName}/apps/{appId}/publish

#Content-Type: application/json - install
#POST /admin/v2.6/applications/{applicationFamily}/environments/{environmentName}/apps/{appId}/install

#Content-Type: application/json -unistall
#POST /admin/v2.6/applications/{applicationFamily}/environments/{environmentName}/apps/{appId}/uninstall

#Content-Type: application/json -update
#POST /admin/v2.6/applications/{applicationFamily}/environments/{environmentName}/apps/{appId}/update

#APP e AVAILABLE UPDATES APPS
#GET /admin/v2.6/applications/{applicationFamily}/environments/{environmentName}/apps
#GET /admin/v2.6/applications/{applicationFamily}/environments/{environmentName}/apps/availableUpdates


 
# DEMO Unistall\Install\Update an APP on PRODUCTION Env.
#IMPORT MODULE EXTENSION FOR BCCONTAINERHELPER - INSTALL\UPDATE MODULES
Import-Module "C:\Powershell Script\Update-BcAppFromAppSource.ps1"
Import-Module "C:\Powershell Script\Unistall-BcAppFromAppSource.ps1"
Import-Module "C:\Powershell Script\AvailableAppsUpdates-BCAppSource.ps1"


#TOKEN, ENVIRONMENT, APPID E NAME
$authContext = New-BcAuthContext –includeDeviceLogin -verbose   #GET TOKEN
$environment = "Production"
$appId = "12233434545454545"  #GLLOBAL APP
$appName = "APP NAME"

#Read last version for a published APP
$AppName = Get-BcPublishedApps -bcAuthContext $authContext -environment $environment | Where-Object { $_.Name -eq "APP NAME" }  
echo $AppName

#Read last version for an available for uodate APP
$AppName = Get-BcAvailableAppsUpdates -bcAuthContext $authContext -environment $environment | Where-Object { $_.Name -eq "APP NAME" }  
echo $AppName


#Installed APP, ToUpdate APP
$InstalledAppVersion = "2.1.1.0"
$ToUpdateAppVersion = "2.1.1.1"  #TARGET APP


#STATEMENTS
#1) - UPDATE AN APP 
Update-BcAppFromAppSource `
    -appId $appId `
    -bcAuthContext $authContext `
    -environment $environment `
    -acceptIsvEula `
    -allowInstallationOnProduction `
    -ToNewAppVersion $ToUpdateAppVersion `
    -installOrUpdateNeededDependencies `
    -languageId 1040 `
    -verbose
        

#2) - UNISTALL AN APP
Uninstall-BcAppFromAppSource `
    -appId $appId `
    -bcAuthContext $authContext `
    -environment $environment `
    -acceptIsvEula `
    -allowInstallationOnProduction `
    -appVersion $InstalledAppVersion `
    -installOrUpdateNeededDependencies `
    -languageId 1040 `
    -verbose


#3) - INSTALL AN APP
Install-BcAppFromAppSource `
    -appId $appId `
    -bcAuthContext $authContext `
    -environment $environment `
    -acceptIsvEula `
    -allowInstallationOnProduction `
    -appVersion $InstalledAppVersion `
    -installOrUpdateNeededDependencies `
    -languageId 1040 `
    -verbose

Source demo Link

https://github.com/rstefanetti/Powershell/tree/BC-APPS-UpdateFromAppSource/APP

3 thoughts on “Update apps installed from Appsource in Business Central Online with Powershell

  • 9 May 2021 at 6:30 AM
    Permalink

    Hi Roberto,
    Thanks for the blog post.

    I have a remark about the spelling of “Uninstall” you use Unistall and Unistall as well.

    In
    Unistall-BcAppFromAppSource.ps1

    I think the parameter is uninstallDependents not installOrUpdateNeededDependencies

    Reply
    • 9 May 2021 at 7:12 AM
      Permalink

      yes, you are are right.. fast posting
      Please suggest bugs\corrections in GitHub, i will check and merge

      Final Script
      The finale script is in development… actually i’m writing a full script like this:
      Autotoken, Apps loop, error handling etc.

      name: #MULTI APPS INSTALL\UPDATE ON BC CLOUD
      $BaseApppath = “$($PSScriptRoot)\”
      $MarktApps= …etc. etc.

      Reply
  • 10 May 2021 at 7:46 PM
    Permalink

    Thanks a lot for sharing Roberto!! I read all and I can´t get all. Does this post mean you can automatize your SAAS app update from AppSource? This would be very useful, because we are overwhelmed updating manually AppSource, if we can´t wait to minor next.

    Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.