Solved

Logoff script to delete drive mappings

Posted on 2010-09-01
4
814 Views
Last Modified: 2012-06-27
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
Comment
Question by:aideb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 34

Assisted Solution

by:Shreedhar Ette
Shreedhar Ette earned 50 total points
ID: 33574739
Hi,

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

Hope this helps,
Shree
0
 
LVL 26

Assisted Solution

by:Tony Johncock
Tony Johncock earned 250 total points
ID: 33574761
Try adding this in front of each remove line:

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


0
 
LVL 26

Assisted Solution

by:Tony Johncock
Tony Johncock earned 250 total points
ID: 33574772
Sorry - forgot to define objFSO for you:

Set objFSO = CreateObject("Scripting.FileSystemObject")
0
 
LVL 3

Accepted Solution

by:
bobalob earned 200 total points
ID: 33585449
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question