Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to Shutdown Terminal Server if no Users Logged in 1

Status
Not open for further replies.

planetbluau

Programmer
May 25, 2010
54
0
0
AU
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
 
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
Work SMARTER not HARDER.
 
OK, so you can use this:

Code:
# 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
Work SMARTER not HARDER.
 
Hi Mark,

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

R
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top