Active Directory | Times Connected to Share”翻译成中文为“活动目录|连接共享的次数”。代码示例:
# 以管理员身份执行此脚本
Import-Module ActiveDirectory
# 获取指定共享文件夹的连接次数
function Get-ShareConnectionCount($sharePath){
$share = Get-WmiObject -Class Win32_Share -Filter "Path='$sharePath'"
if ($share) {
return $share.NumberOfConnections
}
return 0
}
# 获取所有共享文件夹的连接次数
function Get-AllShareConnectionCount{
$shares = Get-WmiObject -Class Win32_Share
foreach ($share in $shares) {
Write-Host "共享路径:" $share.Path ", 连接次数:" $share.NumberOfConnections
}
}
# 获取指定计算机上所有共享文件夹的连接次数
function Get-AllShareConnectionCountOnComputer($computerName){
$shares = Get-WmiObject -Class Win32_Share -ComputerName $computerName
foreach ($share in $shares) {
Write-Host "计算机:" $computerName ", 共享路径:" $share.Path ", 连接次数:" $share.NumberOfConnections
}
}
# 获取指定用户连接的共享文件夹信息
function Get-ShareConnectionInfoByUser($username){
$sessions = Query-WmiObject -Class Win32_ServerConnection | where { $_.UserName -eq $username }
foreach ($session in $sessions) {
Write-Host "用户:" $username ", 计算机:" $session.ComputerName ", 共享路径:" $session.ShareName ", 连接时间:" $session.ConnectionTime
}
}
以上代码示例中,使用了 Powershell 脚本语言,利用了 Active Directory 的 Win32_Share 和 Win32_ServerConnection 类来获取共享文件夹的连接次数和连接信息。