Scheduled powershell script uses a old version of script even after updating the script file

udhayan d 181 Reputation points
2022-06-17T14:33:26.467+00:00

Hi Experts,

I am kind of facing a weird issue. I prepared a simple powershell script to capture all active sessions in postgres and if total count breaches a threshold value send an email notification.
Constructed the script using powershell ISE. Just for simulation set the threshold to a lower value . Everything works perfectly and changed the threshold value to 200 and saved the file.

if($totalconn -gt 40){
send-mailmessage -To $Toaddr -From $fromaddr -Subject "Warning - $message - PLEASE CHECK" -Body $body -SmtpServer $smtpserver}

But whenever the script is triggered as per the schedule in Task scheduler, the if condition evaluates to the old value 40 which is weird. The old version of the script is cached somewhere. I closed all my powershell and powershell ISE windows . I renamed the file. Nothing works , still the old version of the script is getting executed.

I am not the first one to face this issue. I see a lot of similar post in stackoverflow, but i cannot find a clear solution to this problem.
I think a host reboot should fix the issue, but it is not possible immediately in my case.

Thanks in advance

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,627 questions
0 comments No comments
{count} vote

Accepted answer
  1. MotoX80 35,511 Reputation points
    2022-06-17T18:45:53.743+00:00

    My problem is the version of script that is getting executed is not what i saved.

    Well, if you added a Start-Transcript to your script, and you now see a transcript file being created, then the script that you are executing is the script that you updated. Because if you were executing some cached version, it would not include the Start-Transcript and the file would not get created.

    Add output statements (which will appear in the transcript file) to display what you are looking at. You are probably doing a string comparison when you really want to do a numeric comparison.

    $TestValue = 200  
    "Totalconn = {0} and I will test for {1}" -f $totalconn, $TestValue    
    "Totalconn is of type {0}" -f $totalconn.gettype().name    
    "Test is {0} " -f   ($totalconn -gt $TestValue)  
    "Numeric test is {0} " -f   ([int]$totalconn -gt $TestValue)  
    if([int]$totalconn -gt $TestValue){                # insure testing integers, not strings   
    	send-mailmessage -To $Toaddr -From $fromaddr -Subject "Warning - $message - PLEASE CHECK" -Body $body -SmtpServer $smtpserver}  
    	  
    	  
    	
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. MotoX80 35,511 Reputation points
    2022-06-17T14:56:20.183+00:00

    In the task settings you would need to configure it to execute something like "Powershell.exe -file C:\Scripts\SomeScript.ps1".

    In SomeScript.ps1 add a Start-Transcript before executing any other application calls to capture it's activity. Run the task and review the transcript log file.

    https://lazyadmin.nl/powershell/start-transcript/

    The mere existence of the transcript file will indicate that you are executing the script that you think you are updating. Display the data that you are looking at.

    "Totalconn = {0}" -f $totalconn   
    "Test for 40 is {0}" -f ($totalconn -gt 40)  
    
     
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.