LogoffScript

Published on 27 April 2022 at 16:09

Reason

This script searches for a certain name of session and shuts it down.

We use an RDP connection on handscanners to use a new app.
Logging onto the RDP server only boots the app without any other programs/unneeded services.
This has been set in AD in the profile of the scanuser in the Environment tab.
Whenever the user tries to interact before the app has started the session results in a black screen, rendering the user unable to interact with the app.
The easy solution is killing the user session on the terminal server.
Without giving multiple managers local admin rights on the server we configured a local admin and provided the password to them.
Using the following script they can use the local admin account to list the scanuser-sessions to disconnect them.
To avoid killing other users their sessions I used the select-string in the search to list only the scanusers.
A Log file will be created to keep track of who killed what session on which device.
Starting this script will give a popupscreen to enter the local admin password, followed by a popup to ask if they want to log off a user session.
pressing "yes" will give the list where they can logoff the session, "No" stops the script.

Script

#[Function] logoff function
function session{
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Do you want to logoff a usersession from RDP-server?", 0,"Logoff session from RDP-server",4)
If ($intAnswer -eq 6) {
$User = Invoke-Command -ComputerName 'RDP-server' -ScriptBlock { quser } -Credential $Credentials | select-string scan
$UserInfo = foreach ($Session in ($User | select -Skip 1)) {
$Session = $Session.ToString().trim() -replace '\s+', ' ' -replace '>', ''
if ($Session.Split(' ')[3] -eq 'Active') {
[PSCustomObject]@{
UserName = $Session.Split(' ')[0]
SessionName = $Session.Split(' ')[1]
SessionID = $Session.Split(' ')[2]
SessionState = $Session.Split(' ')[3]
IdleTime = $Session.Split(' ')[4]
LogonTime = $Session.Split(' ')[5, 6, 7] -as [string] -as [datetime]
}
} else {
[PSCustomObject]@{
UserName = $Session.Split(' ')[0]
SessionName = $null
SessionID = $Session.Split(' ')[1]
SessionState = 'Disconnected'
IdleTime = $Session.Split(' ')[3]
LogonTime = $Session.Split(' ')[4, 5, 6] -as [string] -as [datetime]
}
}
}
$Session = $UserInfo | out-gridview -Title "Please select the usersession" -OutputMode Single -ErrorAction Stop
$SessionID = $Session.SessionID
$Username = $Session.Username
try {
Invoke-Command -ComputerName 'RDP-server' -ScriptBlock { logoff $Using:SessionID } -Credential $Credentials
}
Catch {
Write-Host "Failed to end usersession $UserName" -ForegroundColor Red
echo "Failed to end usersession $UserName" >> $file
Break
}
if($UserName){
Write-Host "Succesfully logged off $UserName, please log in again" -Foregroundcolor Green
Echo "Succesfully logged off $UserName" >> $file
}
session
} else {
echo "Clicked no, ending script" >> $file
}
}

#start script
try{
#logfile
$date = (Get-Date).tostring("yyyyMMdd")
$timestamp = get-date
#$date = Get-Date
$file = "\\path\to\logfile"
echo "Script started, credentials requested (RDPmanager)" >> $file
hostname >> $file
#Get rdpmanager credentials
Write-Host "Getting RDPmanager credentials... "
$Credentials = Get-Credential -Credential domain\rdpmanager
session
}
catch {
Write-Host "There was a glitch in the system, please report to helpdesk" -ForegroundColor Red
echo "There was a glitch in the system" >> $file
}

 

#end of script
Write-Host "Done." -ForegroundColor Green
Write-Host "###################################" -ForegroundColor Green
echo $timestamp >> $file
echo "------------End of Script------------" >> $file

 

Add comment

Comments

There are no comments yet.