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

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
miketartaglia
Posts: 91
Joined: Mon Feb 02, 2015 7:44 am

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

Post by miketartaglia » Fri Mar 08, 2019 7:47 am

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
}

User avatar
mxtrinidad
Site Admin
Posts: 314
Joined: Sun Mar 03, 2013 12:42 pm

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

Post by mxtrinidad » Fri Mar 08, 2019 8:05 am

We need more information.
Please, can you provide the exact error message you are having in your application?

User avatar
jvierra
Posts: 13681
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Fri Mar 08, 2019 8:17 am

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.

User avatar
miketartaglia
Posts: 91
Joined: Mon Feb 02, 2015 7:44 am

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

Post by miketartaglia » Fri Mar 08, 2019 8:41 am

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.

User avatar
mxtrinidad
Site Admin
Posts: 314
Joined: Sun Mar 03, 2013 12:42 pm

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

Post by mxtrinidad » Fri Mar 08, 2019 9:12 am

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.

User avatar
miketartaglia
Posts: 91
Joined: Mon Feb 02, 2015 7:44 am

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

Post by miketartaglia » Fri Mar 08, 2019 9:33 am

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!

Locked