Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 818
  • Last Modified:

Logoff script to delete drive mappings

I am trying to make a logoff script to remove all drive mappings other than the user drive (U:). This is so that I can ensure that any user created mappings are deleted.

My script fails when a drive mapping does not exist and does not move on to the next one.

Can anyone help?

Thanks
Dim wshNetwork, wshShell

on error resume next


Disconnect()


Sub Disconnect()

Set wshNetwork = WScript.CreateObject("WScript.Network")
    wshNetwork.RemoveNetworkDrive "e:"
    wshNetwork.RemoveNetworkDrive "g:"
    wshNetwork.RemoveNetworkDrive "i:"
    wshNetwork.RemoveNetworkDrive "j:"
    wshNetwork.RemoveNetworkDrive "k:"
    wshNetwork.RemoveNetworkDrive "l:"
    wshNetwork.RemoveNetworkDrive "m:"
    wshNetwork.RemoveNetworkDrive "n:"
    wshNetwork.RemoveNetworkDrive "o:"
    wshNetwork.RemoveNetworkDrive "p:"
    wshNetwork.RemoveNetworkDrive "q:"
    wshNetwork.RemoveNetworkDrive "r:"
    wshNetwork.RemoveNetworkDrive "s:"
    wshNetwork.RemoveNetworkDrive "t:"
    wshNetwork.RemoveNetworkDrive "v:"
    wshNetwork.RemoveNetworkDrive "w:"
    wshNetwork.RemoveNetworkDrive "x:"
    wshNetwork.RemoveNetworkDrive "y:"
    wshNetwork.RemoveNetworkDrive "z:"

End sub

Open in new window

0
aideb
Asked:
aideb
  • 2
4 Solutions
 
Shreedhar EtteCommented:
Hi,

Refer this:
http://www.computerperformance.co.uk/Logon/Logon_Remove.htm

Hope this helps,
Shree
0
 
Tony JLead Technical ArchitectCommented:
Try adding this in front of each remove line:

if objfso.DriveExists("R:") wshNetwork.RemoveNetworkDrive "r:"


0
 
Tony JLead Technical ArchitectCommented:
Sorry - forgot to define objFSO for you:

Set objFSO = CreateObject("Scripting.FileSystemObject")
0
 
bobalobCommented:
Also you can check drive type on the fly with objFSO.GetDrive("X:").DriveType, 3 being a network map.

Example code attached.
On Error Resume Next
Dim objDrive
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim wshNetwork: Set wshNetwork = WScript.CreateObject("WScript.Network")

For i = Asc("F") to Asc("Z")
	if i <> Asc("U") then ' Ignore U: drive
		if objFSO.DriveExists(chr(i)) then 
			Set objDrive = objFSO.GetDrive(chr(i))
			if objDrive.DriveType = 3 then ' Network drive
				wshNetwork.RemoveNetworkDrive chr(i) & ":", true
			end if
		end if
	end if
Next
Set objDrive = Nothing
Set objFSO = Nothing
Set wshNetwork = Nothing

Open in new window

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now