xzay1967
asked on
vbscript to delete old printers
I have some printers mapped on my domain, but had to rename them because after two years, management says they don't know which printer is which. Of course after renaming them, the mapping does not work. That is the easy part, I wrote a vbscript to remap the printers, but it re adds the printers, but the old ones are still there. I added the remove printer connection to the vbscript, but it does not delete the old printers, obviously because of the name change the script cannot find them. How do I get the script to remove the old ones, and add the new ones. I was thinking I could tell the script to delete the printer icon based on the name of the printer in the printers and faxes folder. Is this possible? If not, can someone give me an example of a script. I have attached the sript I wrote.
logon1.txt
logon1.txt
Hi There
try using this code i put below to get a list of all printers. Remove them all then map the ones you want.
You need to add the mapping bit below my bit.
Looking at your code it looks like you know how to do this, but let me know if you need help
Regards
Krystian
try using this code i put below to get a list of all printers. Remove them all then map the ones you want.
You need to add the mapping bit below my bit.
Looking at your code it looks like you know how to do this, but let me know if you need help
Regards
Krystian
Dim WshShell, wshNetwork
Dim colPrintersCollection
Dim iPrinterCount
'Create object
Set WshShell = WScript.CreateObject("WScript.Shell")
Set wshNetwork = WScript.CreateObject("WScript.Network")
' Enumerate the drive mappings
Set colPrintersCollection = wshNetWork.EnumPrinterConnections()
' This section deletes ALL current drive mappings. You may not
' want to do all of them, For example the Home (H:) Drive.
For iPrinterCount = 0 To colPrintersCollection.Count - 1 Step 2
wshNetwork.RemovePrinterConnection colPrintersCollection.Item(iPrinterCount), True, True
Next
' Now map your printers
Sorry ignore this comment in my code
' This section deletes ALL current drive mappings. You may not
' want to do all of them, For example the Home (H:) Drive.
It should have read
' This section deletes ALL current printer mappings.
Krystian
' This section deletes ALL current drive mappings. You may not
' want to do all of them, For example the Home (H:) Drive.
It should have read
' This section deletes ALL current printer mappings.
Krystian
ASKER
Thanks Krys, the problem is after renaming the printers, it killed the mapping. I get a network connection does not exist error when I run the script. When I look at the printer icons in the printers and faxes folder, all the old mapped printers, say "printer not found on server, unable to connect" I was hoping to find a script that can "forcibly" delete these and then map the new ones. I was thinking there might be some way to run a script that simply "see" the icons of the bad printers and then deletes them. As an example the name that shows for one of the bad printers is savin 8035 managers on jdr-fp01, then there is the printer not found message under it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You read my mind. I have never used scripting to edit registry so I need some help with that part. I found an example which is attached along with two screenshots of the registry entries with printers in it.
ScreenShot067.jpg
under-windowsnt.jpg
On Error Resume Next
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
strKeyPath = "Printers"
Set objRegistry = GetObject("winmgmts:\\" & _
strComputer & "\root\default:StdRegProv")
DeleteSubkeysName HKEY_CURRENT_USER, strKeypath , ",,server001,SW2-PR1
COLOUR"
Sub DeleteSubkeysName(HKEY_CURRENT_USER, strKeyPath, strName)
objRegistry.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubkeys
If IsArray(arrSubkeys) Then
For Each strSubkey In arrSubkeys
Wscript.Echo strSubKey
if (strSubkey = strName ) then
Wscript.Echo "Deleting " & strKeyPath & "\" & strSubkey
objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath & "\" &
strSubkey
end if
DeleteSubkeysName HKEY_CURRENT_USER, strKeyPath & "\" &
strSubkey, strName
Next
End If
End Sub
reg.txtScreenShot067.jpg
under-windowsnt.jpg
ASKER
Oh I forgot to mention, I need the all the savins deleted. Could you please show me where to make the changes to the code. Thanks a lot.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
They say hind sight is 20/20. I do not remember what the old share names are, otherwise I would have started over. With that being said, I guess I have to figure out to get rid of the ghost printers. Can you help with the script I attached based on the screen shots?
Oh nuts! Well that's a problem......
Oh well.....my concern now is then....see how you've got:
DeleteSubkeysName HKEY_CURRENT_USER, strKeypath , ",,server001,SW2-PR1COLOUR "
that would delete a specific printer entry, but you'd still have to know them.
So I've added the ability for wildcards, so you can use:
DeleteSubkeysName HKEY_CURRENT_USER, strKeypath , "*savin*"
But if any new printers have this naming convention, and are attached already when this part runs, it'll get rid of that too....which I guess is fine if you attach the printer after the registry bit anyway...
Also, what about
HKCU\Printers\Settings
entries? Do they exist for the old printers too?
Regards,
Rob.
Oh well.....my concern now is then....see how you've got:
DeleteSubkeysName HKEY_CURRENT_USER, strKeypath , ",,server001,SW2-PR1COLOUR
that would delete a specific printer entry, but you'd still have to know them.
So I've added the ability for wildcards, so you can use:
DeleteSubkeysName HKEY_CURRENT_USER, strKeypath , "*savin*"
But if any new printers have this naming convention, and are attached already when this part runs, it'll get rid of that too....which I guess is fine if you attach the printer after the registry bit anyway...
Also, what about
HKCU\Printers\Settings
entries? Do they exist for the old printers too?
Regards,
Rob.
On Error Resume Next
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
strKeyPath = "Printers\Connections\"
Set objRegistry = GetObject("winmgmts:\\" & _
strComputer & "\root\default:StdRegProv")
DeleteSubkeysName HKEY_CURRENT_USER, strKeypath , ",,server001,SW2-PR1COLOUR"
DeleteSubkeysName HKEY_CURRENT_USER, strKeypath , "*savin*"
strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\Devices\"
DeleteSubkeysName HKEY_CURRENT_USER, strKeypath , "*savin*"
Sub DeleteSubkeysName(HKEY_CURRENT_USER, strKeyPath, strName)
objRegistry.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubkeys
If IsArray(arrSubkeys) Then
For Each strSubkey In arrSubkeys
Wscript.Echo strSubKey
If InStr(strName, "*") = 0 Then
If LCase(strSubkey) = LCase(strName) Then
Wscript.Echo "Deleting " & strKeyPath & "\" & strSubkey
objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath & "\" & strSubkey
End If
ElseIf Left(strName, 1) = "*" And Right(strName, 1) = "*" Then
If InStr(LCase(strSubkey), LCase(strName)) > 0 Then
Wscript.Echo "Deleting " & strKeyPath & "\" & strSubkey
objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath & "\" & strSubkey
End If
ElseIf Left(strName, 1) = "*" Then
If Right(LCase(strSubkey), Len(strName) - 1) = Right(LCase(strName), Len(strName) - 1) Then
Wscript.Echo "Deleting " & strKeyPath & "\" & strSubkey
objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath & "\" & strSubkey
End If
ElseIf Right(strName, 1) = "*" Then
If Left(LCase(strSubkey), Len(strName) - 1) = Left(LCase(strName), Len(strName) - 1) Then
Wscript.Echo "Deleting " & strKeyPath & "\" & strSubkey
objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath & "\" & strSubkey
End If
End If
DeleteSubkeysName HKEY_CURRENT_USER, strKeyPath & "\" & strSubkey, strName
Next
End If
End Sub
Thanks for the grade. I hope you managed to get around your issues here, and that we provided enough info to help you.
Regards,
Rob.
Regards,
Rob.
ASKER
Open in new window