Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 908
Glowie Avatar asked Sep 23 '14 13:09

Glowie


People also ask

How do I force uninstall Net?

You can use the Net Use * /delete command to delete active connections on a local computer. The command deletes all the active connections on local computer. This command can also be used on remote computers.

How do I create a new PSDrive in PowerShell?

Adding New Windows PowerShell Drives (New-PSDrive) To create a new Windows PowerShell drive, you must supply three parameters: A name for the drive (you can use any valid Windows PowerShell name) The PSProvider (use "FileSystem" for file system locations and "Registry" for registry locations)

What is net use Y?

Use net use to connect to and disconnect from a network resource, and to view your current connections to network resources. You cannot disconnect from a shared directory if you use it as your current drive or an active process is using it.


1 Answers

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://stackoverflow.com/a/29059100/82660 
#>
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.

like image 176
majkinetor Avatar answered Sep 21 '22 11:09

majkinetor