net use * /delete /y doesn't resolve error "New-PSDrive : Multiple connections to a server ...."
Asked Answered
D

6

7

This New-PSDrive command

New-PSDrive -Name Z -PSProvider FileSystem -Root \\$j\share -Credential $credentials -ErrorAction Stop

Causes the error

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

I have tried disconnecting all drives first, then creating new drive,

net use * /delete /y
New-PSDrive -Name Z -PSProvider FileSystem -Root \\$j\share -Credential $credentials -ErrorAction Stop

I get the output

You have these remote connections:
                \\TSCLIENT\C
                \\TSCLIENT\D
                \\TSCLIENT\E
                \\TSCLIENT\I
                \\TSCLIENT\J
                \\TSCLIENT\K
                \\TSCLIENT\L
                \\TSCLIENT\R
                \\TSCLIENT\T
Continuing will cancel the connections.

The command completed successfully.

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

I also tried removing the Z drive first

Remove-PSDrive -name Z
New-PSDrive -Name Z -PSProvider FileSystem -Root \\$j\share -Credential $credentials -ErrorAction Stop

And get error

Remove-PSDrive : Cannot find drive. A drive with the name 'Z' does not exist.
...
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

How to fix?

UPDATE

I even rebooted the machine and changed the drive name, but I still get the same error of "New-PSDrive: Multiple connections ......"

UPDATE 2

I have also tried using IP address instead of computer name, but that doesn't work either, http://support.microsoft.com/kb/938120

Dola answered 23/9, 2014 at 13:3 Comment(7)
Is that not a message about the target share rejecting your attempt rather than your local machine refusing to mount Z? What if you disconnect everything and then try? What is stored in the variable $j?Furbelow
This could happen also if you have the target drive open in windows explorer. Mappped drive or no.Constitution
@Furbelow $j is the computer name.Dola
@arco444, reboot is short-term solution. What is long-term solution?Dola
Have you found solution to this problem ? Exactly the same behavior here.Betray
@Betray I haven't found solutionDola
see my answer bellowBetray
B
2

I found workaround to this problem that seem to always work. You need to change the computer name, but since this will also stop working eventually just as with server name and IP, you need the option to specify arbitrary number of new computer names resolving to the same computer.

The obvious choice is hosts file. You can add any number of aliases to the IP to it. Afterwards, use the alias that isn't already blocked.

==== EDIT ===

Here is the handy function:

<#  Use unique hostname for this script by altering hosts table to handle situation with multiple different scripts 
    using this share with different creds which leads to following Windows error: 

    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

    #require -RunAsAdministrator
    #require -module PsHosts

    https://mcmap.net/q/1533564/-net-use-delete-y-doesn-39-t-resolve-error-quot-new-psdrive-multiple-connections-to-a-server-quot 
#>
function Set-UncHostnameAlias($UncPath) {
    $remote_hostname = $UncPath -split '\\' | ? {$_} | select -First 1
    $remote_alias    = (Split-Path -Leaf $MyInvocation.ScriptName) -replace '.ps1$'

    $hostEntry = Get-HostEntry $remote_alias* -ea 0 | ? { $_.Comment -eq $remote_hostname } | select -First 1
    if (!$hostEntry) {
        $remote_alias += (Get-HostEntry $remote_alias*).Count + 1
        Write-Verbose "Adding alias $remote_alias => $remote_hostname"
        $remote_ip = Test-Connection -ComputerName $remote_hostname -Count 1  | % IPV4Address | % IPAddressToString
        Add-HostEntry -Name $remote_alias -Address $remote_ip -Force -Comment $remote_hostname | Out-Null
    } else { 
        $remote_alias =  $hostEntry.Name
        Write-Verbose "Using $remote_hostname alias: $remote_alias"
    }

    $UncPath.Replace("\\$remote_hostname", "\\$remote_alias")
}

Do this on the start of the script:

$PathAlias = Set-UncHostnameAlias $Path

and used aliased path afterwards with the New-PSDrive. This works always, even if some other scripts on the same system use different credentials for the same server.

Betray answered 15/3, 2015 at 9:22 Comment(1)
Good to know it works. It is bulletproof solution, you can even make it easier by providing helper method in fashion add-serveralias server newname; New-PSDrive ... newnameBetray
A
2

I was having the same issue with local scripts and found this to be a simple solution. The Get-CimInstance returns all of the mapped network connections, then just pass that to the net use /delete /y command.

$shareDrives = Get-CimInstance -ClassName Win32_NetworkConnection

if ($shareDrives -ne $null)
{
    foreach ($shareDrive in $shareDrives)
    {
        Write-Host "`nRemoving mapped drive $($shareDrive.LocalName)"
        net use $shareDrive.LocalName /delete /y
    }
}
else
{
    Write-Host "`nNo mapped drives to remove!" 
}
Araminta answered 12/5, 2020 at 14:29 Comment(0)
U
1

Using the FQND worked for me..

How to find out FQDN??

ping -a -n 1

Pinging [This is the FQND!!!!] [192.168.0.1] with 32 bytes of Reply from 192.168.0.1: bytes=32 time=1ms TTL=128

Unbolted answered 21/5, 2015 at 20:38 Comment(0)
P
0

You need to use the FQDN instead of the NetBios name.

Proteus answered 22/10, 2014 at 15:54 Comment(0)
P
0

My script needs to be client computer independent since other members of my team might run it. This works for me. Not sure if the "Write-Host" is needed but it also doesn't get in the way. Also, there is some sort of error that doesn't affect using the drive again if it already exists.

if (Get-PSDrive DLL_NEW_TEMP -ErrorAction SilentlyContinue){Write-Host "DLL_NEW_TEMP Drive exists"}
else{
   New-PSDrive -Name DLL_NEW_TEMP -PSProvider FileSystem -Root \\WTDHSxxxL32\d$\ServerDLLDev\New_DLL_temp_location  -Credential $credential
}

if (Get-PSDrive DLL_WORKING  -ErrorAction SilentlyContinue){Write-Host "DLL_WORKING Drive exists"}
else{
   New-PSDrive -Name DLL_WORKING -PSProvider FileSystem -Root \\WTDHSxxx32\d$\ServerDLLDev -Credential $credential
}
Perversity answered 4/5, 2017 at 16:11 Comment(0)
R
0

The below information I found on here

This error message means that you already have a connection to that UNC path, whether it's defined on your computer or not. Windows only allows you to connect to a particular UNC path with one username, regardless of the number of connections to that UNC. If you use the same username for all connections to a UNC from your computer then you shouldn't run into this error.

My issue and its solution: I was connected to the shared drive and it was open in my session with my credentials and at the same I've tried to run New-PSDrive with other credential. After closing my session with the shared drive the command worked like a pro.

If you have to use a different username to connect, then one workaround is to connect to the UNC using an IP address or other alias so that it looks like it's a different path. This is also the workaround recommended by Microsoft:

Renaissance answered 21/10, 2021 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.