Link to home
Start Free TrialLog in
Avatar of Locutus_NYC
Locutus_NYC

asked on

VBS script that will delete old orphaned network printer mappings from client machines dynamically without knowing share name?

VBS script that will delete old orphaned network printer mappings from client machines dynamically without knowing share name?

I would like to be able to edit my current printer VBS script and add the ability to delete any orphaned/old network printer mappings that clients have.  We have taken over this client and alot of the users have around 8-10 old printer mappings that connect to nowhere (share names don't exist anymore).  Is there a way to script it to just delete the orphaned network share connections?

I attached a portion of our printer script... the current script has the ability to delete printers if the network printer share name is know (bottom of script) - but I'd like to know if its possible to do it without knowing the share name and just by checking if the mapping even has a valid connection.
Set WshNetwork = CreateObject("WScript.Network")
On Error Resume Next
IF PrinterPath = "\\CLIENT\IR4080PS" then
PrinterPath = "\\CLIENT\IR4080PS"
WshNetwork.RemovePrinterConnection PrinterPath, true, true
end if
 
On Error Resume Next
IF PrinterPath = "\\CLIENT\ColorPS" then
PrinterPath = "\\CLIENT\ColorPS"
WshNetwork.RemovePrinterConnection PrinterPath, true, true
end if
 
 
 
On Error Resume Next
IF PrinterPath = "\\CLIENT\Canon5020-PS" then
PrinterPath = "\\CLIENT\Canon5020-PS"
WshNetwork.RemovePrinterConnection PrinterPath, true, true
end if

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of and235100
and235100
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Locutus_NYC
Locutus_NYC

ASKER

RTFM huh.. sorry and thanks :)
This function will delete all mapped printers.  I suggest you it that BEFORE mapping printers.
Function func_DelMapPrinters()
	'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	'
	'  This function will parse through a collection of 
	'  printers and delete each one.
	'
	'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
	DIM strComputer
	DIM strImpLvl
	DIM strQuery
	DIM WMI
	DIM colPrinters
	DIM Printer
 
	strComputer = "."
	strImpLvl = "winmgmts:" & "{impersonationLevel=impersonate}!\\"
	strQuery = "Select * from Win32_Printer"
 
	set WMI = GetObject(strImpLvl & strComputer & "\root\cimv2")
	set colPrinters = WMI.ExecQuery
 
	For Each Printer in ColPrinters
		Net.RemovePrinterConnection Printer.Name
	Next
 
	set strComputer = Nothing
	set strImpLvl = Nothing
	set strQuery = Nothing
	set WMI = Nothing
	set colPrinters = Nothing
	set Printer = Nothing
 
End Function

Open in new window

Thank you. Happy to point a useful resource out...
Avatar of Gastone Canali
The easy solution is delete all network printer mappings and recreate only the right printers
' delete all networked printer
Dim oShell
Set oShell = WScript.CreateObject ("WScript.shell")
cmd="cmd.exe /c reg delete HKCU\Printers\Connections /f"
oShell.run cmd
Set oShell = Nothing
'