PowerShell Log off Script

HI Guys,

We have 3 RDS or terminal servers, and i am trying to write a script to logg an individual user off when i need to so far this is what i have writ:

$servers="server-rds01","server-rds02","server-rds03"

$username="Read-Host "Please Enter Username "

$servers | foreach {qwinsta $username /server:$_}

Open in new window


when i run the script the window closes automatically and also i dont know what to write in that code to logg that user off. Please help!
chglAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
$servers="server-rds01","server-rds02","server-rds03"

$username=Read-Host "Please Enter Username "

foreach ($server in $servers )
{
  logoff $username /server:$server
}
0
chglAuthor Commented:
thats doesn't work. The logoff function only accepts these parameters, it does accept username:

PS C:\Documents and Settings\k.uddin> logoff /?
Terminates a session.

LOGOFF [sessionname | sessionid] [/SERVER:servername] [/V]

  sessionname         The name of the session.
  sessionid           The ID of the session.
  /SERVER:servername  Specifies the Terminal server containing the user
                      session to log off (default is current).
  /V                  Displays information about the actions performed.
0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
what is the error in this case?  
Is sessionname not matches with username ?
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

chglAuthor Commented:
no
0
d_nedelchevCommented:
Check out the following articles:

« removed - Qlemo, Topic Advisor »

Terminal Services PowerShell Module

The PSTerminalServices module should do the job. The other option is to try using the query.exe or quser.exe, which are native in Windows Server 2003 Terminal Server, but I'm not sure about 2008.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I agree to the suggestion to use PSTerminalService. qwinsta (or query aso, which are just links to the same binary) requires parsing of the result to get the ID, which is cumbersome and unreliable. However, if you insist:
$servers="server-rds01","server-rds02","server-rds03"
$username="Read-Host "Please Enter Username "

foreach ($server in $servers) {
  qwinsta /server:$server $username | 
    select -skip 1 | % { logoff /server:$server ($_ -split ' +')[2] }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.