How to Programmatically List Active User Connections to a Windows Server 2008 Network Share


1 views

When managing a Windows Server 2008 file share, administrators often need to monitor active connections. The process involves querying Server Message Block (SMB) sessions programmatically. Here's how to achieve this through various methods:

The simplest way to view connected users:

net session

Sample output:

Computer               User name            Client Type       Opens Idle time
----------------------------------------------------------------------------
192.168.1.15           DOMAIN\jsmith       Windows 10        3     00:15:22
192.168.1.23           DOMAIN\bjones       Windows Server    1     01:45:10

For more detailed information and automation:

$sessions = Get-SmbSession
foreach ($session in $sessions) {
    Write-Host "Client: $($session.ClientComputerName)"
    Write-Host "User: $($session.ClientUserName)"
    Write-Host "Protocol: $($session.Dialect)"
    Write-Host "Connected since: $($session.CreateTime)"
    Write-Host "---------------------"
}

For programmatic access via C#:

using System;
using System.Management;

class ShareMonitor {
    static void Main() {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(
            "SELECT * FROM Win32_ServerConnection");
            
        foreach (ManagementObject conn in searcher.Get()) {
            Console.WriteLine("User: " + conn["UserName"]);
            Console.WriteLine("Computer: " + conn["ComputerName"]);
            Console.WriteLine("Share: " + conn["ShareName"]);
            Console.WriteLine("----------------------");
        }
    }
}

Create a monitoring script that logs changes:

$previous = @{}
while ($true) {
    $current = Get-SmbSession | Group-Object -Property ClientUserName
    
    # Compare with previous state
    $new = Compare-Object $previous $current -Property Name
    
    if ($new) {
        $new | ForEach-Object {
            Write-EventLog -LogName "Application" -Source "ShareMonitor" 
                -EntryType Information -EventId 1001 
                -Message "Connection change detected: $($_.Name)"
        }
    }
    
    $previous = $current
    Start-Sleep -Seconds 30
}

When implementing these solutions:

  • Always run with elevated privileges
  • Consider implementing rate limiting for monitoring scripts
  • Log sensitive information securely
  • Use try-catch blocks for error handling

If you encounter issues:

  • Verify the Server service is running (net start lanmanserver)
  • Check firewall settings for SMB ports (445 TCP)
  • Confirm your account has "Manage server" privileges

As a Windows Server administrator, monitoring active connections to your file shares is crucial for both security and maintenance purposes. Windows Server 2008 provides several methods to view currently connected users.

The simplest GUI method is through Computer Management:

1. Open Computer Management (Right-click Computer > Manage)
2. Navigate to: System Tools > Shared Folders > Sessions
3. View all active connections in the right pane

For quick terminal access, the net session command provides connection details:

C:\> net session

Computer               User name            Client Type       Opens Idle time
-----------------------------------------------------------------------------
\\192.168.1.15         DOMAIN\jsmith        Windows 10        3     00:15:22
\\192.168.1.20         DOMAIN\ajohnson      Windows 7         1     01:03:45

For more advanced filtering and output control, use PowerShell:

PS C:\> Get-SmbSession | Format-Table -AutoSize

ClientComputerName ClientUserName Credential             Dialect NumOpens
------------------ -------------- ----------             ------- --------
WIN10-PC1          DOMAIN\jsmith  DOMAIN\jsmith          3.1.1   4
WIN7-PC2           DOMAIN\dwilson DOMAIN\dwilson         2.1     2

To check connections for a particular share only:

PS C:\> Get-SmbOpenFile | Where-Object { $_.Path -like "*AccountingShare*" } | 
        Select-Object ClientComputerName,ClientUserName,Path

ClientComputerName ClientUserName Path
------------------ -------------- ----
WIN10-PC1          DOMAIN\jsmith  C:\Shares\AccountingShare\Budget.xlsx

Create a scheduled task with this PowerShell script to log connections hourly:

$logPath = "C:\ShareAccessLogs\$(Get-Date -Format 'yyyyMMdd').csv"
Get-SmbSession | Export-Csv -Path $logPath -Append -NoTypeInformation

When managing share connections:

  • Audit logs should be reviewed regularly
  • Unexpected connections should be investigated
  • Consider implementing session timeouts for idle users