Skip to content

Instantly share code, notes, and snippets.

@systemcenterblog
Created September 17, 2019 10:35
Show Gist options
  • Save systemcenterblog/5516feac01df187e0da48880aefd3f2d to your computer and use it in GitHub Desktop.
Save systemcenterblog/5516feac01df187e0da48880aefd3f2d to your computer and use it in GitHub Desktop.
#Disconnect sessions from multiple servers using powershell script#
# Found on https://mydailytechnicalskills.blogspot.com/2017/06/disconnect-sessions-from-multiple.html #
#Script no warranty#
function Get-Sessions
{
$queryResults = query session
$starters = New-Object psobject -Property @{"SessionName" = 0; "UserName" = 0; "ID" = 0; "State" = 0; "Type" = 0; "Device" = 0;}
foreach ($result in $queryResults)
{
try
{
if($result.trim().substring(0, $result.trim().indexof(" ")) -eq "SESSIONNAME")
{
$starters.UserName = $result.indexof("USERNAME");
$starters.ID = $result.indexof("ID");
$starters.State = $result.indexof("STATE");
$starters.Type = $result.indexof("TYPE");
$starters.Device = $result.indexof("DEVICE");
continue;
}
New-Object psobject -Property @{
"SessionName" = $result.trim().substring(0, $result.trim().indexof(" ")).trim(">");
"Username" = $result.Substring($starters.Username, $result.IndexOf(" ", $starters.Username) - $starters.Username);
"ID" = $result.Substring($result.IndexOf(" ", $starters.Username), $starters.ID - $result.IndexOf(" ", $starters.Username) + 2).trim();
"State" = $result.Substring($starters.State, $result.IndexOf(" ", $starters.State)-$starters.State).trim();
"Type" = $result.Substring($starters.Type, $starters.Device - $starters.Type).trim();
"Device" = $result.Substring($starters.Device).trim()
}
}
catch
{
$e = $_;
Write-Log "ERROR: " + $e.PSMessageDetails
}
}
}
[string]$IncludeStates = '^(Disc)$'
$DisconnectedSessions = Get-Sessions | ? {$_.State -match $IncludeStates -and $_.UserName -ne ""} | Select ID, UserName
foreach ($session in $DisconnectedSessions)
{
logoff $session.ID
Write-host -Message $session.Username
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment