TS Powershell stopped working

Posted on 2012-08-20
Last Modified: 2012-08-29
Running this powershell script via Task Scheduler:

mport-module psterminalservices

$body = ""
$servers = get-tsservers

foreach ($server in $servers)
    $sessionCount = 0
    $sessions = get-tssession -computername $server.ServerName
    foreach($session in $sessions)
        if($session.UserName -ne "")
    $a = @{Expression = {$_.SessionId}; Label = "Session  "; Align="Left"}, `
         @{Expression = {$_.ConnectionState}; Label = "State        "; Align="Left"}, `
         @{Expression = {$_.UserName}; Label = "User          "}, `
         @{Expression = {$_.DomainName}; Label = "Domain  "}, `
         @{Expression = {$_.ClientIPAddress}; Label = "IP        "}, `
         @{Expression = {$_.ClientName}; Label = "Machine      "}, `
         @{Expression = {($_.CurrentTime - $_.ConnectTime).Hours.ToString() + "h " + ($_.CurrentTime - $_.ConnectTime).Minutes.ToString() + "m "}; Label = "Connected For  "; Align="Center"}, `
         @{Expression = {$_.IdleTime.Hours.ToString() + "h " + $_.IdleTime.Minutes.ToString() + "m"}; Label = "Idle For  "; Align="Center"}
    $body += "===================================================================================================" + "`n"
    $body += ("Windows Terminal Server: " + $server.ServerName.tostring() + "    (Total Sessions = " + $sessionCount.tostring() + ")`n")
    $body += "==================================================================================================="
    $body += $sessions | format-table $a -auto | out-string -width 1000
    $body += "`n`n`n"

$emailFrom = ""
$emailTo = ""
$subject = "Terminal Server Users"
$smtpServer = "mail.domain.local"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$smtp.Send($emailFrom, $emailTo, $subject, $body)

How can I debug to see where it is failing?  Renamed a server over the weekend - suspect it must be that.  What needs to run on the ts servers for the script to pick it up?
Question by:McGruber
    LVL 9

    Expert Comment

    wow pretty messy, not sure why you are using so many @expressions!

    Besides that, what error are you getting with it at the moment? normally the error you get in PowerShell will help you identify where it's falling over.

    Author Comment

    I get a 0 x1 error in task manager.  The powershell script runs every two hours and is supposed to email my group with the tsusers on each terminal server.  I do not see an error - that is why I inquired about a debugging tool or mechanism to find the error.
    LVL 12

    Expert Comment

    I suppose your scheduled task runs the powershell command with a parameter of the file containing the script.    The problem with this is that there actually is no way to capture the output (e.g. error messages) you would get from an interactive session in the scenario.  The problem is well documented here.

    If you read that article, at the end the author recommends you set up your scheduled task to call a good old command (.CMD) file, and in that command file use redirection (>filename.log) to send the powershell output.

    To summarize.
     - your scheduled task should execute CMD with parameters "-C  c:\scripts\call-ps-script.cmd"
     - call-ps-script.cmd should containt a command something like
       "powershell -c c:\scripts\ps-script.ps1 >ps-script.log"

    Or, just open an interactive powershell session and run the script interactively to see any error messages.

    Accepted Solution

    Reboot of DC solved the issue.  Must have been hung up on name resolution of server name change.

    Author Closing Comment

    I resolved it myself.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 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

    23 Experts available now in Live!

    Get 1:1 Help Now