1
A
回答
0
我已经得到了答案,我正在写这个答案来帮助有需要的人,因为我必须自己弄清楚这一点。我使用在线资源创建了一个脚本,以查找AD环境中所有Windows Server上的断开连接的RDP会话。我在每台Windows服务器上运行一个查询并创建一个CSV格式的列表,然后使用该列表从这些服务器注销我的ID,因此我没有任何断开连接的会话。
我这样做是为了确保我的AD帐户在其更改密码时由于某些断开连接的RDP会话没有被锁定。
您可以根据需要自由修改此脚本。
脚本代码如下:
param (
#get current logged on username
[string]$UserName = $env:USERNAME
)
# Import the Active Directory module for the Get-ADComputer CmdLet
Import-Module ActiveDirectory
# Query Active Directory for enabled windows servers computer accounts and sort by name
$Servers = Get-ADComputer -Filter {(OperatingSystem -like "*windows*server*") -and (Enabled -eq "True")} | Sort Name
# Initiating variables
$SessionList = $NULL
$queryResults = $NULL
$SError = $null
$SDown = $null
$z = 0
# Get total number of servers
$count = $Servers.count
# Start looping through each server at a time
ForEach ($Server in $Servers) {
# initiate counter for showing progress
$z = $z + 1
$ServerName = $Server.Name
# Start writing progress
Write-Progress -Activity "Processing Server: $z out of $count servers." -Status " Progress" -PercentComplete ($z/$Servers.count*100)
# check if server is pingable before running the query on the server
if (Test-Connection $Server.Name -Count 1 -Quiet) {
Write-Host "`n`n$ServerName is online!" -BackgroundColor Green -ForegroundColor Black
Write-Host ("`nQuerying Server: `"$ServerName`" for disconnected sessions under UserName: `"" + $UserName.ToUpper() + "`"...") -BackgroundColor Gray -ForegroundColor Black
# Store results in array
[array]$queryResults += (
# Query server for specific username
query user $UserName /server:$ServerName |
foreach {
# Look for lines with Disc string to filter out active sessions
if ($_ -match "Disc") {
# format the output in CSV by replacing more than 2 spaces with a comman
write-output ("`n$ServerName," + (($_.trim() -replace ' {2,}', ',')))
}
}
)
}
# If server is not pingable show error message
else {
# Make list of server that are down.
[array]$SDown += ($ServerName)
Write-Host "`nError: Unable to connect to $ServerName!" -BackgroundColor red -ForegroundColor white
Write-Host "Either the $ServerName is down or check for firewall settings on server $ServerName!" -BackgroundColor Yellow -ForegroundColor black
}
}
# If there are some non pingable server then display the list
if ($SDown -ne $null -and $SDown) {
Write-Host "`nScript was unable to connect to the following server:" -ForegroundColor White -BackgroundColor Red
$SDown
}
# Check if any disconnected session are stored in the array
if ($queryResults -ne $null -and $queryResults) {
# Convert the CSV fromat to table format with headers
$QueryResultsCSV = $queryResults | ConvertFrom-Csv -Delimiter "," -Header "ServerName","UserName","SessionID","CurrentState","IdealTime","LogonTime"
# Show the results on console
$QueryResultsCSV |ft -AutoSize
# Go through each Disconnected session stored in the array
$QueryResultsCSV | foreach {
# Grabb session ID and ServerName
$Sessionl = $_.SessionID
$Serverl = $_.ServerName
# Show message on the console
Write-Host "`nLogging off"$_.username"from $serverl..." -ForegroundColor black -BackgroundColor Gray
sleep 2
# Logout user using session ID
logoff $Sessionl /server:$Serverl /v
}
}
else {
# if array is empty display message that no session were found
Write-Host `n`n`n`n("*" * $LineSize)
Write-Host "You are all good! No ghost sessions found!" -BackgroundColor Green -ForegroundColor Black
Write-Host ("*" * $LineSize)
}
# Pause at the end so you can capture the output
$null = Read-Host "`n`nScript execution finished, press enter to exit!"
截图:
相关问题
- 1. 在SSH会话注销时NodeJS进程断开连接
- 2. 断开连接时自动注销
- 3. 在RDP会话中查找用户断开连接时间Windows服务器2012
- 4. 注销Facebook连接会话
- 5. Facebook的连接 - 注销+销毁会话
- 6. 保持断开连接后RDP会话活着
- 7. 断开连接时销毁socket.io连接
- 8. 销毁会话,同时注销
- 9. 注销和网络输出上的Socket-io断开连接
- 10. 的Oracle 9i的会话断开连接
- 11. 断开连接后RDP会话的GUI是否可以保持活动状态
- 12. 当连接附件时,Android tcpip ADB会话断开连接
- 13. 检索会话值并连接到不同的数据库在注销功能
- 14. 会话注销时出现ExecJS RuntimeError
- 15. Powershell脚本检查断开连接的会话已超过12小时
- 16. PHP会话超时/注销
- 17. 注销时保存会话
- 18. 麻烦注销Facebook连接网站和销毁会话
- 19. 注销时未销毁会话
- 20. 将客户端断开连接并重新连接到接收者会话
- 21. Powershell RDP会话和CLI脚本
- 22. 注销/销毁会话时出现的问题
- 23. 自动AD注销后的SignalR断开连接
- 24. asp.net会话注销
- 25. 注销citrix会话
- 26. Facebook注销会话
- 27. 断开Asterisk Manager的连接时出错
- 28. 流失注销旧会话注销