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

时间:2009-11-25 09:22:00

标签: powershell wmi powershell-v1.0 wmi-query

我需要确定使用PowerShell脚本(v 1.0)访问Windows XP(SP2)计算机上的共享文件夹的用户/会话。这是使用计算机管理|显示的信息系统工具|共享文件夹|会话。谁能指点我如何解决这个问题?

我猜它需要WMI查询,但我在网上的初始搜索没有透露查询详细信息。

谢谢,MagicAndi

2 个答案:

答案 0 :(得分:3)

我想出了以下脚本:

$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
}

以下文章很有用:

与往常一样,如果你在PowerShell中找不到办法,可以看看有人在C#中做过类似的事情。

答案 1 :(得分:0)

我已对其进行一些修改以显示主机名而不是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
}