代码之家  ›  专栏  ›  技术社区  ›  Tangiest

确定使用PowerShell访问共享文件夹的用户

  •  2
  • Tangiest  · 技术社区  · 16 年前

    我猜这将需要一个WMI查询,但我最初的在线搜索没有透露查询的详细信息。

    谢谢你,MagicAndi

    2 回复  |  直到 16 年前
        1
  •  4
  •   Tangiest    16 年前

    我想出了以下脚本:

    $computer = "LocalHost"
    $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
        }    
    
        Read-Host
    }
    

    以下文章很有用:

        2
  •  1
  •   shadowz1337    8 年前

    我对其进行了一些修改,以显示主机名而不是IP:

    $computer = "LocalHost"
    $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)
        {
            $ComputerName = [system.net.dns]::resolve($usersession.computername).hostname
            $userDetails = [string]::Format("User {0} from machine {1} on share: {2}", $userSession.UserName, $ComputerName, $userSession.ShareName)
            Write-Host $userDetails
        }    
    
        Read-Host
    }