'************************************************************************ 'DESCRIPTION: Remotely scrub a machine of its printers 'WRITTEN BY: Daniel M. Jones 'DATE: October 30, 2008 'UPDATE: ver 1.0.0 ' 'NOTE: This script completely removes ALL NETWORK printers '************************************************************************ CONST ALL_USERS = "ALL USERS" CONST HKEY_CURRENT_USER = &H80000001 CONST HKEY_LOCAL_MACHINE = &H80000002 CONST HKEY_USERS = &H80000003 dim arrEnumPrinters() dim arrPrinters() dim arrSIDs() dim strComputer dim strCurrentUser boolLocal = false boolSilent = false strComputer = "." set objArgs = WScript.Arguments set objFSO = WScript.CreateObject("Scripting.FileSystemObject") set objShell = WScript.CreateObject("Wscript.Shell") set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv") set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") set tblServices = objWMI.ExecQuery("Select * from Win32_Service ") '************************************************************************************************************************************************ ' FUNCTIONS '************************************************************************************************************************************************ function ping (strComputer) ping = false set objExec = objShell.Exec("%comspec% /c ping.exe " & strComputer & " -n 1 -w 100") do until objExec.Stdout.AtEndOfStream strLine = objExec.StdOut.ReadLine if (inStr(strLine, "Reply")) then ping = true exit function end if loop end function function restartService(strService) set tblServices = objWMI.ExecQuery("Select * from Win32_Service where Name='" & strService & "'") for each objService in tblServices errReturn = objService.StopService() wscript.sleep 2500 errReturn = objService.StartService() next end function function scrubPrinters 'errReturn = objReg.DeleteKey (HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows NT\CurrentVersion\Print\Providers") errReturn = objReg.DeleteKey (HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\PrintPorts") errReturn = objReg.DeleteKey (HKEY_CURRENT_USER, "Printers\DevModePerUser") errReturn = objReg.DeleteKey (HKEY_CURRENT_USER, "Printers\DevModes2") errReturn = objReg.DeleteKey (HKEY_CURRENT_USER, "Printers\Settings") for each strSID in arrSIDs if (len(strSID) > 10) then objReg.EnumKey HKEY_USERS, strSID & "\Printers\Connections", arrEnumPrinters if (isArray(arrEnumPrinters)) then errReturn = objReg.DeleteValue (HKEY_USERS, strSID & "\Printers\DeviceOld") for each strPrinter in arrEnumPrinters strPrinter = lcase(strPrinter) select case (mid(strPrinter, 3, inStr(3, strPrinter, ",") - 3)) case "server1" : boolNetworkPrinter = true case "server2" : boolNetworkPrinter = true case else : boolNetworkPrinter = false end select if (boolNetworkPrinter) then if (boolSilent = false) then msgbox "Erasing: " & replace(strPrinter, ",", "\") errReturn = objReg.DeleteKey (HKEY_USERS, strSID & "\Printers\Connections\" & strPrinter) errReturn = objReg.DeleteValue (HKEY_USERS, strSID & "\Printers\Settings\" & strPrinter) errReturn = objReg.DeleteValue (HKEY_USERS, strSID & "\Software\Microsoft\Windows NT\CurrentVersion\Devices\" & replace(strPrinter, ",", "\")) errReturn = objReg.DeleteValue (HKEY_USERS, strSID & "\SOFTWARE\Microsoft\Windows NT\CurrentVersion\PrinterPorts\" & replace(strPrinter, ",", "\")) end if next end if end if next restartService "Spooler" end function '************************************************************************************************************************************************ ' BEGIN '************************************************************************************************************************************************ if (objArgs.count = 0) then strComputer = inputBox("Computer:") else for i = 0 to objArgs.count - 1 strArg = objArgs(i) select case (ucase(strArg)) case "/COMPUTER" : strComputer = objArgs(i + 1) case "/SILENT" : boolSilent = true case "/LOCAL" : boolLocal = true end select next end if if (boolLocal) then strComputer = "." if (strComputer <> ".") then if (ping(strComputer) = false) then if (boolSilent = false) then msgbox "Computer Unreachable: " & strComputer : wscript.quit end if end if set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\CIMV2") set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") objReg.EnumKey HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", arrSIDs scrubPrinters if (boolSilent = false) then msgbox "Done"