Question : Disconnecting a UNC connection

Does anyone know how to find and delete a UNC connection to a server?  I want an automated way to find a UNC connection to a specific server and then delete it. When it's a UNC path, it does not seem to be stored anyplace.

For example, if you run the following command:
net use \\server\share password /u:username

it will attach you to the share.  You can run netstat on the command line and see that you have a connection to the server, but it does not give the share name.  Does anyone know how to determine what connections to a share on a specific server exist and how to remove the connection?  I would like to put this into a VB program when I determine how to accomplish this.  Please NO LINKS!  I want someone who knows how to do this, not someone who will just google the topic and paste links.
Thanks,
BPL

Answer : Disconnecting a UNC connection

Here is what you can do in VBScript. I haven't found a VBScript command to disconnect from an UNC path if there is no drive letter associated. Therefore I use the"net use" command line utility in this case.

      objShell.Run "net use " & objDrive.Item(i+1) & " /del /y"

if there is no drive letter.
      
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
 
Option Explicit
 
Dim objNet, objDrive, i, objShell
 
' enumerate the mapped drives
Set objNet = CreateObject("WScript.Network")
Set objDrive = objNet.EnumNetworkDrives
Set objShell = CreateObject("WScript.Shell")
 
 
If objDrive.Count <> 0 Then
' List all network drives
For i = 0 To (objDrive.Count -1) Step 2
	WScript.Echo "UNC Path " & objDrive.Item(i) & " is mapped to " & objDrive.Item(i +1)
	IF Instr(objDrive.Item(i+1), "172.108.162.47") > 0 Then
		WScript.Echo "Match found, disconnecting from " & objDrive.Item(i+1)
		If objDrive.Item(i) <> "" Then
			objNet.RemoveNetworkDrive objDrive.Item(i), True
		Else
			objShell.Run "net use " & objDrive.Item(i+1) & " /del /y"
		End If
	End If
Next
 
End If
 
 
Wscript.Quit
Random Solutions  
 
programming4us programming4us