如何使用 powershell 检查映射驱动器是否打开/连接?

如何使用 powershell 检查映射驱动器是否打开/连接?

我正在编写一个取消映射和重新映射某些驱动器的登录脚本。

powershell 调用一个小的批处理文件来进行实际的取消映射,因为 powershell 在可靠地映射驱动器方面似乎有点不稳定。

我使用的代码是:

    $arrDrives = "m:","n:","o:","p:","q:","r:","s:","u:","v:","x:","y:"
    foreach ($drive in $arrDrives) { 
        if (test-path $drive) {
            UpdateSubHeading ("Removing drive " + $drive)
            c:\bin\removeDrive.bat $drive } 
    }

它调用的批处理文件只是:

if exist %1 net use %1 /del

一切正常,除非尝试取消映射的驱动器有打开的连接。如果用户打开了文件,则系统会挂起。

在我尝试取消映射之前,有没有什么方法可以检查映射驱动器上是否有任何打开的连接,如果有,则跳过取消映射?

谢谢,

答案1

该脚本可能正在等待删除连接的权限

例如

M:\>net use m: /delete
There are open files and/or incomplete directory searched pending on the connection to m:.

Is it OK to continue disconnection and force them closed (Y/N) [N]:

您需要在命令行上提供权限

例如

M:\>net use m: /delete /yes
There are open files and/or incomplete directory searched pending on the connection to m:.

m: was deleted successfully

或者你的情况

if exist %1 net use %1 /del /yes

相关内容