D365FO Tool – Automating Start and Stop Environments

If you have Tier1 environments in your project and you would like to automate the starting and stopping of them then Use #d365fotools and get on with your work. two new commands Invoke-D365LcsEnvironmentStart and Invoke-D365LcsEnvironmentStop available in d365 tools (GitHub – richardsondev/d365fo.tools: Tools used for Dynamics 365 Finance and Operations)

— Install d365fo.tools on power shell.
Install-Module -Name d365fo.tools

— Get LCS API Token and saveLCS API Config variables
Get-D365LcsApiToken -ClientId “a56e047a-188c-xxxxxxxxxxx” -Username “serviceaccount@domain.onmicrosoft.com” -Password “strongPassword” -LcsApiUri “https://lcsapi.lcs.dynamics.com” | Set-D365LcsApiConfig -ProjectId 1350683

-Username, -Password : Service account that should have access to LCS project and MFA should be disabled and should not be Federated authentication account (this is to avoid error)
-ClientId : link below is to create the app registration on azure portal, with permission set as shown in picture below.
https://docs.microsoft.com/en-us/graph/auth-register-app-v2

-LcsApiUri – URI / URL to the LCS API you want to use

Depending on whether your LCS project is located in europe or not, there is 2 valid URI’s / URL’s

Valid options: “https://lcsapi.lcs.dynamics.com” “https://lcsapi.eu.lcs.dynamics.com” “https://lcsapi.fr.lcs.dynamics.com” “https://lcsapi.sa.lcs.dynamics.com” “https://lcsapi.uae.lcs.dynamics.com” “https://lcsapi.lcs.dynamics.cn

— This is to stop the environment
Invoke-D365LcsEnvironmentStop -ProjectId 1350683 -EnvironmentId “782f99fe-cad6-467b-860f-xxxxxxx”

— This is to start the environment
Invoke-D365LcsEnvironmentStart -ProjectId 1350683 -EnvironmentId “782f99fe-cad6-467b-860f-xxxxxx”

-Projectid , -EncironmentId: This can get from LCS Project, Project settings page.

Building Azure Pipeline to Start and Stop the VM

You can create a build pipeline and just add a Powershell task and add the script to start the VM

 Install-PackageProvider nuget -Scope CurrentUser -Force -Confirm:$false  
 Install-Module -Name AZ -AllowClobber -Scope CurrentUser -Force -Confirm:$False -SkipPublisherCheck  
 Install-Module -Name d365fo.tools -AllowClobber -Scope CurrentUser -Force -Confirm:$false  
 Get-D365LcsApiToken -ClientId "a56e047a-188c-xxxx-xxxxxxxxxx" -Username "serviceaccount@domain.onmicrosoft.com" -Password "password" -LcsApiUri "https://lcsapi.lcs.dynamics.com" | Set-D365LcsApiConfig -ProjectId 1350683  
 Invoke-D365LcsEnvironmentStart -ProjectId 1350683 -EnvironmentId "782f99fe-cad6-467b-xxxxxxxxxxx"  

Scheduled can be defined on azure pipeline to start and stop the VM

Leave a comment