gpt4 book ai didi

powershell - net use */delete/y 不能解决错误 "New-PSDrive : Multiple connections to a server ...."

转载 作者:行者123 更新时间:2023-12-04 00:42:02 25 4
gpt4 key购买 nike

这个 New-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

最佳答案

我发现这个问题的解决方法似乎总是有效。您需要更改计算机名称,但由于这与服务器名称和 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 .这始终有效,即使同一系统上的其他一些脚本对同一服务器使用不同的凭据。

关于powershell - net use */delete/y 不能解决错误 "New-PSDrive : Multiple connections to a server ....",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25995858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com