Link to home
Start Free TrialLog in
Avatar of McGruber
McGruber

asked on

TS Powershell stopped working

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 "")
        {
            $sessionCount++
        }
    }
   
    $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 = "administrator@domain.com"
$emailTo = "it@domain.com"
$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?
Avatar of chrismerritt
chrismerritt

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.
Avatar of McGruber

ASKER

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.
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.
ASKER CERTIFIED SOLUTION
Avatar of McGruber
McGruber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I resolved it myself.