使用远程桌面列出用户连接到的所有服务器

有没有办法列出所有的服务器,一个给定的用户login到整个活动目录

就像是…:

QueryRdpConnections -user BobAdmin 

结果…:

 Server ---------------- Web001 Web002 Web003 SQL004 SQL007 

你可以在powershell中使用qwinsta:

 QUERY SESSION [sessionname | username | sessionid] [/SERVER:servername] [/MODE] [/FLOW] [/CONNECT] [/COUNTER] [/VM] sessionname Identifies the session named sessionname. username Identifies the session with user username. sessionid Identifies the session with ID sessionid. /SERVER:servername The server to be queried (default is current). /MODE Display current line settings. /FLOW Display current flow control settings. /CONNECT Display current connect settings. /COUNTER Display current Remote Desktop Services counters information. /VM Display information about sessions within virtual machines. 

快速search让你在technet上的这篇文章。

我正在使用脚本的以下部分查询所有活动的服务器,并查找远程login的用户:

 function QueryRdpConnections { $ErrorActionPreference= 'silentlycontinue' # Import the Active Directory module for the Get-ADComputer CmdLet Import-Module ActiveDirectory #Query Active Directory for computers running a Server operating system $Servers = Get-ADComputer -Filter {OperatingSystem -like "*server*" -and Enabled -eq 'true'} ForEach ($Server in $Servers) { $ServerName = $Server.Name # Run the qwinsta.exe and parse the output $queryResults = (qwinsta /SERVER:$ServerName | foreach { (($_.trim() -replace "\s+",","))} | ConvertFrom-Csv) # Pull the session information from each instance ForEach ($queryResult in $queryResults) { $RDPUser = $queryResult.USERNAME $sessionType = $queryResult.SESSIONNAME # We only want to display where a "person" is logged in. Otherwise unused sessions show up as USERNAME as a number If (($RDPUser -match "[az]") -and ($RDPUser -ne $NULL)) { Write-Host $ServerName logged in by $RDPUser on $sessionType } } } } 

我从powershell_ise运行它,只需在第22行编辑$ RDPuser -match“%%%”到$ RDPuser -match“user1 / 2/3 /”以便为特定用户运行。 如果按原样运行,则会显示所有由rdplogin的用户。