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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Map network printers based on workstation IP addresses

Status
Not open for further replies.

Anatec

IS-IT--Management
Apr 13, 2015
13
US
thread329-1642011
Does anyone know what the resolution to this thread was for the network printing portion? I'm in the same spot as this guy was... I am looking for a VBScript that automatically adds printers based on the third octet in the IP address to determine which set of printers get mapped to the user's machine. I am very new to scripting, so please make you explanation as basic as possible. If you can highlight the area where I would need to input my specifics, that would be great. NB: As a bonus, I would love to be able to remove existing network printers prior to mapping the new ones.
Thanks in advance.
 
Since that thread involved a lot of code that was modified here and there, it would be easier for you to start by posting the code you are trying now, and explain what is (or is not) working.
 
Honestly, I don't know anything about scripting so I was hoping to just use some of the code that was in the previous post. Here's what I have...

Set objShell = CreateObject("Wscript.Shell")
set objExec = objShell.Exec("%comspec% /c ipconfig.exe")
set objNetwork = WScript.CreateObject("WScript.Network")

'loop through the stream looking for key phrase "IP Address"
do while NOT (objExec.Stdout.AtEndOfStream)

strLine = objExec.Stdout.ReadLine
if(inStr(strLine, "IP Address")) then
'Parse strLine to obtain IP.
strIP = trim(right(strLine, len(strLine) - inStrRev(strLine, ":")))
exit do
end if
loop
arrTokens = split(strIP, ".")
select case strIP arrTokens(2)
'2nd Floor
case "10.231.147"
objNetwork.AddWindowsPrinterConnection "\\nesvr-prt01\add_2nd_flr_printers.bat"

end select

I get the following error:

Line: 18
Char: 4
Error: Syntax error
Code: 800A03EA
Source: Microsoft VBScript compilation error
 
Assuming you want it based on the first three octets, the code below cleans up the retrieval of the IP address anyway:

Code:
Set objShell = CreateObject("Wscript.Shell")
Set objExec = objShell.Exec("%comspec% /c ipconfig.exe")
Set objNetwork = WScript.CreateObject("WScript.Network")

'loop through the stream looking for key phrase "IP Address"
Do While NOT (objExec.Stdout.AtEndOfStream)

	strLine = objExec.Stdout.ReadLine
	If (InStr(strLine, "[highlight #8AE234]IP Address[/highlight]")) Then
		'Parse strLine to obtain IP.
		strIP = Trim(Right(strLine, Len(strLine) - InStrRev(strLine, ":")))
		Exit Do
	End If
Loop

arrTokens = Split(strIP, ".")
strIPBase  = arrTokens(0) & "." & arrTokens(1) & "." & arrTokens(2)
Select Case strIPBase 
	'2nd Floor
	Case "10.231.147"
		objNetwork.AddWindowsPrinterConnection "\\nesvr-prt01\add_2nd_flr_printers.bat"

End Select

Note that this uses the output of "ipconfig.exe" to retrieve the local IP address, which is not a good method. For one, the text I hightlighted would work in XP, but not in Win7 for example (the text is [highlight #8AE234]IPv4 Address[/highlight] in Win7). You might be better off using a WMI query to get the IP address, like the code in the link below:
 
You might want to check out my login script FAQ. faq329-5798

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Sounds good.. I made a few tweaks. My goal is to have this be a logon script where it pulls the IP from the workstation and then determines, based on the third octet of the IP, which printer script to run. Does that make sense? I appreciate your help.

Code:
dim NIC1, Nic, sIP

Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
if Nic.IPEnabled then
sIP = Nic.IPAddress(i)
Set WshNetwork = WScript.CreateObject("WScript.Network")

iFirstDot = InStr(0, sIP,".")
iSecondDot = InStr(iFirstDot+1,sIP,".")
iThirdDot = InStr(iSecondDot+1,sIP,".")
sThirdOctet = Mid(sIP, iSecondDot+1, Len(sIP)-iThirdDot)

'Map printer based on octet
Select Case sThirdOctet
   case "146"
	MapPrinter "\\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat"
   case "147"
	MapPrinter "\\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat"

end Select

I get the following error:
Line: 20
Char: 11
Error: Expected 'End'
Code: 800A03F7
Source: Microsoft VBScript compilation error
 
1) This code is quite different from what you posted originally. And if you executed the code you just posted, you would have gotten errors long before "Line 20 Expected End", so I dont really know what code you're working with now.
2) You did not include all the relevant code to get the IP address from the link I gave above. I moved the relevant code to a function called "GetIP" below to help clean things up.
3) I do not know what "MapPrinter" is, so I will assume it's a subroutine that you did not post, and that it works properly.

Code:
sIP = GetIP()
If sIP <> "" Then
	
   iFirstDot = InStr([highlight #FCE94F]1[/highlight], sIP,".")
   iSecondDot = InStr(iFirstDot+1,sIP,".")
   iThirdDot = InStr(iSecondDot+1,sIP,".")
   sThirdOctet = Mid(sIP, iSecondDot+1, Len(sIP)-iThirdDot)

   'Map printer based on octet
   Select Case iThirdDot
      case "146"
         MapPrinter "\\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat"
      case "147"
         MapPrinter "\\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat"
   end Select 
end If


Function GetIP()
   dim NIC1, Nic, StrIP, CompName
   Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
   For Each Nic in NIC1
      if Nic.IPEnabled then
         GetIP = Nic.IPAddress(i)
      end if
   next
End Function
 
The Select should have read:
[pre] Select Case [highlight #FCE94F]sThirdOctet[/highlight][/pre]
 
Ok so I updated a few things... I do not get any error codes but when I run the vbscript the printer scripts from the print server are not running so I'm assuming something isn't right.. do you have any idea what that might be?

Code:
set objNetwork = WScript.CreateObject("WScript.Network")
sIP = GetIP()
If sIP <> "" Then
	
   iFirstDot = InStr(1, sIP,".")
   iSecondDot = InStr(iFirstDot+1,sIP,".")
   iThirdDot = InStr(iSecondDot+1,sIP,".")
   sThirdOctet = Mid(sIP, iSecondDot+1, Len(sIP)-iThirdDot)

   'Map printer based on octet
   Select Case iThirdOctet
      case "146"
         objNetwork.AddWindowsPrinterConnection "\\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat"
      case "147"
         objNetwork.AddWindowsPrinterConnection "\\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat"
   end Select 
end If


Function GetIP()
   dim NIC1, Nic, StrIP, CompName
   Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
   For Each Nic in NIC1
      if Nic.IPEnabled then
         GetIP = Nic.IPAddress(i)
      end if
   next
End Function
 
Another way to identify the third octet would be to just split the IPAddress based on the periods. I think this is a little easier to follow for that portion. Arrays start at zero, so the first octet is element zero, the second octet is element 1, the third is element 2 and the final octet is element 3.

Code:
IPAddress = "192.168.100.18"
IPArray = Split(IPAddress,".")
ThirdOctet = IPArray(2)
WScript.Echo "Third Octet is:" & ThirdOctet



I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Okay Mark, that seems easier than what I had... how would you replace that in the current code so that it is functional? I tried the following but get an error on line 6. Sorry I'm so new at scripting, literally picked it up this last week.

Code:
set objNetwork = WScript.CreateObject("WScript.Network")
sIP = GetIP()
If sIP <> "" Then
	
   IPArray = Split(IPAddress,".")
   ThirdOctet = IPArray(2)

   'Map printer based on octet
   Select Case ThirdOctet
      case "146"
         objNetwork.AddWindowsPrinterConnection "\\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat"
      case "147"
         objNetwork.AddWindowsPrinterConnection "\\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat"
   end Select 
end If


Function GetIP()
   dim NIC1, Nic, StrIP, CompName
   Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
   For Each Nic in NIC1
      if Nic.IPEnabled then
         GetIP = Nic.IPAddress(i)
      end if
   next
End Function
 
Code:
set objNetwork = WScript.CreateObject("WScript.Network")
sIP = GetIP()
If sIP <> "" Then
	
   IPArray = Split([highlight #FCE94F]sIP[/highlight],".")
   ThirdOctet = IPArray(2)

   'Map printer based on octet
   Select Case ThirdOctet
      case "146"
         objNetwork.AddWindowsPrinterConnection "\\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat"
      case "147"
         objNetwork.AddWindowsPrinterConnection "\\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat"
   end Select 
end If


Function GetIP()
   dim NIC1, Nic, StrIP, CompName
   Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
   For Each Nic in NIC1
      if Nic.IPEnabled then
         GetIP = Nic.IPAddress(i)
      end if
   next
End Function 

Great post?

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
markdmac said:
Another way to identify the third octet would be to just split the IPAddress based on the periods
Yep, which is what the OP was doing in their second post... then somehow that changed

Anatec: You need to go line by line through your code and look at what each line is doing:

[pre]Line 1: set objNetwork = WScript.CreateObject("WScript.Network")
Line 2: sIP = GetIP()
Line 3: If sIP <> "" Then
Line 4:
Line 5: IPArray = Split(IPAddress,".")
Line 6: ThirdOctet = IPArray(2)[/pre]

In Line 2, you are storing the IP address in a variable called "sIP". But in Line 5, you are running the Split command on a variable called "IPAddress", which does not exist. The result is an empty array and an error on Line 6. You have to be consistent with your variables.

Line 5 should read [highlight #FCE94F]IPArray = Split(sIP, ".")[/highlight]
 
Haha.. figured it out after I thought about it. Now with the following code I get an error on line 13 that says "the printer name is invalid." but I think it is because I'm am trying to run a .bat file. I replaced \\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat with \\nesvr-prt01\FLR2_RICOH_1 and it added! My next question is how do I get this to run a batch file? I tried swapping out the following code but didn't have any luck...
Code:
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "c:\batch\test.cmd"
 
Sorry if that was confusing... this is what it looks like now with an error on line 15 stating "the network path was not found."
FYI, "\\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat" is a shared path on our printer server that everyone can access

Code:
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
set objNetwork = WScript.CreateObject("WScript.Network")
sIP = GetIP()
If sIP <> "" Then
	
   IPArray = Split(sIP,".")
   ThirdOctet = IPArray(2)

   'Map printer based on octet
   Select Case ThirdOctet
      case "146"
         objShell.run "\\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat"
      case "147"
         objShell.run "\\nesvr-prt01\!Print drivers\Omaha Printers\add_2nd_flr_printers.bat"
   end Select 
end If


Function GetIP()
   dim NIC1, Nic, StrIP, CompName
   Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
   For Each Nic in NIC1
      if Nic.IPEnabled then
         GetIP = Nic.IPAddress(i)
      end if
   next
End Function
 
I guess I could just manually list out every printer.. just not sure if that would effect performance since it will be a logon script.
 
Take a look at my login script FAQ linked above. You should only need to actually map the printer, not run a batch file. When connecting to the shared printer it will download the drivers from the server.

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Here is an example of how to add a printer connection to a shared printer.

Code:
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
set objNetwork = WScript.CreateObject("WScript.Network")
sIP = GetIP()
If sIP <> "" Then
	
   IPArray = Split(sIP,".")
   ThirdOctet = IPArray(2)

   'Map printer based on octet
   Select Case ThirdOctet
      case "146"
         [highlight #FCE94F]objNetwork.AddWindowsPrinterConnection "\\Server\HP5si"[/highlight]
      case "147"
         objNetwork.AddWindowsPrinterConnection "\\Server\HP6si"
   end Select 
end If


Function GetIP()
   dim NIC1, Nic, StrIP, CompName
   Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
   For Each Nic in NIC1
      if Nic.IPEnabled then
         GetIP = Nic.IPAddress(i)
      end if
   next
End Function

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Yes Mark that helps a lot thank you... I do have 1 last question. I will attach 1 floor of printers, I have another 2 to add... I want to make it so that it will remove other floor printers if a PC is connect to a new floor. (Ex, there a PC on floor 1 that moves to floor 2... will remove floor 1 & 3 printers and add floor 2 printers.) this seems like it will be a bit cumbersome as a logon script. Do you have any recommendations how to make this run faster or a better solution to my issue? Thank you
Code:
set objNetwork = WScript.CreateObject("WScript.Network")
sIP = GetIP()
If sIP <> "" Then
	
   IPArray = Split(sIP,".")
   ThirdOctet = IPArray(2)

   'Map printer based on octet
   Select Case ThirdOctet
      case "146"
  	 objNetwork.AddWindowsPrinterConnection "\\NESVR-PRT01\FLR2_HP_CLR1"
	 objNetwork.AddWindowsPrinterConnection "\\NESVR-PRT01\FLR2_RICOH_1"
	 objNetwork.AddWindowsPrinterConnection "\\NESVR-PRT01\FLR2_RICOH_2"
	 objNetwork.AddWindowsPrinterConnection "\\NESVR-PRT01\FLR2_RICOH_3"
	 objNetwork.AddWindowsPrinterConnection "\\NESVR-PRT01\FLR2_RICOH_4"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR1_HP_CLR1"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR1_RICOH_1"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR1_TOSHIBA_1"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR3_HP_CLR3"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR3_RICOH_1"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR3_RICOH_2"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR3_RICOH_3"
      case "147"
         objNetwork.AddWindowsPrinterConnection "\\NESVR-PRT01\FLR2_HP_CLR1"
	 objNetwork.AddWindowsPrinterConnection "\\NESVR-PRT01\FLR2_RICOH_1"
	 objNetwork.AddWindowsPrinterConnection "\\NESVR-PRT01\FLR2_RICOH_2"
	 objNetwork.AddWindowsPrinterConnection "\\NESVR-PRT01\FLR2_RICOH_3"
	 objNetwork.AddWindowsPrinterConnection "\\NESVR-PRT01\FLR2_RICOH_4"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR1_HP_CLR1"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR1_RICOH_1"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR1_TOSHIBA_1"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR3_HP_CLR3"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR3_RICOH_1"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR3_RICOH_2"
	 objNetwork.RemoveWindowsPrinterConnection "\\NESVR-PRT01\FLR3_RICOH_3"
   end Select 
end If


Function GetIP()
   dim NIC1, Nic, StrIP, CompName
   Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
   For Each Nic in NIC1
      if Nic.IPEnabled then
         GetIP = Nic.IPAddress(i)
      end if
   next
End Function
 
Are your IPs assigned by floor? How can you identify where a computer is located? Is there a floor designation in the computer name or registry?

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top