CimSession created inside Start-ThreadJob is correctly received after Job is over but the object is disposed #21447
Labels
Needs-Triage
The issue is new and needs to be triaged by a work group.
Prerequisites
Steps to reproduce
I searched for similar issues, but since I'm new to PowerShell (and programming in general) I might have missed a result with similar names, so sorry if that's the case.
I'm trying to get some info from computers in my AD Domain (15k computers), and I'm using New-CimSession and Get-Ciminstance to do that. Unfortunately I'm restricted to the DCOM protocol, so I can't use PSSession.
I did some speed tests and found out that New-CimSession is fast, and if I put it inside a Start-ThreadJob it's just as fast, with the added benefit of a throttle. Sometimes it seems New-CimSession creates less sessions than it should, which I believe is a throughput issue, so ThrottleLimit seems to fix that.
Anyway, my code.
First, I get a list of computers from AD with Get-ADComputer (not in the script below), $PCList, then I use Test-Connection in a ForEach-Object parallel to see if they are online, $Results, then I use that list to create the sessions. I reduced the results down to 5 to make it easier to test.
That is when I get an error,
If I try to get the CimSession, nothing appears.
Edit for clarity: when I type $test1 in the powershell, I get back the list of the CimSessions, but they give the error above when I try to use them.
If I type Get-CimSession, nothing appears.
I believe this is because Start-ThreadJob runs the code in a separate instance, but then shouldn't it return no objects as they are all disposed?
Meanwhile, if I run the above code with $test2:
I get the correct result (Table with ComputerName, UserAccount, QueryDate).
I tested doing the Get-CimInstance query INSIDE the threadjob, and it works. The code looks like this:
However, since I'm reusing the CimSessions for other Win32 Class queries, this would not be ideal.
For now, I'm sticking with using $test2 method, but I figured it might be niche enough to mention.
Expected behavior
Actual behavior
Error details
Environment data
Visuals
No response
The text was updated successfully, but these errors were encountered: