[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1846
  • Last Modified:

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.
  • 2
2 Solutions
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 http://msdn.microsoft.com/en-us/library/bb525382(v=vs.85).aspx 

Chris DentPowerShell 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.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now