Page 1 of 1

ERROR: New-PSDrive : Multiple connections to a server or shared resource by the same user

Posted: Fri Mar 08, 2019 6:47 am
by mtartaglia
I am copying one file from about 10 different servers with the below foreach code. I receive this error at some point during the copies, but I can't figure out why I'm getting it. I have googled and found this to be issues for others, but could not find any solution that would help. Any help would be greatly appreciated.

ERROR: New-PSDrive : Multiple connections to a server or shared resource by the same user

foreach ($Server in $Servers)
{
$pass = "PA$$W0RD" | ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PsCredential("us\account", $pass)
New-PSDrive -name Z -Root \\$Server\c$ -Credential $cred -PSProvider filesystem
$progressbaroverlay1.step + 10
foreach ($Brand in $Brands)
{
[System.Windows.Forms.Application]::DoEvents()
$sourceFile = "Z:\programdata\$Brand setup\server configuration.xml"
$destFile = "$Server-$Brand.xml"
if (!(Test-Path "Z:\programdata\$Brand setup"))
{
Break
}
Copy-Item $sourceFile -Destination $destFile

}

[System.Windows.Forms.Application]::DoEvents()
Remove-PSDrive Z
}

Re: ERROR: New-PSDrive : Multiple connections to a server or shared resource by the same user

Posted: Fri Mar 08, 2019 7:05 am
by mxtrinidad
We need more information.
Please, can you provide the exact error message you are having in your application?

Re: ERROR: New-PSDrive : Multiple connections to a server or shared resource by the same user

Posted: Fri Mar 08, 2019 7:17 am
by jvierra
You cannot use credentials on two drives to the same server. If there is already a drive mapped then the second drive will attach correctly if you don't add credentials. This is normal Windows behavior and doesn't have anything to do with PowerShell.

A method to overcome the error is to either check if drives are mapped to that server or catch the error and do the mapping without the credentials.

Re: ERROR: New-PSDrive : Multiple connections to a server or shared resource by the same user

Posted: Fri Mar 08, 2019 7:41 am
by mtartaglia
Unless I am misunderstanding you, The error message I see in red inside Powershell studio is in the subject and the original post of this thread.

Re: ERROR: New-PSDrive : Multiple connections to a server or shared resource by the same user

Posted: Fri Mar 08, 2019 8:12 am
by mxtrinidad
Thanks! I wanted to make sure you were not experiencing other errors. The use of keyword 'exit' or 'break' tend to give an Windows Form exception error (which is a .NET and not SAPIEN). I would suggest to avoid using them.

Follow JVierra recommendation. Make sure to check and remove the drive letter before creating a new one.

Re: ERROR: New-PSDrive : Multiple connections to a server or shared resource by the same user

Posted: Fri Mar 08, 2019 8:33 am
by mtartaglia
So I rebooted my machine and the error went away. So I was obviously at one point connected to one of the servers with my regular user name. Thanks for your help. I am wondering if I could code this differently to avoid this or if there is something I can do on my computer to avoid this.

Thanks for your help!