Closed chadbaldwin closed 5 months ago
not a bug per se because it's this way also on .NET but a welcome feature request. I'll look into it into the weekend
edit: thanks @chadbaldwin for the report
@niphlod Just an FYI, might not be worth fixing, but I at least wanted to make some note so y'all are aware of it.
I updated my scripts to utilize this change, however, I found that if there is an exception thrown prior to the .Clear()
being run, then you can't re-use the parameter set. So this might mess some people/scripts up if they are looping and one iteration of that loop throws an exception, it's caught but is then configured to keep going.
In my case, I just need to fix the exception and I won't run into this issue anymore, so I can work around it.
PowerShell 7.3 added a clean
block to functions, which could be used to resolve this by putting $cmd.Parameters.Clear()
in the clean
block. BUT, I'm going to assume that won't work since it kills all backwards compatibility anything lower than Pwsh 7.3.
Obviously another option would be to put the whole thing in a try/finally block.
High level summary of the issue...
If you try to create a SqlParameter, save it to a variable and then use it more than once, you receive an exception.
But it seems if a simple call to
$cmd.Parameters.Clear()
is made inInvoke-DbaAsync
, that seems to resolve the issue.I guess when you add a SqlParameter to a SqlCommand, it gets claimed in some way. So if you try to use it somewhere else, you get an error saying it's already contained by another SqlParameterCollection.
Verified issue does not already exist?
I have searched and found no existing issue
What error did you receive?
Steps to Reproduce
Please confirm that you are running the most recent version of dbatools
Other details or mentions
I'm pretty sure this is the change that needs to be made:
This...
Needs to go around here...(between these two lines) https://github.com/dataplat/dbatools/blob/56d716a32a0007d3963e1414ce8a27ff656b4ac3/private/functions/Invoke-DbaAsync.ps1#L294-L295
I made this change locally and it fixed the issue, but I don't know what it might blow up, so I decided to submit this as an issue rather than a PR. I don't know how to run all the pester tests and stuff.
What PowerShell host was used when producing this error
PowerShell Core (pwsh.exe)
PowerShell Host Version
SQL Server Edition and Build number
.NET Framework Version