Solved

Powershell to Logoff terminal session

Posted on 2013-01-03
4
1,934 Views
Last Modified: 2013-01-09
I need a powershell script that would log off a specific user from the terminal server. Can someone please help...
0
Comment
Question by:pauledwardian
  • 3
4 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 38740737
Here is a quick dirty method

$sessions = qwinsta /server servername

foreach ($session in $Sessions)
{
#do a split for the values returned which are fixed and assign a variable to it
I can code that if you want me to
Then an if statement if they match e.g.

if ($newvariable -like "*username*")
{
#run rwinsta and disconnect the session by session id which you get from the previous

}

There are many other ways but this is a quick and dirty idea, if you want an actual complete script let me know.
0
 
LVL 29

Expert Comment

by:becraig
ID: 38740776
There is a powershell module you can get http://archive.msdn.microsoft.com/PSTerminalServices
but what I have above will work as is with no additional downloads or installs.
0
 
LVL 29

Accepted Solution

by:
becraig earned 500 total points
ID: 38741008
try this :

param ([string] $username, [string] $server );
if (($username -eq "") -or ($server -eq ""))
{write-host "
Incorrect Syntax `n
`t Usage: script.ps1 -user username -server computername
" -fore Red
}
else 
{
$sessions = qwinsta /server $server
foreach ($session in $Sessions)
{
$session1 = ($session -replace [regex]::Escape("rdp-tcp#0"), '')
$sessdata = $session1 -Split "\s+"
$user = $sessdata[1] 
$sessid = $sessdata[2] 
if ($user -eq "$username") 
{
write-host "User $user is logged on with session ID $sessid" -fore green
write-host "rwinsta /server bjbidodepl01 $sessid " -fore red}
}
}

Open in new window


Try this it should work
0
 

Author Closing Comment

by:pauledwardian
ID: 38761263
thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question