Solved

Powershell to Logoff terminal session

Posted on 2013-01-03
4
1,975 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Set OWA language and time zone in Exchange for individuals, all users or per database.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

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