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. 流失註銷舊會話註銷