[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 545
  • Last Modified:

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?
0
McGruber
Asked:
McGruber
  • 3
1 Solution
 
chrismerrittCommented:
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.
0
 
McGruberAuthor Commented:
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.
0
 
Carlo-GiulianiCommented:
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.
0
 
McGruberAuthor Commented:
Reboot of DC solved the issue.  Must have been hung up on name resolution of server name change.
0
 
McGruberAuthor Commented:
I resolved it myself.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now