Last active
November 17, 2020 15:23
-
-
Save lansalot/0f3feb3f0d8ad325c251b1af8d75f374 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 Get-TSSessions { | |
$qwinsta = (query user) -replace "IDLE TIME","IDLETIME" -replace "LOGON TIME","LOGONTIME" | |
$csv = $qwinsta | ForEach-Object { | |
$_.Trim() -replace "\s+","," | |
} | ConvertFrom-Csv | |
ForEach ($obj in $csv) { | |
If ($obj.id -eq 'Disc') { | |
$obj.logonTime = $obj.IdleTime | |
$obj.IdleTime = $obj.State | |
$obj.State = $obj.id | |
$obj.id = $obj.sessionname | |
$obj.sessionname = $null | |
} | |
Switch ($obj.IdleTime) { | |
{$_ -eq '.'} { $obj.IdleTime = $null} | |
{$_ -match ":"} { $obj.IdleTime = New-TimeSpan -hours $obj.idletime.split(":")[0] -Minutes $obj.idletime.split(":")[1] } | |
default { $obj.IdleTime = New-TimeSpan -Minutes $obj.idletime } | |
} | |
} | |
$csv | |
} | |
Get-TSSessions|Where {$_.idletime -gt (new-timespan -hours 2) -and $_.state -eq "Disc"}| % { | |
Write-Host "Logging off $($_.username) who was idle for $($_.idletime) since $($_.logontime) at $(get-date)" | Out-File c:\de\disconnectedlogoffs.txt -Append | |
logoff $($_.id) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment