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!

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.
 
You can identify where a computer is by the IP address. Each floor has 2 subnets designated to it differentiated by the 3rd octet.

10.231.144 & 145 = 1st floor
10.231.146 & 147 = 2nd floor
10.231.148 & 149 = 3rd Floor
 
You can consolidate by specifying the multiple subnets per floor.
Code:
case "146","147"

If you move a computer and the IP changes, the login script should add the printers in as needed.

You may need to clear the existing printers before adding them to ensure things stay clean, or you can make it part of your move process.


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.
 
So this is the most ideal code for this situation? This seems like it would clear the existing printers before adding the new ones, correct?

Code:
case "146","147"
	 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"
  	 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"
 
Yes that will work or you can remove all current network printers without namign them like this:
Code:
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'To remove only networked printers use this If Statement
	If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
	  WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
	End If
'To remove all printers incuding LOCAL printers use this statement and comment out the If Statement above
'WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
Next


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.
 
Your way will probably be a more efficient way to remove all the networked printers... I'm just confused where to put that in the following code. I've tried a variety of different options.. can't seem to figure it out :
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","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"
   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
 
Put it under here:
set objNetwork = WScript.CreateObject("WScript.Network")

Please note I copied that from my login script FAQ. Change WSHNetwork to match your code which is using objNetwork.

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.
 
Can you not use gpmc use item level targeting and select Ip address range

MCSE NT to 2012, MCITP:EA/SA, MCSA, MCDBA, MCTS, MCP+I, MCP
 
Well, hopefully you learned something about scripting even if you won't use it.

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