Skip to content

Guidance on Invoke-D365LcsApiRefreshToken usage #736

@smholvoet

Description

@smholvoet

Hi 👋

I'm using this module to apply quality updates to our (rather extensive collection of 🥴) cloud-hosted environments. This works great but I'm not sure how I should make use of the Invoke-D365LcsApiRefreshToken cmdlet.

Pseudo code of the steps we're going through:

1. authenticate via Get-D365LcsApiToken | Set-D365LcsApiConfig, this gets me a valid token for 1 hour
2. fetch details of the target environment, as we need the environment ID later on
3. fetch details of the LCS asset which we're going to deploy, as we need the asset ID later on
4. start the VM
5. meat and bones of the script:
 a. start the deployment via Invoke-D365LcsDeployment
 b. poll the deployment status every 5 minutes, timeout after 6 hours
 c. check for the deployment outcome* ⚠
6. stop the VM* ⚠

* these steps never get executed as I'm no longer authenticated against the LCS API

Steps 1 through 5a work fine, but I'm running into issues as the token expires after 60 min, which is right around the time when the deployment has started and I'm in the process of polling the deployment status every 5 minutes:

Write-Host "Successfully started deployment of asset '$($AssetName)' to environment '$($TargetEnvironmentName)' with activity id '$($DeploymentOperation.ActivityId)'"
Write-Host "Waiting for deployment to complete..."
$DeploymentStatus = Get-D365LcsDeploymentStatus -ActivityId $DeploymentOperation.ActivityId -EnvironmentId $TargetEnvironment.EnvironmentId -SleepInSeconds 0

# Poll deployment status every 5 min, timeout after 6 hours
while ( ($DeploymentStatus -ne "Completed") -and ($DeploymentPollingCount -lt 72) ) {
    $DeploymentStatus = Get-D365LcsDeploymentStatus -ActivityId $DeploymentOperation.ActivityId -EnvironmentId $TargetEnvironment.EnvironmentId -SleepInSeconds 300
    Write-Host "Status: $($DeploymentStatus.OperationStatus)"
    $DeploymentPollingCount++
} 

What would be the best way to incorporate the Invoke-D365LcsApiRefreshToken cmdlet in my script? Adding it to the above while loop would mean that it would refresh every 5 min which I guess would be overkill?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions