Solved

Powershell to Logoff terminal session

Posted on 2013-01-03
4
2,088 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

627 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