What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Sensor to count users exe/script advanced

Votes:

0

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>"

prtg script sensor

Created on Mar 14, 2022 6:47:51 PM

Last change on Mar 15, 2022 6:31:24 AM by  Florian Lesage [Paessler Support]



3 Replies

Votes:

0

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.

Created on Mar 15, 2022 6:41:54 AM by  Florian Lesage [Paessler Support]

Last change on Mar 15, 2022 6:43:02 AM by  Florian Lesage [Paessler Support]



Votes:

0

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)

Created on Mar 15, 2022 2:53:45 PM



Votes:

0

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).

Created on Mar 16, 2022 9:36:37 AM by  Florian Lesage [Paessler Support]




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.