INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Script to Shutdown Terminal Server if no Users Logged in

Script to Shutdown Terminal Server if no Users Logged in

(OP)
Hi,
I've tried to search for a script to count any users logged in via RDP and if the count is zero, to shut down the server.

This is for a W2KR2 TS.

Anyone know how to do this?

Thanks,
Rodney

RE: Script to Shutdown Terminal Server if no Users Logged in

Just want to clarify since you are asking in the PowerShell forum, you have Windows 2000?

I think you will be limited to using WMI calls and nothing native to PowerShell.

I would suggest you download ScriptoMatic V2 from Microsoft and see what classes you have available to you to query. When you identify the class I would be happy to help write the code to achieve your task.

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

Work SMARTER not HARDER.

RE: Script to Shutdown Terminal Server if no Users Logged in

(OP)
Sorry Windows 2008 R2

RE: Script to Shutdown Terminal Server if no Users Logged in

OK, so you can use this:

CODE --> PowerShell

# Parse 'query session' and store in $sessions: 
$sessions = query session | ?{ $_ -notmatch '^ SESSIONNAME' } | %{
    $item = "" | Select "Active", "SessionName", "Username", "Id", "State", "Type", "Device"
    $item.Active = $_.Substring(0,1) -match '>'
    $item.SessionName = $_.Substring(1,18).Trim()
    $item.Username = $_.Substring(19,20).Trim()
    $item.Id = $_.Substring(39,9).Trim()
    $item.State = $_.Substring(48,8).Trim()
    $item.Type = $_.Substring(56,12).Trim()
    $item.Device = $_.Substring(68).Trim()
    $item
} 
           
# How many active users: 
$SessionCount = @($sessions | ?{ $_.State -eq 'Active' }).Count

If ($SessionCount -eq 0){
    Stop-Computer
} 

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

Work SMARTER not HARDER.

RE: Script to Shutdown Terminal Server if no Users Logged in

(OP)
Hi Mark,

Thank you so much. I will give that a try later today. Much appreciated!

R

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close