I have a PS script which checks a collection of servers to determine if my admin account is logged in - I use Team Viewer RDP sessions for a lot of my work. When it finds a session, it logs my admin account out of that server. I use this at the end of the day so that I don’t leave my admin account logged in somewhere I overlooked.
If I open a PS window and launch the script, it appears to hit all the servers in my list but doesn’t do anything.
If I open PS “as administrator” and run the script, it logs me out of the sessions it finds. So far, so good.
Here’s my code - note that Write-Color is a function I did not include here.
$Username = "MyAdminAccountID"
$serverList = get-content c:\utils\ServersKLst.csv
$StartTime = $(get-date)
$LogCount = 0
foreach ($Server in $serverList) {
$sessionID = ((cmd /c quser /server:$server "2>NUL"| ? { $_ -match $username }) -split ' +')[2]
Write-Color -text "$sessionID"," $Server" -color yellow,yellow
If ($sessionID) {
Write-color -text " $($Username)"," logged ","OFF ","$($Server)"," session ID: ","$($sessionID)" -color blue,green,red,yellow,green,yellow
Logoff $sessionID /server:$Server
$LogCount++
}
}
$elapsedTime = $(get-date) - $StartTime
$totalTime = "{0:HH:mm:ss}" -f ([datetime]$elapsedTime.Ticks)
if ($LogCount -ne 1) {
$Plural = "s"
}
Write-Color -text "`n Logoff completed. Total time:"," $totalTime"," Logged off of ","$LogCount"," Server$Plural`n`n" -color cyan,yellow,green,yellow,green
Pause
So, to use this I have to:
- right-click Powershell
- select Run As Admin
- Enter my Admin creds
- Change directory to C:\Utils
- Run .\logmeoff.ps1
What I’d like to do is have an icon on my desktop (or preferably task bar) which I can run at the end of the day as I’m packing up and have this script close any sessions I haven’t already closed. If I have to provide my admin creds when I launch the desktop icon that’s fine. I just don’t know how to do this.
Thanks for suggestions!
—K