gpt4 book ai didi

Powershell找出断开的RDP session 并同时注销

转载 作者:行者123 更新时间:2023-12-03 00:35:11 26 4
gpt4 key购买 nike

是否有脚本可以从任何服务器注销我断开连接的 RDP session ?这导致了很多痛苦和持续的广告帐户锁定。

任何帮助都是极好的。

最佳答案

我得到了答案,我正在写这个答案来帮助有需要的人,因为我必须自己解决这个问题。我使用在线资源创建了一个脚本,以找出 AD 环境中所有 Windows Server 上断开连接的 RDP session 。我在每个 Windows Server 上运行一个查询并创建一个 CSV 格式的列表,然后我使用该列表从这些服务器上注销我的 ID,所以我没有任何断开连接的 session 。

我这样做是为了确保我的 AD 帐户在更改密码时不会因为某些断开的 RDP session 而被锁定。

您可以根据需要自由修改此脚本。

脚本代码如下:

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!"

截图:
  • 当脚本在所有服务器上运行时,显示在线和离线服务器:
    Inital script run
  • 脚本无法连接的服务器列表:
    List of server script was unable to connect
  • 该脚本列出了发现断开的 RDP session 的服务器。
    Disconnected RDP Sessions
  • 当脚本开始记录您断开连接的 session 并在最后暂停时。
    enter image description here
  • 关于Powershell找出断开的RDP session 并同时注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43352905/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com