Link to home
Start Free TrialLog in
Avatar of SpencerKarnovski
SpencerKarnovski

asked on

How to run a BAT file (to remove old mapped printers) on a user profile - advice needed.

Ok - we have a user who has multiple unused printers attached to her profile.  I have a remove printer BAT file that will remove all the printers, both local and network, from a user profile.

What is the best way to run this bat file.  For example, if I store the BAT file on a server, then navigate to that file from her active session and run the file, it will delete all the printers on the server, where the BAT file is stored.

Do I have to move the BAT file to the workstation she is sitting at? - add it to the locally stored profile in C: > USERS, and run it from her user account via Run as admin?  I obviously have to run the BAT file from her user account, but what is the best practice for doing so.

How do you run scripts that should only effect the logged in users profile, and nothing else. Where do you run them from.

Thanks.
Avatar of Bill Prew
Bill Prew

What does your batch script look like.  Placing it on a shared server location and then executing it from the users workstation should affect the printers on their computer, not the server.  Unless you are doing DELete statements against files, rather than UNINSTALL type commands?

~bp
Avatar of SpencerKarnovski

ASKER

Hello, sorry for the late reply.

Here is the script, this removes all local printers/network printers.  I run this via a standard user profile, and instead of removing all the printers associated with that profile it deleted all the printers on the server where the profile was stored.

Thank god we don't keep profiles stored on the print server ><    Must remember that one, when running scripts, make sure - you are totally sure - that they will not run remotely, and not locally.

Option Explicit

' This script will enumerate all printers and then remove the ones that do not match an array of printers that
' should not be removed. Nor will it remove any Terminal Server or Citrix client printer mappings.
' It will tatoo the registry to ensure it only runs once per user, which makes it easy to manage for the future.
' Note that there are two separate subroutines. One to remove network printers, and another to remove local printers.

' IMPORTANT: This script DOES NOT remove the drivers.

' Written by Jeremy@jhouseconsulting.com on 8th July 2008

Dim WshShell, WSHNetwork, WSHPrinters, arrPrinters, LOOP_COUNTER, strPrinter, strKey, strValue
Dim strComputer, objWMIService, colInstalledPrinters, objPrinter

'These are the printers we DO NOT want to remove.
arrPrinters = Array("Send To OneNote 2007","Microsoft XPS Document Writer","Microsoft Office Document Image Writer","Adobe PDF","Generic / Text Only")

set WshShell = WScript.CreateObject("WScript.Shell")
Set wshNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections

strKey = "HKCU\Printers\"
If RegValueExists(strKey & "PrintersRemoved") Then
  strValue = WshShell.RegRead(strKey & "PrintersRemoved")
  If instr(lCase(strValue),"yes")<>1 Then
    RemoveNetworkPrinters()
    DeleteLocalPrinters()
    UserMessageWithTimeout()
  End If
Else
  RemoveNetworkPrinters()
  DeleteLocalPrinters()
  UserMessageWithTimeout()
End If

'Clean Up Memory We Used
Set WshShell = Nothing
set WSHNetwork = Nothing
Set WSHPrinters = Nothing

wscript.quit(0)

Sub RemoveNetworkPrinters()
  Dim BlnReturn
  For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
    If Instr(WSHPrinters.Item(LOOP_COUNTER +1),"in session")=0 Then
      BlnReturn=InArray(WSHPrinters.Item(LOOP_COUNTER +1),arrPrinters)
      If BlnReturn=False Then
        On Error Resume Next
        WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
        If Err.Number = -2147022646 Then
'          wscript.echo "This is a local printer and cannot be removed using the RemovePrinterConnection method."
          On Error Goto 0
        End If
      End If
    End If
  NEXT
  WshShell.RegWrite strKey & "PrintersRemoved", "Yes - on " & date & " at " & time
End Sub

Sub DeleteLocalPrinters()
  Dim BlnReturn
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' query for installed local printers
  Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")
  For Each objPrinter in colInstalledPrinters
    If Instr(lcase(objPrinter.Name),"in session")=0 Then
      BlnReturn=InArray(objPrinter.Name,arrPrinters)
      If BlnReturn=False Then
        objPrinter.delete_
      End If
    End If
  Next
  Set objWMIService = Nothing
End Sub

Function RegValueExists(sRegValue)
' Returns True or False based of the existence of a registry value.
  Dim oShell, RegReadReturn
  Set oShell = CreateObject("WScript.Shell")
  RegValueExists = True  ' init value
  On Error Resume Next
  RegReadReturn = oShell.RegRead(sRegValue)
  If Err.Number <> 0 Then
    RegValueExists = False
  End if
  On Error Goto 0
  Set oShell = Nothing
End Function

Function InArray(item,myarray)
  Dim i
  For i=0 To UBound(myarray) Step 1
    If lcase(item)=lcase(myarray(i)) Then
      InArray=True
      Exit Function
    End If
  Next
  InArray=False
End Function

Sub UserMessageWithTimeout()
  Dim strMessage, objShell, intReturn
  Const wshOK = 1
  Const wshYes = 6
  Const wshNo = 7
  Const wshOKButton = 0
  Const wshCriticalMessage = 16
  Set objShell = CreateObject("Wscript.Shell")
  strMessage = "** Your printers have now been deleted. **" & VbCr & VbCr & _
  "* You will need to reinstall your printers." & VbCr & _
  "* To do this go to http://myiprint.mydomain.com.au/ipp" & VbCr & _
  "* Note: Printer naming conventions have been modified." & VbCr & _
  "* Note: Detailed printer locations have been added to help you locate your printers." & VbCr & VbCr & _
  "Need Help?" & VbCr & _
  "* The IT Helpdesk will be manned for any assistance users may require."
  intReturn = objShell.Popup(strMessage, 60, "Printing Maintenance", wshOKButton + wshCriticalMessage)
'  If intReturn = wshOK Then
'    Wscript.Echo "You clicked the OK button."
'  Else
'    Wscript.Echo "The popup timed out."
'  End If
  Set objShell = Nothing
End Sub

Open in new window

Okay, I don't really understand how that's happening, so not sure I know how to work around it :-(.

~bp
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia 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