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

我正在写一个login脚本,取消映射和重新映射某些驱动器。

PowerShell调用一个小的batch file来实际解映射,因为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 } } 

它所调用的batch file只是:

 if exist %1 net use %1 /del 

这一切工作正常,除非有一个开放的连接到它试图取消映射的驱动器。 如果用户打开一个文件,则会挂起。

有没有一种方法可以检查映射的驱动器是否有任何连接打开,然后尝试取消映射,如果存在则跳过取消映射?

谢谢,

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

例如

 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