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

Default printer by Subnet 1

Status
Not open for further replies.

ckugo

IS-IT--Management
Jan 6, 2004
165
US
Is it possible to make a user get a default printer determined by their subnet? Here is the default printer script that I am using now:

Dim objPrinter
Set objPrinter = CreateObject("WScript.Network")
objPrinter.SetDefaultPrinter "PrinterName"

I have users that move frequently and I would like thier default printer to change at login determined by their subnet (assuming all networking and vpns are setup properly and functioning).
i.e.

Location 1 - 192.168.100.0
default printer - Printer1

Location 2 - 192.168.200.0
default printer - Printer2

All the help and time that anyone spends or thinks about this is greatly appreciated.

Thanks,

Chris
 
A starting point to get the IP address(es)
Set myWMI = GetObject("winmgmts:\\.\root\cimv2")
Set myobj = myWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each itm in myobj
MsgBox itm.Description & ": " & itm.IPAddress(0)
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PH,

Thanks so much for the quick reply, and you are exactly right, that does give me the ip address. However, what if that changes?? It doesn't really matter what their ip is, I need to know the subnet 192.168.100.0, where the 100 is what needs to determine which printer they get as a default. Is there any way to disregard the last octet and check by the third octet??

Thanks again,

Chris
 
Doesn't the IP address belongs to a subnet ?
What about testing this value :
Split(itm.IPAddress(0),".")(2)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hey, that pulled that right info. Wow, great job and thanks so much for making my life and I am sure a lot of other peoples a lot easier. Have a star!

Thanks so much,

Chris
 
Sorry, but I have one more question:

How can I store the output of the Split command into a varible?

Thanks,

Chris
 
Say you have itm.IPAddress(0)="192.168.100.11", then
arrIP = Split(itm.IPAddress(0),".")
will result to:
arrIP(0)="192"
arrIP(1)="168"
arrIP(2)="100"
arrIP(3)="11"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
This is what I am trying and it keeps giving me an error:

Set myWMI = GetObject("winmgmts:\\.\root\cimv2")
Set myobj = myWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
arrIP = Split(itm.IPAddress(0),".")

if arrIP(2) = 11 then
msgbox "This works!!"
end if


Please tell me what I am doing wrong.


Thanks,

Chris
 
it keeps giving me an error
Please when you post this sort of sentence ALWAYS post the whole error message.
Seems you missed the For Each itm loop ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Sorry about the ignorance, I just wasn't thinking. I got it to work. Thanks so much again for your help and patience.

Thanks,

Chris
 
SO would you share the script that worked? <g>

Marty
Network Admin
Hilliard Schools
 
Here is the basic setup:

Set myWMI = GetObject("winmgmts:\\.\root\cimv2")
Set myobj = myWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each itm in myobj
arrIP = Split(itm.IPAddress(0),".")
Next

Now, like PHV described the ip subnets are split and can be referred to as (0),(1),etc.

So like in the above example you could do:

Set myWMI = GetObject("winmgmts:\\.\root\cimv2")
Set myobj = myWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each itm in myobj
arrIP = Split(itm.IPAddress(0),".")
Next

if arrIP(2) = "200" then
Set objPrinter = CreateObject("WScript.Network")
objPrinter.SetDefaultPrinter "PrinterName"
else
if arrIP(2) = "100" then
Set objPrinter = CreateObject("WScript.Network")
objPrinter.SetDefaultPrinter "PrinterName1"

and so on.

Hope that is helpful to you. PHV deserves all the credit for this one. Thanks for all your help.

Chris

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top