为什么“net use * /delete”不起作用但在我的 PowerShell 脚本中等待确认?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13284106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:41:37  来源:igfitidea点击:

Why "net use * /delete" does not work but waits for confirmation in my PowerShell script?

.netpowershellmapped-drivenet-use

提问by pencilCake

I have a script where I want to disconnect from the mapped drives before I create a new PSDrive.

我有一个脚本,我想在创建新的 PSDrive 之前断开与映射驱动器的连接。

Otherwise I get this 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

New-PSDrive :不允许同一用户使用多个用户名多次连接到服务器或共享资源。断开与服务器或共享资源的所有先前连接,然后重试

So I have this line:

所以我有这条线:

net use * /delete

(Unfortunately I could not find a way to disconnect from a specific mapped drive just by providing the server name so far :( )

(不幸的是,到目前为止,我无法通过提供服务器名称来找到与特定映射驱动器断开连接的方法:()

When PS comes to this line

当PS来到这一行

You have these remote connections:

\\ServerName\SharedFolder Continuing will cancel the connections.

您有这些远程连接:

\\ServerName\SharedFolder 继续将取消连接。

And then it stops executing.

然后它停止执行。

Is there a way to auto confirm disconnecting from the mapped drives automatically without confirmation (it does not have to be the net use /delete solution)?

有没有办法在没有确认的情况下自动确认从映射驱动器断开连接(它不必是网络使用/删除解决方案)?

Note that: I run my script from the Powershell ISE PS promt

请注意:我从 Powershell ISE PS promt 运行我的脚本

回答by Nick

Try this:

尝试这个:

net use * /delete /y

The /ykey makes it select Yes in prompt silently

/y键使得它在提示是选择默默

回答by Slogmeister Extraordinaire

With PowerShell 5.1 in Windows 10 you can use:

使用 Windows 10 中的 PowerShell 5.1,您可以使用:

Get-SmbMapping | Remove-SmbMapping -Confirm:$false