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

Custom EXE Batch Sensor for counting files in a particular folder

Votes:

0

I'm working on an problem where I need to count the number of .PDF files located in a particular folder and use a sensor to report that number. The sensor should return the number and if the number of .PDF files is > 1, create an error event. I messed around with the Folder sensor and File sensors but those don't appear to meet my needs, so I landed on trying to create my own custom sensor. I figured I could just write a simple batch script to count the files in the folder I want and return that integer via the custom exe sensor and viola, problem solved! However, I'm running into a couple of different problems. First, here is the initial code for the batch file I made:

@echo off

set /P folderloc=
set /a count=0
for /f "tokens=* delims= " %%a in ('dir/b/a-d "%folderloc%\*.pdf"') do (
set /a count+=1
)

echo %count%

The .bat files executes, requires a folder location be entered, then runs this loop to catch how many .PDF files are in this folder and returns the count. I've tested it, it works great. Now, I placed this .bat in the custom sensors/exe folder on the probe that hosts the device I'll be running this sensor on and went off to create the sensor. In the sensor settings, I assumed that the 'Parameters' field was where I would put my user input (ie the folder location), but the senor doesn't like that apparently so here is my first dilemma. Here is an example of what I placed in the Parameters field:

C:\Users\PDF Folder\

My first questions is, am I using this parameters field correctly? I tried different variations of the folder location to see if maybe I had the syntax wrong. Since the script requires folderloc to be defined by a user, would that user input not go into the parameters field? If not, is there any way around this? The error I get in return is just a time out error. So it appears to just be running with nothing happening until the timeout time of 60s.

Since I couldn't get that to work, I thought I'd go ahead and make sure I could get this working if I placed the directory location directly into the script like this:

set /a count=0
for /f "tokens=* delims= " %%a in ('dir/b/a-d "C:\Users\PDF Folder\*.pdf"') do (
set /a count+=1
)

echo %count%

I then created the sensor the same way but left the parameters field empty. It worked just like I wanted and returns a single integer value to my sensor. Excellent! Now dilemma number 2. I obviously don't care about any folders on my probe host and need this check to run on one of the devices that connect to the probe. This is where I think I may be in trouble because the .bat sits on the probe device but needs to read a folder location on the child device. With my working example above, I tried using the $ symbol to indicate I want to grab the folder from the child device, like how you would do it in a folder sensor.

set /a count=0
for /f "tokens=* delims= " %%a in ('dir/b/a-d "C$\Users\PDF Folder\*.pdf"') do (
set /a count+=1
)
echo %count%

Unfortunately, this failed as well and I think it's because this is running straight from the probe device. I was hoping if I could get the parameters in the sensor settings working properly, I would be able to connect to the correct directory since that's how it's handled on other sensors. Ideal scenario would look like this:

set /P folderloc=
set /a count=0
for /f "tokens=* delims= " %%a in ('dir/b/a-d "%folderloc%\*.pdf"') do (
set /a count+=1
)

echo %count%

Under custom exe settings, the Parameters field would have something like this: C$\Users\PDF Folder

Any help would be appreciated!

batch custom-exe-script file folder prtg script

Created on Mar 10, 2022 7:52:09 PM

Last change on Mar 14, 2022 6:41:10 AM by  Felix Wiesneth [Paessler Support]



4 Replies

Votes:

1

Hello Jason,

Thank you for your message. Please, note that we do not provide official support for scripting and customizations therefore I hope some PRTG users can also help here.

Nevertheless, regarding the parameter "C:\Users\PDF Folder\", you encountered an issue due to the space between "PDF" and "Folder". Therefore, the script interpreted them as two distinct arguments. When there is a space in the value to provide, I invite you to use quotes "" to avoid this issue.

Regarding the remote aspect of your script, unfortunately I can't provide help with batch however you have the possibility to easily execute remote commands with PowerShell from the probe server. You can do so by executing the cmdlet Invoke-Command for example.

$user = ""
$passwd = ""
$credentials = New-Object System.Management.Automation.PSCredential ("$user", (ConvertTo-SecureString "$passwd" -AsPlainText -Force))
Invoke-Command -ComputerName "IP/DNS" -Credential $credentials -ScriptBlock {
# Your code here
}

Regards.

Created on Mar 15, 2022 2:13:56 PM by  Florian Lesage [Paessler Support]

Last change on Mar 15, 2022 2:18:12 PM by  Florian Lesage [Paessler Support]



Votes:

0

Thanks Florian,

The spacing issue with the folder name isn't really an issue for me as I was using that as an example and looking at my real directory path I'm using, there aren't any spaces like that. I appreciate the tip though. It'll be helpful in the future.

In regards to powershell, the Invoke-Command would be perfect in this scenario but I think now I'm going to run into access issues. The passwords for the servers I'd be trying to connect to are pretty locked down by our info sec department so I wouldn't have them on hand to enter here directly into the script. That's partly why I was trying to connect to the host from directly in the host's sensor via the parameters field but it's looking like I may be out of luck on that front. From what I'm gathering, there really isn't a way to run a script that sits on the probe machine that does something on the host machine w/out inputting the credentials again. Correct?

Thanks, Jason

Created on Mar 15, 2022 6:48:35 PM



Votes:

0

Hi Jason,

You do not have to write the credentials and address in plain text within the script, you have the possibility to use the ones defined in the parent device settings, under Basic Device Settings and Credentials for Windows systems in PRTG).

To do so, simply add the following placeholders in the field Parameters of the EXE/Script or EXE/Script Advanced sensor:

"%host" "%windowsuser" "%windowspassword"

Then, you can use the following template to get the address and credentials of the target in the script, without having them in plain text:

param(
[string]$target,
[string]$user,
[string]$passwd
)

$credentials = New-Object System.Management.Automation.PSCredential ("$user", (ConvertTo-SecureString "$passwd" -AsPlainText -Force))
Invoke-Command -ComputerName "$target" -Credential $credentials -ScriptBlock {
# Your code here
}

Let me know if you have further concerns.

Created on Mar 16, 2022 3:00:36 PM by  Florian Lesage [Paessler Support]

Last change on Mar 16, 2022 3:00:59 PM by  Florian Lesage [Paessler Support]



Votes:

0

Hello Jason,

Please take into account that the PRTG probe runs on the Local System account. Where it comes to accessing files and/or folders, this account, as the name already suggests, does not get you any further than folders on the PRTG probe machine.

This is why you will always need to use credentials of an account with sufficient rights to access the server\folder.

I would suggest creating a shared folder for the server\folder you like to monitor and use a special account, e.g. 'PRTGSensor' with read-only rights for this folder.

Another thing to avoid is using \\server\c$ as c$ is an administrative share that's only available using admin credentials.


Sensors | Multi Channel Sensors | Tools | Notifications

Kind regards,

[[https://prtgtoolsfamily.com]] PRTG Tools Family

Created on Mar 16, 2022 4:15:38 PM




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.