[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Printer Mapping Script

Posted on 2009-04-23
5
Medium Priority
?
516 Views
Last Modified: 2012-05-06
Hey All,

The below script is currently being used as a vbs logon script, which just maps the printers based on the system ip address. My problem is that I cannot figure out a way to create a trigger that looks to see if the drive is already mapped, then determines what to do from there.

Something like:

IF \\server\printer_name is mapped then DIE :) Any help would be appreciated.
Dim strReturnIP, sThirdOctet
Dim arrOctets
 
' SETS STRING RETURNIP
strReturnIP = GetIP(".") ' a . is the local machine
 
' VERIFIES VALID IP ADDRESS
If InStr(strReturnIP, ".") > 0 Then
	arrOctets = Split(strReturnIP, ".")
	sThirdOctet = arrOctets(2)
 
	' SETS UNC PRINTER OBJECTS
	Set objDict = CreateObject("Scripting.Dictionary")
 
	' PINNACLE OFFICE, DEFAULT IS PRIMARY PRINTER
	objDict.Add "205", "\\ventsvr\PINN_HQ 2015_A;True|\\ventsvr\Pinn_CS 3050_A;False"
 
	' ST. MARY'S OFFICE, DEFAULT IS RECEPTION AREA
	objDict.Add "100", "\\ventsvr\StMarys_HP 3005_A;True|\\ventsvr\StMarys_HP 3005_B;False|\\ventsvr\StMarys_HP 3005_HCFA;False|\\ventsvr\StMarys_HQ 2015_A;False"
 
	' NW TUCSON OFFICE, DEFAULT IS PRIMARY PRINTER
	objDict.Add "80", "\\ventsvr\NWTucson_HP 3005_A;True"
 
	' Foothills OFFICE, DEFAULT IS PRIMARY PRINTER
	objDict.Add "70", "\\ventsvr\Foothills_HP 2055_A;True|\\ventsvr\Foothills_HP 2055_B;False"
 
	' COMMERCIO OFFICE, DEFAULT IS MA STATION
	objDict.Add "60", "\\clserver\Comm_LJ 3005_MA_Station;True|\\ventsvr\Comm CS 3050_A;False|\\ventsvr\Comm_HP 2035_A;False"
 
	' GREEFIELD OFFICE, DEFAULT IS PRIMARY PRINTER
	objDict.Add "50", "\\ventsvr\GF_HP 3005_A;True"
 
	' WARNER OFFICE, DEFAULT IS MA STATION
	objDict.Add "20", "\\ventsvr\Warner_HP 2015_B;True|\\ventsvr\Warner_CS 3050_A;False|\\ventsvr\Warner_HP 2015_A;False"
 
	' Arrowhead OFFICE, DEFAULT IS TRAY 2 PRIMARY PRINTER
	objDict.Add "10", "\\ventsvr\Arrowhead_HP 3005_T2;True|\\ventsvr\Arrowhead_HP 3005_T1;False|\\ventsvr\Arrowhead_HP 3005_T3;False|\\ventsvr\Arrowhead CS 3050_A;False"
 
	' ESTRELLA OFFICE, DEFAULT IS PRIMARY PRINTER
	objDict.Add "9", "\\ventsvr\Estrella_HP 1300_A;True"
 
	' MAP PRINTER BASED ON 3RD OCTET
	If objDict.Exists(sThirdOctet) = True Then
		arrPrinters = Split(objDict(sThirdOctet), "|")
		For Each strPrinter In arrPrinters
			If InStr(strPrinter, ";") > 0 Then
				strPrinterPath = Split(strPrinter, ";")(0)
				boolDefault = Split(strPrinter, ";")(1)
			Else
				strPrinterPath = strPrinter
				boolDefault = False
			End If
			MapPrinter strPrinterPath, boolDefault
		Next
	Else
		' RETURNS ERROR IF NO IP ASSIGNED TO PRINTER (DISABLED)
		'WScript.Echo "No printers have been assigned to the " & sThirdOctet & " subnet."
	End If
Else
	' RETURNS ERROR IF NO IP ADDRESS IS FOUND (DISABLED)
	'MsgBox "There was an error finding your IP Address." & VbCrLf & _
		'"The IP address returned was: " & strReturnIP
End If
 
' MAP THE PRINTER IF IP VALID, USES UNC PATH, SETS DEFAULT PRINTER
Sub MapPrinter(sArgPrinterUNC, bArgDefault)
 
	On Error Resume Next
	Dim oNet
 
	Set oNet = WScript.CreateObject("WScript.Network")
		oNet.AddWindowsPrinterConnection sArgPrinterUNC
		
		If bArgDefault = True Then
			oNet.SetDefaultPrinter sArgPrinterUNC
		End If
	
End Sub
 
' GETS IP ADDRESS OF THE COMPUTER PASSED TO US
Function GetIP(sArgComputer)
 
	On Error Resume Next
	Dim objWMIService
	Dim colItems
	Dim objItem
	Dim strIPAddress
	
 
	Set objWMIService = GetObject("winmgmts:" _
		& "{impersonationLevel=impersonate}!//" & sArgComputer & "\root\cimv2")
 
	If Err.Number = 0 Then
		Set colItems = objWMIService.ExecQuery _
			("Select * from win32_NetworkAdapterConfiguration WHERE IPEnabled = True",,48)
 
			'Go through the addresses
			For Each objItem in colItems
				If objItem.IPAddress(0) <> "0.0.0.0" Then
					strIPAddress = objItem.IPAddress(0) 'Join(objItem.IPAddress, ".")
					Exit For
				End If
			Next
	End If
 
	GetIP = strIPAddress
 
End Function

Open in new window

0
Comment
Question by:ValleyENT
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:bobpsmith
ID: 24221078
Can you use the EnumPrinterConnections method as described in the Windows Scripting documenation? Below is an example from the doc's...

         Set WshNetwork = WScript.CreateObject("WScript.Network")
         Set oDrives = WshNetwork.EnumNetworkDrives
         Set oPrinters = WshNetwork.EnumPrinterConnections
         WScript.Echo "Network drive mappings:"
         For i = 0 to oDrives.Count - 1 Step 2
            WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
         Next
         WScript.Echo
         WScript.Echo "Network printer mappings:"
         For i = 0 to oPrinters.Count - 1 Step 2
            WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
         Next
0
 
LVL 10

Accepted Solution

by:
TakedaT earned 1500 total points
ID: 24221145
You can probably use a function similar to the following just before you map your printer.  This particular one checks each installed printer on the machine to see if its name matches the "strPrinterName" passed to it.  If it does match, then the printer is already installed and the script quits.  The Win32_Printer class also has many other properties that you can pull other than .name, like .portname for example.  Here is a link to the msdn page for the class.

http://msdn.microsoft.com/en-us/library/aa394363(VS.85).aspx
Function CheckForPrinter(strPrinterName)
	bPrinterFound = False
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:" _
  	  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colInstalledPrinters =  objWMIService.ExecQuery _
   	 ("Select * from Win32_Printer")
	For Each objPrinter in colInstalledPrinters
		If objPrinter.Name = strPrinterName then
			bPrinterFound = True
		End If
	Next
	If bPrinterFound = True then wscript.quit
End Function

Open in new window

0
 
LVL 4

Author Comment

by:ValleyENT
ID: 24221590
I couldn't get either to work. must be an easier way to do this.
0
 
LVL 10

Expert Comment

by:TakedaT
ID: 24224432
On a machine that has at least one of these printers installed, could you issue the following command at a cmd prompt:
wmic path win32_printer get *>C:\wmicget.txt
Then post the file C:\wmicget.txt here so I can see its contents

0
 
LVL 4

Author Closing Comment

by:ValleyENT
ID: 31574054
I spent some time on this one and developed my own variation.

Thanks~
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn how to dynamically set the form action using jQuery.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month20 days, 5 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question