Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Don't install printers if they already exist

Status
Not open for further replies.

58sniper

MIS
Apr 25, 2004
9,152
US
I swear I've seen this posted here before, but I can't seem to find it.

I have this script, and it all appears to work fine. What I'd like to do is use some IF statements for the printers. If they are not installed, install them. If they are installed, ignore them. How can that be done?

Is there a way to do the same with the mapped drives?

Code:
'********************************************************************************** 
' Set Environment Variables 
'********************************************************************************* 
Set WSHNetwork = WScript.CreateObject("WScript.Network") 
Set WSHShell = WScript.CreateObject("WScript.Shell") 

On Error Resume Next 

Domain = WSHNetwork.UserDomain 
UserName = "" 

While UserName = "" 
UserName = WSHNetwork.UserName 

MyGroups = GetGroups(Domain, UserName) 
Wend 

'********************************************************************************* 
'Function: GetGroups
'********************************************************************************* 
Function GetGroups(Domain, UserName) 
	Set objUser = GetObject("WinNT://" & Domain & "/" & UserName) 
	GetGroups="" 
	For Each objGroup In objUser.Groups 
		GetGroups=GetGroups & "[" & UCase(objGroup.Name) & "]" 
	Next 
End Function 

'******************************************************************************** 
'Function: InGroup 
'******************************************************************************** 
Function InGroup(strGroup) 
	InGroup=False 
	If InStr(MyGroups,"[" & UCase(strGroup) & "]") Then 
		InGroup=True 
	End If 
End Function 


'******************************************************************************** 
'Map Drives:
'******************************************************************************** 
WSHNetwork.RemoveNetworkDrive "i:", True, True
WSHNetwork.RemoveNetworkDrive "j:", True, True
WSHNetwork.RemoveNetworkDrive "l:", True, True
WSHNetwork.RemoveNetworkDrive "p:", True, True
WSHNetwork.RemoveNetworkDrive "q:", True, True
WSHNetwork.RemoveNetworkDrive "r:", True, True
WSHNetwork.RemoveNetworkDrive "s:", True, True
WSHNetwork.MapNetworkDrive "i:", "\\velvet\infoctr"
WSHNetwork.MapNetworkDrive "j:", "\\velvet\reference"
WSHNetwork.MapNetworkDrive "l:", "\\clio\images"
WSHNetwork.MapNetworkDrive "p:", "\\velvet\projects"
WSHNetwork.MapNetworkDrive "q:", "\\survey\www"
WSHNetwork.MapNetworkDrive "r:", "\\survey\databases"
WSHNetwork.MapNetworkDrive "s:", "\\velvet\shared"

if ingroup ("netadmin") then
	WSHNetwork.MapNetworkDrive "u:", "\\clio\netadmin"
end if

'******************************************************************************** 
'Map Printers:
'******************************************************************************** 
WshNetwork.RemovePrinterConnection "\\journey\color"
WshNetwork.RemovePrinterConnection "\\journey\colorduplex"
WshNetwork.RemovePrinterConnection "\\journey\production"
WshNetwork.RemovePrinterConnection "\\journey\productionduplex"
WshNetwork.RemovePrinterConnection "\\journey\upstairs"
WshNetwork.RemovePrinterConnection "\\journey\upstairsduplex"
WshNetwork.RemovePrinterConnection "\\journey\downstairs"
WshNetwork.RemovePrinterConnection "\\journey\downstairsduplex"

WSHNetwork.AddWindowsPrinterConnection "\\velvet\color"
WSHNetwork.AddWindowsPrinterConnection "\\velvet\colorduplex"
WSHNetwork.AddWindowsPrinterConnection "\\velvet\production"
WSHNetwork.AddWindowsPrinterConnection "\\velvet\productionduplex"
WSHNetwork.AddWindowsPrinterConnection "\\velvet\upstairs"
WSHNetwork.AddWindowsPrinterConnection "\\velvet\upstairsduplex"
WSHNetwork.AddWindowsPrinterConnection "\\velvet\downstairscopier"
WSHNetwork.AddWindowsPrinterConnection "\\velvet\downstairscopierduplex"
WSHNetwork.AddWindowsPrinterConnection "\\velvet\upstairscopier"
WSHNetwork.AddWindowsPrinterConnection "\\velvet\upstairscopierduplex"

If INGROUP ("2North") Then
	WSHNetwork.SetDefaultPrinter "\\velvet\upstairs"
End If

If INGROUP ("2South") Then
	WSHNetwork.SetDefaultPrinter "\\velvet\productionduplex"
End If

If INGROUP ("1st") Then
	WSHNetwork.SetDefaultPrinter "\\velvet\downstairscopierduplex"
End If



Pat Richard, MCSE MCSA:Messaging CNA
Microsoft Exchange MVP
Want to know how email works? Read for yourself -
 
You could query WMI to build a list of printers as shown in the example, then do some IF instr() to check if the printer is not installed, then AddWindowsPrinterConnection.

From my experience, a printer will not 'reinstall' if it is already installed - so there is no time wasted. The printer will only be added if the printer is not already installed. This WMI example takes a while to query the computer, so I dont think that it is worth the time or the extra lines of script.

Example
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery ("SELECT * FROM Win32_Printer")
For Each objItem In colItems
	printerList = printerlist & "[" & objItem.name & "]"
Next




 
Hey Pat,

Take a look at my login script FAQ. I have code for removing network printers which you could modify to check for a printer and move on if it already exists. Same thing witht he mapped drives. Take a look at my add-on for moving network shares to a different server. Since this grabs the path you could use that to compare as well.

Neither are full solutions for you but they have the bits you need to be able to identify the information you are looking for.

I do agree however with the above post that the added time it takes to check may not be worth it.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Mark - Thanks. I actually built some of that script from your FAQ.

I'm just trying to make sure I'm not reinstalling printers again if they already exist on the client workstation. I think I've got it figured out.

Pat Richard, MCSE MCSA:Messaging CNA
Microsoft Exchange MVP
Want to know how email works? Read for yourself -
 
'blnMapPrinter has global scope in a class

Public Function MapPrinter(strPrinter, blnDisconnect)
'initilize printers dicObject
On Error Resume Next
If blnMapPrinter = False Then
'msgbox "init printers"
Dim strRootKey, arrSubKeys, refRegistry, subKey, strPrinterMapping

Set enumPrinters = WScript.CreateObject("Scripting.Dictionary")
Set refRegistry = GetObject("winmgmts:root\default:StdRegProv")
Const HKEY_CURRENT_USER = &H80000001
strRootKey = "Printers\Connections"

If refRegistry.EnumKey(HKEY_CURRENT_USER, strRootKey, arrSubKeys) = 0 Then
'init dictionary object of printers
For Each subKey In arrSubKeys
strPrinterMapping = ""
strPrinterMapping = Trim(LCase(CStr(subKey)))
If InStr(strPrinterMapping, ",,") = 1 Then
strPrinterMapping = Replace(strPrinterMapping, ",", "\")
If enumPrinters.Exists(strPrinterMapping) Then
'already have this one, unlikely
Else
'Wscript.Echo "adding " & strPrinterMapping & " to enumPrinters"
enumPrinters.Add strPrinterMapping, "1"
End If
End If
Next
End If
Set refRegistry = Nothing
blnMapPrinter = True
End If

Dim blnConnected
Dim i
blnConnected = False

If enumPrinters.Exists(LCase(strPrinter)) Then
blnConnected = True
End If
'Wscript.Echo strPrinter
'Wscript.Echo blnDisconnect & " disconnect"
'Wscript.Echo blnConnected & " already connected"
'can i check if printer is already available???

If blnConnected = False Then
'msgbox strPrinter
On Error Resume Next
Err.Clear
WshNetwork.AddWindowsPrinterConnection strPrinter
If Err.Number = 0 Then
MapPrinter = 0
Else
MapPrinter = 4
'msgbox "couldnt map printer"
End If
On Error Goto 0
ElseIf blnConnected = True And blnDisconnect = "False" Then
'msgbox "cant map printer " & strPrinter & " already in use and disconnect set to false."
MapPrinter = 8
ElseIf blnConnected = True And blnDisconnect = "True" Then
'msgbox strPrinter

WshShell.RegDelete "HKCU\Printers\Connections\" & Replace(strPrinter, "\", ",") & "\"
'Wscript.Echo Err.Number & " after remove registry printer"
On Error Resume Next
'msgbox ""
WshNetwork.AddWindowsPrinterConnection strPrinter
If Err.Number = 0 Then
MapPrinter = 0
Else
MapPrinter = 2
'msgbox "couldnt map printer"
End If
On Error Goto 0
End If

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top