Powershell for WIndows Server 2003 terminal server (send notification to "disconnected" user)

jjoz
jjoz used Ask the Experts™
on
Hi All,

I'm wondering if it is possible to create a powershell script to run scheduled which checked for the current "Disconnected" user from Terminal server ?

so the script should / can check within x amount of days of disconnected session and then NET SEND the user or send the email to that user.

Any idea would be greatly appreciated.

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017

Commented:
An mechanism to get a list of users is to run: "query user"
http://windowsitpro.com/article/articleid/15035/how-can-i-check-if-a-user-is-logged-on-via-terminal-server.html

You can then parse the data to extract the username and their status/idle time
Presumably you would have a map or a known username -> email address.

does the server have an IIS SMTP installed?
Dropping a properly formatted text file into the c:\inetpub\mailroot\pickup will send the message.

Sending email from powershell:
http://www.searchmarked.com/windows/how-to-send-an-email-using-a-windows-powershell-script.php
Commented:
For powershell:

$yourtime=3*24*3600 #3 days in seconds
Get-WMIObject Win32_ServerSession | Where-Object {$_.IdleTime -gt $yourtime}

Resulting objects have properties described at http://msdn.microsoft.com/en-us/library/aa394417(VS.85).aspx and you can use it for own purposes.

Sending message is not so easy... net send is obsolete and doesn't work at all in newer systems. They use msg.exe for sending messages but there's no really reliable method except mail... :(

Author

Commented:
yes that's why it is quite hard to choose which methods to send the notification with :-|

Email is the most appropriate one i think.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
You can use msg.exe...
If you like 3rd party solutions then look at http://www.ctrl-alt-del.com.au/CAD_TSUtils.htm for an utility called TSMSG.

Author

Commented:
Hi Arnold, yes I can install IIS and make it send email to my Exchange Server 2007.

THanks for the suggestion.
Chris DentPowerShell Developer
Top Expert 2010
Commented:

IIS isn't necessary to send a mail...

If you're using PowerShell 2, Send-Email will get the notification out without installation of anything else.

If you're using PowerShell 1, System.Net.SmtpClient can be used (Send-Email just wraps that one up).

Chris

Author

Commented:
ah Yes, thanks for the explanation Chris,

this is Windows Server 2003 so I'm going to be using PS 1.0
Chris DentPowerShell Developer
Top Expert 2010

Commented:

Then this is how a mail might be sent. Obviously the message text will need a bit more work than I've given it here ;)

Just make sure any antivirus you have isn't blocking SMTP (TCP Port 25) or the message send will fail.

Chris
$To = "WhateverUser@domain.com"
$From = "you@domain.com"
$Subject = "Get off my server!"
$Body = "Please log off..."

$SmtpServer = "yourExchangeServer"

# Create MailMessage
$MailMessage = New-object Net.Mail.MailMessage($To, $From, $Subject, $Body)

# Optional: Set the body to HTML
$MailMessage.IsBodyHTML = $True

# Create the SMTP Client
$SmtpClient = New-Object Net.Mail.SmtpClient($SmtpServer)
$SmtpClient.Send($MailMessage)

Open in new window

Chris DentPowerShell Developer
Top Expert 2010
Commented:

Oops.. I put $To and $From the wrong way around.

It should be...

Chris
$To = "WhateverUser@domain.com"
$From = "you@domain.com"
$Subject = "Get off my server!"
$Body = "Please log off..."

$SmtpServer = "yourExchangeServer"

# Create MailMessage
$MailMessage = New-object Net.Mail.MailMessage($From, $To, $Subject, $Body)

# Optional: Set the body to HTML
$MailMessage.IsBodyHTML = $True

# Create the SMTP Client
$SmtpClient = New-Object Net.Mail.SmtpClient($SmtpServer)
$SmtpClient.Send($MailMessage)

Open in new window

Author

Commented:
Wow, thanks for the helps again Chris, I'll try that tomorrow in the office.

Cheers.

Author

Commented:
I've just realized that Powershell 2.0 is already released in different name URL: http://www.microsoft.com/downloads/details.aspx?FamilyId=909bbcf1-bd78-4e03-8c83-69434717e551 

I wonder if this can be used in Windows Server 2003 R2 SP2 x64 environment, Does anyone recommend it or not ?
Commented:
Powershell 2.0 is nice. It allows run commands remotely and some other things.
Look at http://blogs.msdn.com/powershell/archive/2007/11/06/what-s-new-in-ctp-of-powershell-2-0.aspx for list of improvements.
I love 2.0 but I use it everyday on 2008R2. AFAIK it works OK on 2003 x64 too.
Chris DentPowerShell Developer
Top Expert 2010

Commented:

I haven't installed it on many of my servers simply because I do most work from my workstation. However, it's solid and will run perfectly well under Windows 2003.

Whether you install it on the server or a workstation, I recommend it. Lots of tweaks to make life easier for all of us (and we don't have to worry about compatibility between versions).

Chris

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial