Created
January 30, 2019 22:53
-
-
Save nanoDBA/6ac84abae93b92b1036f440e48817309 to your computer and use it in GitHub Desktop.
It's a mess but it worked... I was able to query several servers concurrently to find where I was logged in via RDP and logoff my sessions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Modules PoshRSJob | |
break; #Safety First - These bits of script are meant to match a user name | |
# and LOGOFF the RDP sessions of that user when passed a list of servers | |
# So... potentially very dangerous | |
# source: https://4sysops.com/archives/how-to-force-a-user-logoff-remotely-with-powershell/ | |
$results = Start-RSJob -Throttle 20 <#-ModulesToImport dbatools#> -InputObject $TargetServerstoKILL_RDP_SESSIONS -Verbose -ScriptBlock { $username = 'kill.this.username'; Write-Output "$($_)..."; $sessionId = 7777777; Write-Output "`$sessionId = $($sessionId)"; quser /server:"$($_)" | Where-Object { $_ -match $userName } ; $sessionId = ((quser /server:"$($_)" | Where-Object { $_ -match $userName }) -split ' +')[2] ; Write-Output "`$sessionId = $($sessionId)" ; IF ($sessionId -NE "SESSIONNAME" -AND $sessionId -NE $null -AND $_ -NE $($env:COMPUTERNAME)) { logoff $sessionId /server:"$($_)" } ELSE { Write-Output "No match found for $username" } } | Wait-RSJob -Timeout 72 -ShowProgress -Verbose | Receive-RSJob -Verbose; $results | Out-GridView |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment