Hi folks

I want to monitor the lastdirsync status of Azure AD Connect with PRTG. For this I have written a ps1 script and get the minutes of the last sync correctly output in integer numbers. Now when I output the script in PRTG, PRTG never gives me the correct number. Either the number counts up after each update or the number is arbitrary.

If I fill the variable in the script for output to PRTG with a simple 1+1 calculation, the result is also correct in PRTG. I have a lot of scripts running that give me numbers. Only with the error I just do not come further.

Maybe someone here has an idea.

Here is the script:

$certificate = "xxx" $appid = "xxx" $tenantid = "xxx"

Connect-MgGraph -CertificateThumbprint $certificate -ClientId $appid -TenantId $tenantid

$lastDirSync = Get-MgOrganization | select OnPremisesLastSyncDateTime

$lastDirSyncCut = $lastDirSync.OnPremisesLastSyncDateTime[0]

$CurrentDateandTime = get-date $Minutes = ($lastDirSyncCut - $CurrentDateandTime).Minutes

$output = [math]::Abs($Minutes)

"<prtg>" " <result>" " <channel>Last Directory Sync in Min</channel>" " <value>$output</value>" " </result>" "</prtg>"


Article Comments

Hello Rene,

Thank you for the post, can you try explicitly emptying "$output" at the beginning of the script?

Regards. Oscar


Aug, 2022 - Permalink

Hi Oscar,

the same result. It looks like the sensor is just counting up. It counts up to 59 every hour and then drops down to 2 and starts over.

Regards, René


Aug, 2022 - Permalink

Hello Rene,

If you run the script several times without PTRG in the picture does it show the right data?

What does it look like after adding the emptying section at the beginning?

To investigate further we also need to check sensor settings and need screenshots, I recommend you to open a ticket by reaching out to support@paessler.com


Aug, 2022 - Permalink

The problem was solved during ticket processing. But anyway, I would like to announce the result here in case someone else encounters the problem.

The problem was that the PRTG service or even the PRTG user account that started the query on the sample did not get the certificate. The solution is simple. In the script, the certificate must first be read and then processed.

$CertThumbprint = "1234567890" $cert = Get-ChildItem Cert:\LocalMachine\My\$CertThumbprint

Connect-MgGraph -TenantId "xxx" -ClientID "xxx" -Certificate $cert


Sep, 2022 - Permalink

Hello Rene,

Thank you for letting us know.

Regards, Oscar


Sep, 2022 - Permalink

Hi Oscar, no problem


Sep, 2022 - Permalink