Solved

Powershell to Logoff terminal session

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

Expert Comment

by:becraig
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
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 28

Accepted Solution

by:
becraig earned 500 total points
Comment Utility
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
Comment Utility
thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Synchronize a new Active Directory domain with an existing Office 365 tenant
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now