I'm sure the script itself is fine but the parameters at the end for prtg I'm sure I'm getting wrong. What do you guys think?

$OU = "OU=test,OU=test,DC=test,DC=com"

  1. script to search active directory
$users = Foreach($OU in $OUs){
    Get-ADUser -filter {enabled -eq $true} -SearchBase $OU -Properties lastlogondate | where-object {$_.lastlogondate -lt (get-date).AddDays(+60)} | Select-Object SamAccountName
}

#Count results
$a = @($users.count)

#Write to prtg
Write-Host "<prtg>"
Write-Host "<result>" 
"<channel>Users Count</channel>" 
"<value>"$a"</value>" 
"</result>"
Write-Host "</prtg>"

Article Comments

Hello,

Thank you for your message.

Please, note that we can provide limited support regarding customisations. To easily troubleshoot issues, I invite you to use a try catch statement and then return the error in the sensor message field, as illustrated below:

try {
    $users = Foreach ($OU in $OUs) {
        Get-ADUser -filter { enabled -eq $true } -SearchBase $OU -Properties lastlogondate | where-object { $_.lastlogondate -lt (get-date).AddDays(+60) } | Select-Object SamAccountName
    }

    #Count results
    $a = @($users.count)

    #Write to prtg
    Write-Output @"
<prtg>
<result>
<channel>Users Count</channel>
<value>$a</value>
</result>
</prtg>
"@

}
catch {
    Write-Output @"
<prtg>
<error>1</error>
<text>$($_.exception.message) At line : $($_.InvocationInfo.ScriptLineNumber)</text>
</prtg>
"@
}

The code above also uses Write-Output to return the data to PRTG instead of Write-Host which can generate issues with the PowerShell security enhancements added to PRTG.

Finally, make sure to copy the script in the EXEXML ("C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML") folder of the corresponding probe server and add an EXE/Script Advanced sensor to use it.

Regards.


Mar, 2022 - Permalink

Thank you for your help! I made the modifications you recommended but it seems to be throwing the same error as previous without much change. The PS1 placement was correct thankfully.

xml: The returned xml does not match the expected schema. (Code: PE233) - JSON: The returned JSON does not match the expected structure (Invalid JSON). (Code: PE231)


Mar, 2022 - Permalink

I suspect the variable $a to be incorrect and breaking the output. I invite you to execute the script manually and check which value it returns as well as its type (string, array, etc.) with $a | Get-Member

Can you please also provide the output when executing the script manually with the PowerShell console (x86).


Mar, 2022 - Permalink