I have multiple functions which contain statements like these:
http://pastebin.com/skdH29ia
They work fine almost every time, but occasionally output errors like:
Property 'LogonName' cannot be found on this object; make sure it exists and is settable.
Even more odd is that the error never occurs on the first object in the collection, regardless of how many objects the collection has. If error occurs, it occurs after the first object and on every object after the first object.
I have used similar structure to add and set properties of various different kinds of psobjects inside a ForEach-Object loop, and any one of them can randomly, if rarely fail. I've never been able to consistently replicate the problem, even when using identical data multiple times.
This is still something that I should get fixed because it affects people using my scripts.
Anyone have any idea what could be the cause?