Jump to content

Check for active sessions on server


Recommended Posts

I'm trying to make a script to check whether a user has an open session on a file server. I've tried the following code:

#include <NetShare.au3>
#include <Array.au3>
$shares = _Net_Share_SessionEnum("SERVERHERE", "", "USERNAMEHERE")
_ArrayDisplay($shares)

Which always returns nothing, but when I check the Sessions on the Shared Folders SnapIn the user is connected. The account running the script is a member of the Administrators group. Any ideas?

Link to comment
Share on other sites

Sure it is :)

Since @error it is THE error code, you can add it to the ConsoleWrite() function, just like this:

; Calling the function
If @error Then
    ConsoleWrite("Error detected! Error: " & @error & @CRLF)
EndIf

You can find more about @error code in the Help file, about the function which throws the error.

Just look in the Help file about _Net_ShareSessionEnum() function, and, basing on the @error code returned, you get the cause that generates it :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

This is the Help page about _Net_Share_SessionEnum.

In that page, there are no errors listed, but, as you can see from the MSDN page, you could check the return value of the function.

Just put 

Global $strRetVal = _Net_Share_SessionName(...)
ConsoleWrite("Return Value: & $strRetVal & @CRLF)

So you can see what is the result of the function :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Thanks, but I'm still not any further forward. My code currently looks like:

#include <NetShare.au3>
Global $intRetVal = _Net_Share_SessionEnum("SERVERNAME", "", "USERNAME")
If @error Then
   ConsoleWrite("Error: " & @error)
Else
    ConsoleWrite("Return Value:" & $intRetVal & @CRLF)
EndIf

And the return I'm getting is: "Error: 10"

Looking at the MSDN page you linked above there's no failure return value that is just numbers, they're all in text?

Link to comment
Share on other sites

  • 3 weeks later...

@rossy

My falut.

If the function success, it return an array, and so, you have to display the array with _ArrayDisplay() function; else, if the functions does not success, it sets the @error to non-zero, and that is what it is, since:

On 21/7/2018 at 8:10 PM, rossy said:

I added your above code into my script and I got the number "10" back as the error.

So, there is an error in your function call :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

  • 2 weeks later...
On 07/08/2018 at 7:29 AM, DonChunior said:

@rossy, I can reproduce your error code 10, when I'm using an account that has no administration rights on the server.
When I run the script with an account that has admin rights on the server, it works!

Thank you so much, that's exactly what it was! When running the script, I didn't right click and run as administrator.

For anyone else who may come across this thread, my final code for this was:

#RequireAdmin
#include <NetShare.au3>
#include <Array.au3>
Global $userinshare = _Net_Share_SessionEnum("SERVERHERE", "", "USERNAMEHERE")
If IsArray($userinshare) Then
   MsgBox(-1, "", "User has active sessions on server.")
Else
   MsgBox(-1, "", "User is not connected to server.")
EndIf

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...