How can I view via NetSessionEnum() all the current active sessions on a Domain Controller?

Folks -

I'd like a simple script I can run that uses the Win32 NetSessionEnum() call to enumerate all the active sessions on a particular Domain Controller.  I'm interested in seeing the computers and the usernames for each computer that have an active session.  This call should provide this information.

I'd prefer a simple VBScript but a Powershell script is fine too.  The script would be run locally on the machine with full administrative rights.  No need for remote connectivity.

If there's a better way to see this information, please let me know.
Who is Participating?
OsmozeConnect With a Mentor Commented:
VB.NET example
<DllImportAttribute("Netapi32.dll", EntryPoint:="NetSessionEnum", SetLastError:=True)> _
Public Shared Function NetSessionEnum(<InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal servername As String, _
<InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal UncClientName As String, _
<InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal username As String, _
ByVal level As UInteger, <OutAttribute()> ByRef bufptr As IntPtr, ByVal prefmaxlen As Integer, _
<OutAttribute()> ByRef entriesread As Integer, <OutAttribute()> ByRef totalentries As Integer, _
<InAttribute(), OutAttribute()> ByRef resume_handle As IntPtr) As NET_API_STATUS
End Function

refer to 

Chris DentConnect With a Mentor PowerShell DeveloperCommented:
Hmm yeah, that's a long way from being simple in PS, and not even possible in VbScript (unless someone wrote a wrapper to help VbScript out). You'd have to convert the samples here into PowerShell to make headway:

It's entirely possible, but not something I have time to do until Thursday (this is here in case anyone else wants to). Even then, it'll be less work to use Add-Type in PS and keep the majority of it as C# / VB .NET (rather than PS itself).

If anyone happens to need an example of that kind of thing, here's one I made for DHCP:

And if you don't get a response, ping me on Thursday :)

amendalaAuthor Commented:
I'd forgotten about the PSTool PSLoggedOn, that helped immensely.

I also whipped up a C# app to implement the call per some of the documentation above.  Both applications return slightly different results but that's due to the manner in which they obtain the information.  PSLoggedOn does not rely exclusively on NetSessionEnum().

Unfortunately, I cannot share the code I wrote due to workplace IP restrictions.  Sorry.

Thanks for the help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.