问题
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,
回答1:
I had similar error message. Below is my history and solution. So yesterday I created new PS-Drive
$c = Get-Credential
$Drive = new-PSDrive -Name "MyDrive" -PSProvider FileSystem -Root "\\10.11.22.100\LogShare" -Credential $c -Scope global
after that I successfully being using "MyDrive"
Like
get-childItem -path "MyDrive:\"
Today, after restart the PC, tried again
get-childItem -path "MyDrive:\"
but got
Get-ChildItem : Cannot find drive. A drive with the name 'MyDrive' does not exist.
Tried to create drive again:
$c = Get-Credential
$Drive = new-PSDrive -Name "MyDrive" -PSProvider FileSystem -Root "\\10.11.22.100\LogShare" -Credential $c -Scope global
Now got
new-PSDrive : 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
As a solution, I removed -Credential $c from New-PSDrive command and it worked.
Powershell somehow remembers credentials and does not let to connect to shared drive with same creds
来源:https://stackoverflow.com/questions/30143451/issue-with-mapping-network-drives-using-new-psdrive