Powershell

如何使用 powershell 檢查映射的驅動器是否打開/連接?

  • July 20, 2010

我正在編寫一個登錄腳本,它取消映射並重新映射一些驅動器。

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

這一切都很好,除非它試圖取消映射的驅動器有一個打開的連接。如果使用者打開了一個文件,那麼它就會掛起。

有沒有辦法在我嘗試取消映射之前檢查映射驅動器是否打開了任何連接,如果有則跳過取消映射它?

謝謝,

Ben

該腳本可能正在等待刪除連接的權限

例如

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

引用自:https://serverfault.com/questions/162122