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
}
ERROR: New-PSDrive : Multiple connections to a server or shared resource by the same user
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.
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.
- mtartaglia
- Posts: 101
- Last visit: Mon Dec 19, 2022 11:45 am
- 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
We need more information.
Please, can you provide the exact error message you are having in your application?
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
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.
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.
- 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
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.
- 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
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.
Follow JVierra recommendation. Make sure to check and remove the drive letter before creating a new one.
- 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
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!
Thanks for your help!