Hi,
I am wondering how cloud connections should be handled across scripts for things to run as smooth as possible.
For example, if I disconnect from Graph in one script in a scheduled task, and another script in a different schedule task is using Graph, I either get Authentication needed. Please call Connect-MgGraph.
or Object reference not set to an instance of an object.
.
While for Exchange I get Exception calling "GetCurrentConnectionContext" with "1" argument(s): "You must call Connect-ExchangeOnline before calling any other cmdlet."
.
If I stop disconnecting at the end of the scripts, I don't get such errors.
The solution is of course apparently quite simple; don't disconnect, and that's exactly how I have done it for a long time.
However, I am struggling with a memory leak in the Adaxes Service, which I cannot seem to find the root cause for. I have tried to find out if a single script is the culprit or if it is a more widespread problem, by correlating the memory consumption increase with when the various scripts are running. But there is not any clear jumps in memory consumption, but a steady creep, sort of.
Here is a plot of how the number of threads and memory consumption (MB) is growing throughout the lifetime of the Adaxes Service (at about 8GB, all of the servers memory (14GB) is consumed):
Here is for a singe day (14. February):
The growth is most rapid in the morning, which makes sense, since there is definitely most activity then.
To give a sense of the scope, we have about 30 scheduled tasks and about 2500 users. With automated onboarding and offboarding, group related events, property and group member synchronizations, integrations between AD/Adaxes and different systems.
To wrap it up:
- What is the recommended way to handle Graph/Exchange/Teams connections across scripts?
- ExchangeOnline keeps filling the servers temp folder with tmpEXO folders, probably due to not closing connections. Do you know how this can be prevented?
- What resources/modules/connections are shared between scripts?
Should I import modules in each script?
- Do you have any tips for troubleshooting memory leaks?
For reference, this is how I am connecting to the various cloud services:
function Connect-MgGraphAdaxes
{
Param (
[Parameter()]
[int]
$RetryCount = 1
)
try
{
$token = $Context.CloudServices.GetAzureAuthAccessToken("https://graph.microsoft.com")
$tokenSecure = ConvertTo-SecureString -String $token -AsPlainText -Force
$null = Connect-MgGraph -AccessToken $tokenSecure
}
catch
{
if ($RetryCount -gt 0)
{
Connect-MgGraphAdaxes -RetryCount ($RetryCount - 1)
}
else
{
throw
}
}
}
function Connect-ExchangeOnlineAdaxes
{
Connect-ExchangeOnline `
-Organization '<domain>.onmicrosoft.com' `
-AppId '<app ID>' `
-CertificateThumbprint '<certificate thumbprint>'
#Gives "The token has expired" error:
#[void]$Context.CloudServices.ConnectExchangeOnline()
}
function Connect-MicrosoftTeamsAdaxes
{
$graphToken = $Context.CloudServices.GetAzureAuthAccessToken("https://graph.microsoft.com")
$teamsToken = $Context.CloudServices.GetAzureAuthAccessToken("48ac35b8-9aa8-4d74-927d-1f4a14a0b239")
$null = Connect-MicrosoftTeams -AccessTokens @($graphToken, $teamsToken)
}
Best regards,
Martin