New Question
 
 
PRTG Network Monitor

Intuitive to Use.
Easy to manage.

200.000 administrators have chosen PRTG to monitor their network. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free PRTG
Download >>

 

What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general. You are invited to get involved by asking and answering questions!

Learn more

 

Top Tags


View all Tags


Can you monitor server share sessions with PRTG?

Votes:

0

Your Vote:

Up

Down

I need to monitor how many share sessions are on my fileservers. Is there a way to do this with PRTG?

custom-script-exe custom-sensor file-monitoring powershell prtg shares

Created on Aug 27, 2013 1:50:01 PM by  Greg Campion [Paessler Support]

Last change on Mar 19, 2015 3:41:48 PM by  Martina Wittmann [Paessler Support]



13 Replies

Accepted Answer

Votes:

1

Your Vote:

Up

Down

This article applies to PRTG Network Monitor 13 or later

Fileshare Active Sessions Sensor

Using the code below in an EXE/Script Advanced sensor will allow you to query a server and find out

  • how many unique users have share sessions open,
  • how many unique computers have unique sessions open,
  • and the total number of share sessions for the computer.

Note: For this sensor to work, you need to define the computer's name in the parameters field. You can also enter in multiple computer names and gather the stats for all of the defined servers in total. You can also use the %host variable here to use the parent device in the sensor.

$computer = $args

$namespace = "root\CIMV2"
$userSessions = Get-WmiObject -class Win32_ServerConnection -computername $computer -namespace $namespace

$users=$usersessions | select UserName -unique
$users=$users.count

$Sessions=$usersessions | select ConnectionID -unique
$Sessions=$Sessions.count

$Computers=$usersessions | select ComputerName -unique
$Computers=$Computers.count

Write-Host "<prtg>"
"<result>" 
"<channel>Total Users connected to Shares</channel>" 
    
"<value>"+ $users +"</value>" 
"</result>"

"<result>" 
"<channel>Total Computers connected to Shares</channel>" 
"<value>"+ $Computers +"</value>" 
"</result>"

"<result>" 
"<channel>Total Share Sessions</channel>" 
"<value>"+ $Sessions +"</value>" 
"</result>"

"<text>" + $Sessions + " Total Sessions</text>"
Write-Host "</prtg>"

Created on Aug 27, 2013 1:55:00 PM by  Greg Campion [Paessler Support]

Last change on Jun 25, 2014 4:42:05 PM by  Greg Campion [Paessler Support]



Votes:

0

Your Vote:

Up

Down

I found a couple additional things needed to be done for this to work:

1. You'll need to allow the execution of Powershell scripts on the probe system via "Set-ExecutionPolicy Unrestricted" or the script won't run. On a x64 server I had to do this specifically in the x86 PowerShell instance.

2. The server name needs to be entered in the parameters field (as noted) with the name surrounded in quotes.

3. Change "Security Context" to "Use Windows Credentials of parent device."

James

Created on Aug 28, 2013 6:53:11 PM by  James Prudente (0) 1



Votes:

0

Your Vote:

Up

Down

Thanks for posting that information James!

Created on Aug 29, 2013 2:48:46 PM by  Greg Campion [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Is there a need to modify the above script for monitoring a remote server?

I was successful applying the demo script, prtg provides, to the target server, following the hints from james.

here is what I did, when I tried to add a second script with the content from above:

I set the security information in powershell as requested. I copied the demo batchfile prtg provides and replaced the content with the script from above. I added the advanced exe sensor to the target machine I want the information from, in our case a windows 2008 file server. I entered the FQDN of the server into parameters, like 'example.local' I chose, as written above parent security context.

I received the "Error reading response: missing /prtg" Error.

Is there anything else I have to take care of?

I assume that the given script from above is still valid, as I am not firm with scripting...

thank you in advance for your response

regards Oliver

Created on Sep 12, 2014 7:21:45 AM by  roadtek (0) 1



Votes:

0

Your Vote:

Up

Down

Can you run the script directly in PowerShell? Please try to replace the first line with param($computer)

...and see if PRTG executes it properly? At least that was needed to make it run in PowerShell without errors. Seems like the $args variable is no longer used, hence it's not working anymore.

You should be able to call it like this:
.\Get-Shares.ps1 your-server-name

Created on Sep 17, 2014 12:35:02 PM by  Stephan Linke [Paessler Support]

Last change on Sep 17, 2014 12:36:25 PM by  Stephan Linke [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Has anyone seen this error when trying to use the above script? I can run the powershell script with no issues from the server running the PRTG local probe

XML: Structural error in xml file, 1 open items. -- JSON: The returned json does not match the expected structure (Invalid JSON.). (code: PE231)

Created on Dec 18, 2015 3:15:14 PM by  bhansen70 (0) 1



Votes:

0

Your Vote:

Up

Down

Can you post here was the result looks like when you run the script with Powershell?

Created on Dec 21, 2015 9:49:05 AM by  Greg Campion [Paessler Support]



Votes:

0

Your Vote:

Up

Down

I am impressed with this script; however I have all "0" in all fields even thought I am connected to shares and have files opened - any thoughts?

Created on May 16, 2017 12:17:48 PM by  AdamG (20)



Votes:

0

Your Vote:

Up

Down

Dear AdamG

Please enable the sensor option "Write Exe result to disk". The logs appear in "C:\ProgramData\Paessler\PRTG Network Monitor". Do you see a powershell / WMI error message in the script result?

Created on May 17, 2017 11:47:45 AM by  Arne Seifert [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Thank You Arne, I was able to fix that with submitting ticket and get help, but wonder if we can use similar script which shows user names (AD) connected to file server, now I see only digits but would be wonderful if I see what users are connected - names. Person who is helping me send me:

$computer = "localhost"

$namespace = "root\CIMV2" $userSessions = Get-WmiObject -class Win32_ServerConnection -computername $computer -namespace $namespace

$users=$usersessions | select UserName -Unique $users_list=($users | select -expand UserName) -join ", " $users_count=$users.count

$Sessions=$usersessions | select ConnectionID -unique $Sessions=$Sessions.count

$Computers=$usersessions | select ComputerName -unique $Computers=$Computers.count

Write-Host "<prtg>" "<result>" "<channel>Total Users connected to Shares</channel>"

"<value>"+ $users_count +"</value>" "</result>"

"<result>" "<channel>Total Computers connected to Shares</channel>" "<value>"+ $Computers +"</value>" "</result>"

"<result>" "<channel>Total Share Sessions</channel>" "<value>"+ $Sessions +"</value>" "</result>"

"<text>" + $Sessions + " Total Sessions - Usernames: " + $users_list + "</text>" Write-Host "</prtg>"

But I have none data - all sensors shows 0 - any thoughts?

Created on May 19, 2017 1:50:36 AM by  AdamG (20)



Votes:

0

Your Vote:

Up

Down

Dear AdamG

What is the exact issue? Getting no names, or getting zero values? Only the sensor status message can have text (channel data only stores numbers, not strings.) If the message string is empty, or if you get no data in the first place, please again enable the Write Exe result to disk option to see there is an error during the script execution.

Created on May 19, 2017 3:16:06 PM by  Arne Seifert [Paessler Support]

Last change on May 19, 2017 3:18:53 PM by  Arne Seifert [Paessler Support]



Votes:

0

Your Vote:

Up

Down

I have found very usable script but after spending some time I don't know how to implement into this custom sensor:

$computer = $args $namespace = "root\CIMV2" $userSessions = Get-WmiObject -class Win32_ServerConnection -computername $computer -namespace $namespace

if($userSessions -ne $null)

{ Write-Host "The following users are connected to your PC: "

foreach ($userSession in $userSessions) { $userDetails = [string]::Format("User {0} from machine {1} on share: {2}", $userSession.UserName, $userSession.ComputerName, $userSession.ShareName) Write-Host $userDetails }

Write-Host }

Output is:

The following users are connected to your PC: User test from machine 1.1.1.1 on share: test User test2 from machine 1.1.1.2 on share: test2

But after changing script to:

$computer = $args $namespace = "root\CIMV2" $userSessions = Get-WmiObject -class Win32_ServerConnection -computername $computer -namespace $namespace

$users=$usersessions | select UserName -unique $users_list=($users | select -expand UserName) -join ", " $users=$users.count

  1. $userDetails = [string]::Format("User {0} from machine {1} on share: {2}", $userSession.UserName, $userSession.ComputerName, $userSession.ShareName)

$Sessions=$usersessions | select ConnectionID -unique $Sessions=$Sessions.count

$Computers=$usersessions | select ComputerName -unique $Computers=$Computers.count

if($userSessions -ne $null)

{ Write-Host "<prtg>"

"<result>" "<channel>Connection Details</channel>" foreach ($userSession in $userSessions) { $userDetails = [string]::Format("User {0} from machine {1} on share: {2}", $userSession.UserName, $userSession.ComputerName, $userSession.ShareName)

  1. Write-Host $userDetails "<value>"+ $userDetails +"</value>" }

"</result>"

"<result>" "<channel>Total Users connected to Shares</channel>" "<value>"+ $users +"</value>" "</result>"

"<result>" "<channel>Total Share Sessions</channel>" "<value>"+ $Sessions +"</value>" "</result>"

"<result>" "<channel>Total Computers connected to Shares</channel>" "<value>"+ $Computers +"</value>" "</result>"

"<text> Connection Details: "+ $userDetails +"</text>"

Write-Host "</prtg>" }

I have in text field only last line listed in all shares - I am missing something simple.....

Created on May 19, 2017 3:52:05 PM by  AdamG (20)



Votes:

0

Your Vote:

Up

Down

Dear AdamG

To debug this script, please manually output the important variables like $userSessions and $users. Check if the content is correct. If that is the case, please check how you process it for the XML return.

Created on May 22, 2017 1:05:23 PM by  Arne Seifert [Paessler Support]



Please log in or register to enter your reply.


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.