Created
March 16, 2014 15:57
-
-
Save daveselias/9585309 to your computer and use it in GitHub Desktop.
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
Function Kill-Sessions{ | |
Param( | |
[Array]$Computer | |
) | |
$AllSessions=@() | |
[int]$Number = 0 | |
ForEach($ComputerName in $Computer){ | |
$SystemSessions = (Get-LoggedOnUser -ComputerName $ComputerName -ErrorAction SilentlyContinue) | |
ForEach($Session in $SystemSessions){ | |
$Number=($Number+1) | |
$Session | Add-Member -MemberType NoteProperty -Name "Number" -Value $Number | |
$AllSessions += $Session | |
} | |
} | |
$AllSessions = $AllSessions | Where-Object {($_.IdleTime -like "*:*") -and ($_.IdleTime -gt "00:59")} | |
$AllSessions | Select Number, `|, ComputerName, UserName, State, IdleTime, SessionName | ft -AutoSize | |
$Terminate=@() | |
$Terminate = Read-Host "Would you like to end one of the above sessions?" | |
IF($Terminate -like "Y*"){ | |
$End = Read-Host "Enter the Number of the session you would like to end" | |
$KillSession = ($Allsessions | ? {$_.number -eq $End}) | |
$Serv = ($Killsession).ServerName.ToString() | |
$ID = ($KillSession).SessionID.tostring() | |
Invoke-Expression "& `"LOGOFF`" /SERVER:$Serv $ID /V" | |
} | |
} | |
$AllServers = (Get-ADComputer -filter {name -like "*Servername*"}).name | |
Kill-Sessions -Computer $AllServers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment