Powershell variable "random" corruption?

First I admit this may be a long shot, but was hoping that someone could provide either thoughts on how find the root cause or a “yeah I’ve seen that before!” kind of insight.

I have a script that on the surface is fairly simple, and I’d say 99.9% of the time it runs without a hitch.

General outline of the script is that it is run as a scheduled task, once daily.

  1. At startup it queries our SQL server for a current list of active jobs. into: $CurrentJobList
  2. It then imports the CSV file output from the last run of active jobs into: $PrevJobList
  3. I then use the compare-object function to compare one column "name", capturing the result as: $MisMatches
  4. If $MisMatches is true I will, pipe $CurrentJobList to Export-CSV into two files (one for upload to a service, the second as the reference list that will be used on next run).
This works like a champ almost all the time, but sometimes, which seems complete random at this point, maybe twice in over a year?, it doesn't.

When it doesn’t work correctly both 'exported CSV files seem to include the results of the $MisMatches variable which causes problems specifically because the $MisMatches variable includes the “SideIndictator” column, which messes up the file for the external service.

I’ve checked my code and cannot find any operation that would “write” to the variable. I’m assuming that while the script is running some event on the host interferes with the running process?

  • Host information
    • Server 2012 r2
    • PS host version: 5.1.14409.1018
    • Sophos Endpoint AV
    • also serves as PRTG network monitoring server
Below are the lines in the code, with line # that reference the variable.
151. $CurrentJobList = get-JobList -SqlConnection $conn
153. $script:Result += "`r`nCurrent Job List:`r`n" + $($CurrentJobList.Name -join "`r`n")
159. $MisMatches = Compare-Object $PrevJobList $CurrentJobList -Property name -PassThru
180. $CurrentJobList | Export-Csv $($ExportPath + 'Certify-DimensionFile.csv') -NoTypeInformation -Force
191. $CurrentJobList | Export-Csv $($ExportPath + 'PSjobList.csv')
Thanks for any insight, you might be able to provide!

Ok, so after digging into this some more, I “think” this is related to the “PassThru” parameter.

Per the Microsoft documentation for compare object:

If the objects are different, Compare-Object wraps the differing objects in a PSCustomObjectwrapper with a SideIndicator property to reference the differences. When you use the PassThru parameter, Compare-Object omits the PSCustomObject wrapper around the compared objects and returns the differing objects, unchanged.

In my case and testing on my computer it doesn’t appear that the PassThru is functioning as advertised.

PS Scripts:\> $CurrentJobList = get-JobList -SqlConnection $conn
PS Scripts:\> $CurrentJobList[0]

Name      Code
----      ----
19xx-Job1 19xx


PS Scripts:\> $MisMatches = Compare-Object $jobList18Jun $CurrentJobList -Property name -PassThru
PS Scripts:\> $CurrentJobList[0]

SideIndicator  Name       Code
-------------  ----       ----
=>             19xx-Job1  19xx