Solved

Powershell to Logoff terminal session

Posted on 2013-01-03
4
2,046 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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

729 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