I am running a powershell based multithreaded application in which each thread (.net task) needs to copy a bunch of files from one machine to another with a different credential. This is the script which runs in each .net task
New-PSDrive -Name tid -PSProvider FileSystem -Root \\sname\c$\Users\<Uname>\Appdata\Local\temp\<myapp>\tid -Credential $cred
Copy-Item -Source c:\test\* tid:\
Remove-PSDrive -Name tid
At any point there could be a bunch of tasks executing this script. Most of the time it works fine, the files are copied. Once in a while I see this error in mapping the network drive:
Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again
For testing purposes all my tasks were given the same credential. Can't explain why this error doesn't occur everytime if it is an issue with multiple connections as the error string says.
Ran 20 parallel scripts 15 times (a total of 300 times) and saw this error 6 times. Can someone explain the reason for this and how should this be addressed,