net use * / delete / y不解决错误" New-PSDrive:与服务器的多个连接...."

时间:2014-09-23 13:03:54

标签: powershell remoting drive

这个新的PSDrive命令

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

导致错误

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

我尝试先断开所有驱动器,然后再创建新驱动器,

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

我得到了输出

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

我还尝试先删除Z盘

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

并获得错误

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

如何解决?

更新

我甚至重新启动了机器并更改了驱动器名称,但我仍然遇到“New-PSDrive:多个连接......”的相同错误。

更新2

我也尝试使用IP地址而不是计算机名称,但这也不起作用,http://support.microsoft.com/kb/938120

5 个答案:

答案 0 :(得分:2)

我找到了解决此问题的方法,似乎始终有效。您需要更改计算机名称,但由于这也将最终停止工作,就像服务器名称和IP一样,您需要选择指定解析到同一台计算机的任意数量的新计算机名称。

显而易见的选择是hosts文件。您可以向IP添加任意数量的别名。然后,使用尚未阻止的别名。

====编辑===

这是一个方便的功能:

<#  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")
}

在脚本开头执行此操作:

$PathAlias = Set-UncHostnameAlias $Path

然后使用New-PSDrive使用别名路径。即使同一系统上的某些其他脚本对同一服务器使用不同的凭据,这也始终有效。

答案 1 :(得分:1)

使用FQND为我工作..

如何找出FQDN ??

ping -a -n 1

以32字节为单位Ping [这是FQND !!!!] [192.168.0.1] 来自192.168.0.1的回复:bytes = 32 time = 1ms TTL = 128

答案 2 :(得分:1)

我在使用本地脚本时遇到了同样的问题,发现这是一个简单的解决方案。 Get-CimInstance返回所有映射的网络连接,然后将其传递给net use / delete / y命令。

$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!" 
}

答案 3 :(得分:0)

您需要使用FQDN而不是NetBios名称。

答案 4 :(得分:0)

我的脚本需要独立于客户端计算机,因为我团队的其他成员可能会运行它。这适合我。不确定是否需要“写入主机”但它也不会妨碍。此外,还存在某种错误,如果它已经存在,则不会再次影响驱动器。

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
}