[Webinar] Streamline your web hosting managementRegister Today

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

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

0
Locutus_NYC
Asked:
Locutus_NYC
1 Solution
 
Locutus_NYCAuthor Commented:
RTFM huh.. sorry and thanks :)
0
 
sr75Commented:
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

0
 
and235100Commented:
Thank you. Happy to point a useful resource out...
0
 
canaliCommented:
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
'


 
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

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