remount-drives-for-admin.vbs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. Option Explicit
  2. Dim objNetwork, objShell
  3. Dim strDriveLetter, strNetworkPath
  4. Dim colDrives, intDrive, strDrives
  5. If WScript.Arguments.length =0 Then
  6. Set objNetwork = CreateObject("WScript.Network")
  7. Set colDrives = objNetwork.EnumNetworkDrives
  8. For intDrive = 0 To (colDrives.Count -1) Step 2
  9. ' WScript.Echo colDrives.Item(intDrive) & " is mapped to: " & colDrives.Item(intDrive + 1)
  10. If Len(strDrives) > 0 Then strDrives = strDrives & " "
  11. strDrives = strDrives & " " & Chr(34) & colDrives.Item(intDrive) & Chr(34) & " " & Chr(34) & colDrives.Item(intDrive + 1) & Chr(34)
  12. Next
  13. If Len(strDrives) > 0 Then
  14. ' re-call script with elevation
  15. Set objShell = CreateObject("Shell.Application")
  16. objShell.ShellExecute "cscript.exe", Chr(34) & WScript.ScriptFullName & Chr(34) & strDrives, "", "runas", 1
  17. Else
  18. WScript.Echo "No drives Mapped."
  19. End If
  20. Else
  21. ' elevated part
  22. Set objNetwork = CreateObject("WScript.Network")
  23. For intDrive = 0 To (WScript.Arguments.Count - 1) Step 2
  24. WScript.Echo WScript.Arguments(intDrive) & " is mapped to: " & WScript.Arguments(intDrive + 1)
  25. On Error Resume Next ' ignore already mapped drives
  26. objNetwork.MapNetworkDrive WScript.Arguments(intDrive), WScript.Arguments(intDrive + 1)
  27. On Error GoTo 0
  28. Next
  29. End If