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

Ask your 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.
This topic is 5 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

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

Post 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
}
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

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

Post by mxtrinidad »

We need more information.
Please, can you provide the exact error message you are having in your application?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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.
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

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

Post 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.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

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

Post 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.
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

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

Post 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!
This topic is 5 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked