Share via

PowerShell process taking long time to finish after script has completed

Peter Bishop 181 Reputation points
May 18, 2023, 8:52 PM

I have a simple PowerShell script file which executes within a second interactively but when I can it from a SQL Server Integration Services (SSIS) package, runs for 8 seconds. I added some logging to the script and in combination with the SSIS logging I found the following:

  • SSIS process starts at 00:00:00
  • PowerShell script logs start time as 00:00:00
  • PowerShell script logs end time as 00:00:01
  • SSIS process starts at 00:00:08

If I look at Task Manager, I can see the PowerShell process is sitting there for 8 seconds so it's not that the SSIS process isn't finishing but instead that the PowerShell process isn't exiting. The script file ends with "exit 0" so I'm confused as to why the PowerShell process is hanging around.

We recently migrated from Windows Server 2012 R2 (where everything ran without issue) to Windows Server 2019 (where it is laggy). On the 2012 server, the PowerShell version is:

Name Value
PSVersion 5.1.14409.1029
PSEdition Desktop
BuildVersion 10.0.14409.1029
CLRVersion 4.0.30319.42000

whereas on the 2019 server, it is:

Name Value
PSVersion 5.1.17763.3770
PSEdition Desktop
BuildVersion 10.0.17763.3770
CLRVerion 4.0.30319.42000

Any ideas? Thanks.

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,768 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,581 Reputation points
    May 19, 2023, 2:22 AM

    Instead of exiting the process, try it this way:

    param(
        [string]$logfile,
        [string]$sourceFile,
        [string]$destinationFile
    )
    ("PID ${PID} started at " + (Get-Date)) | Out-File -Filepath $logfile -Append
    Try{
        Move-Item -Path $sourceFile -Destination $destinationFile -Force -ErrorAction Stop 2>&1 | Out-File -Filepath $logfile -Append
        ("PID ${PID} ended at " + (Get-Date)) | Out-File -Filepath $logfile -Append
        $LASTERRORCODE = 0
    }
    Catch{
        ("PID ${PID} ERROR at $(Get-Date) :`n$_") | Out-File -FilePath $logfile -Append
        $LASTERRORCODE = 1
    }
    
    
    
    

    I added the try/catch (and changed the "Continue" to "Stop" in the erroraction parameter).

    0 comments No comments

  2. MotoX80 35,226 Reputation points
    May 19, 2023, 12:57 PM

    My favorite Crystal Ball tool is Sysinternal's Process Monitor.

    https://learn.microsoft.com/en-us/sysinternals/downloads/procmon

    Use it to trace the PowerShell and SQL processes.

    For example, I ran this.

    powershell "'xxx'|out-file c:\temp\foo.txt"
    
    

    In the trace I see PS writing to my txt file and then starts terminating threads as it cleans up.

    User's image

    After the final thread exit, I see a process exit followed by registry cleanup. That all occurs in under a second.

    User's image

    Where do you see the 8 second gap? Does it occur before or after the Process Exit? What entries are in the trace during those 8 seconds?

    Is your log file being written to a local drive or a network share?


  3. Peter Bishop 181 Reputation points
    May 23, 2023, 3:38 PM

    I did the interactive tracing and the AWS modules are still referenced but the delay doesn't occur :-/

    Looking on the relevant AWS page it states "AWS.Tools is the new modular variant of AWS Tools for PowerShell" so tried the following:

    • Uninstall-Module AWSPowerShell
    • Install-Module AWS.Tools.Common
    • Install-Module AWS.Tools.S3

    because these are the only modules that we're using as a business - although not in this script, but hey!

    And this solved the problem! So there's either an issue in the AWSPowerShell module or it's so bloated that it's causing an unexpected outcome. By installing the cut-down modules, either the bloat has been removed or whichever part of AWSPowerShell that was causing the problem is no longer installed.

    Thanks all for your assistance.


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.