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

Map printers based on workstation IP addresses.

Status
Not open for further replies.
Mar 3, 2011
12
US
I am looking for a VB Script that would map network printers to my laptop users IP addresses. Printers are located in 5 sites, each on a different subnet. Laptop user travel to each location a few times per week. I would like to use the Third octet in the IP address to determine which set of printers get mapped to the user's machine. I searched the UDFs and didn't see anything specific to my issue. 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 exsisting network printers prior to mapping the new ones.
Thanks in advance.
 
obtain the IP address and parse it into 4 tokens. Use a select case on the 3rd token to determine which printer to install. There are many ways to obtain the local IP address. The example below uses one.

Code:
'execute an ipconfig to the local machine and store the ouput in the stdout text stream
set objExec = objShell.Exec("%comspec% /c ipconfig.exe")

'loop through the stream looking for key phrase "IPv4 Address"
do while NOT (objExec.AtEndOfStream)
   strLine = objExec.ReadLine
   if(inStr(strLine, "IPv4 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 arrTokens(2)  '3rd token/octet
   case "10"
      objNetwork.AddWindowsPrinterConnection "\\server\ny_printer"
   case "30"
      objNetwork.AddWindowsPrinterConnection "\\server\wa_printer"
      objNetwork.AddWindowsPrinterConnection "\\server\wa_printer2"
   case "80"
      objNetwork.AddWindowsPrinterConnection "\\server\az_printer"
end select

Untested..

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
oops. you'll also need the network object to use the .AddWindowsPrinterConnection funtion.

Put this at the top of the code
set objNetwork = WScript.CreateObject("WScript.Network")

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
I have an FAQ that demos taking action based on the IP. It is designed to work within my login script.


I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thank you guys. I will try the above code and post my results.
 
Sorry it took this long to get back. Geates, I ran the code below and recieved this error message:
Line 4
Char: 1
Error: object required: objshell
Code: 800A01A8

Can you tell me what I need to do to resolve this.

Code:
__________________________________________________________________
'execute an ipconfig to the local machine and store the ouput in the stdout text stream

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

'loop through the stream looking for key phrase "IPv4 Address"
do while NOT (objExec.AtEndOfStream)
strLine = objExec.ReadLine
if(inStr(strLine, "IPv4 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 arrTokens(2) '3rd token/octet
'Main
case "10.87.251.16""
objNetwork.AddWindowsPrinterConnection "\\mainsvr\maccounting2"
'West
case "10.85.213.83"
objNetwork.AddWindowsPrinterConnection "\\Westsvr\waccounting2"

'East
case "10.83.85.20"
objNetwork.AddWindowsPrinterConnection "\\Eastsvr\eaccounting2"

'North
case "10.88.138.20"
objNetwork.AddWindowsPrinterConnection "\\Northsvr\naccounting2"

'South
case "10.88.141.20"
objNetwork.AddWindowsPrinterConnection "\\Southsvr\saccounting2"


end select
 
Add the following to the top of that code

Set objShell = CreateObject("Wscript.Shell")

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
oops again.

what markdmac said :)

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
I tried again and recieved the following error:

Line 8
Char: 1
Error: object doesn't support ths property or method:'objExec.AtEndOfStream'
Code: 800A01B6

Please help !

Code:
__________________________________________________________________
'execute an ipconfig to the local machine and store the ouput in the stdout text stream

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 "IPv4 Address"
do while NOT (objExec.AtEndOfStream)
strLine = objExec.ReadLine
if(inStr(strLine, "IPv4 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 arrTokens(2) '3rd token/octet
'Main
case "10.87.251.16""
objNetwork.AddWindowsPrinterConnection "\\mainsvr\maccounting2"
'West
case "10.85.213.83"
objNetwork.AddWindowsPrinterConnection "\\Westsvr\waccounting2"

'East
case "10.83.85.20"
objNetwork.AddWindowsPrinterConnection "\\Eastsvr\eaccounting2"

'North
case "10.88.138.20"
objNetwork.AddWindowsPrinterConnection "\\Northsvr\naccounting2"

'South
case "10.88.141.20"
objNetwork.AddWindowsPrinterConnection "\\Southsvr\saccounting2"


end select
 
Like I said, I hadn't tested it.

Code:
do while NOT (objExec[red].Stdout.[/red]AtEndOfStream)

same with .readline

Code:
strLine = objExec[red].Stdout.[/red]ReadLine

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
I thought that was it, I saw a cmd screen for a sec but still got the following error message:

Line 20
Char: 1
Error: Subscript out of range: '[number: 2]'
Code: 800A00009

What should I do next ?


Code:
__________________________________________________________________
'execute an ipconfig to the local machine and store the ouput in the stdout text stream

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 "IPv4 Address"
do while NOT (objExec.Stdout.AtEndOfStream)

strLine = objExec.Stdout.ReadLine
if(inStr(strLine, "IPv4 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 arrTokens(2) '3rd token/octet
'Main
case "10.87.251.16""
objNetwork.AddWindowsPrinterConnection "\\mainsvr\maccounting2"
'West
case "10.85.213.83"
objNetwork.AddWindowsPrinterConnection "\\Westsvr\waccounting2"

'East
case "10.83.85.20"
objNetwork.AddWindowsPrinterConnection "\\Eastsvr\eaccounting2"

'North
case "10.88.138.20"
objNetwork.AddWindowsPrinterConnection "\\Northsvr\naccounting2"

'South
case "10.88.141.20"
objNetwork.AddWindowsPrinterConnection "\\Southsvr\saccounting2"


end select
 
Since your case statement lists the entire IP, not just one of the octets, you don't need the array. Also the key phrase is "IP Address", not "IPv4 Address".
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 [b]"IP Address"[/b]
do while NOT (objExec.Stdout.AtEndOfStream)

   strLine = objExec.Stdout.ReadLine
   if(inStr(strLine, [b]"IP Address"[/b])) then
      'Parse strLine to obtain IP.
      strIP = trim(right(strLine, len(strLine) - inStrRev(strLine, ":")))
      exit do
   end if
loop
[b][s]arrTokens = split(strIP, ".")[/s]
select case strIP [s]arrTokens(2)  '3rd token/octet[/s][/b]
'Main
   case "10.87.251.16"
      objNetwork.AddWindowsPrinterConnection "\\mainsvr\maccounting2"
'West
   case "10.85.213.83"
      objNetwork.AddWindowsPrinterConnection "\\Westsvr\waccounting2"
      
'East
   case "10.83.85.20"
      objNetwork.AddWindowsPrinterConnection "\\Eastsvr\eaccounting2"

'North
   case "10.88.138.20"
      objNetwork.AddWindowsPrinterConnection "\\Northsvr\naccounting2"

'South
   case "10.88.141.20"
      objNetwork.AddWindowsPrinterConnection "\\Southsvr\saccounting2"
   

end select


 
Willie, did you take a look at my FAQ? That code is fully tested.

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Your initial question indicated that you wanted to install printers based on the third octect of the IP Address. The code splits the ip address into 4 pieces allowing you to use a select...case against the 3rd octect. Do as guitarzan suggests or modify you cases to use the 3rd octect

Code:
arrTokens = split(strIP, ".")
select case strIP arrTokens(2)  [b][red]'3rd token/octet[/red][/b]
   'Main
   case "251" '10.87.251.16
      objNetwork.AddWindowsPrinterConnection "\\mainsvr\maccounting2"
   'West
   case "213" '10.85.213.83
      objNetwork.AddWindowsPrinterConnection "\\Westsvr\waccounting2"
   'East
   case "85" '10.83.85.20
      objNetwork.AddWindowsPrinterConnection "\\Eastsvr\eaccounting2"
   'North
   case "138" '10.88.138.20
      objNetwork.AddWindowsPrinterConnection "\\Northsvr\naccounting2"
   'South
   case "141" '10.88.141.20
      objNetwork.AddWindowsPrinterConnection "\\Southsvr\saccounting2"
end select

'Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Thank you guys for all your help, it is much appriciated. I made the above changes and the code finally ran with no errors. I have done some modifications to to the script to also map a few shared folders. Can you help me with a code that I can place at the begining of the script that will remove all old shares EXCEPT for the "Z:drive" (which is the user's home folder).
 
As far as I know, removing network drive is something that should to be done on the user level.

Enumerate network drives and remove all entries that != Z:.

Code:
set objNetwork = CreateObject("WScript.Network") 
set colShares = objNetwork.EnumNetworkDrives() 

for i = 0 To colShares.Count - 1 step 2 
   if (colShares.item(i) <> "Z:") then
      objNetwork.RemoveNetworkDrive colShares.item(i)
   end if
next

This code should remove all network drives. If not, it can be accomplished by enumerating the HKCU\Network key and using objNetwork.RemoveNetworkDrive.

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Geates, my goal is to ensure that the shared drives mapped at one site do not follow the machine to anoher. Laptop users will need to be mapped to their home folder (Z:Drive) regardless of the site they are visiting. If there is another way I can achive this goal, please let me know. I would really appreciate the input.
 
How are the drives being mapped in the first place? If it is being done with "NET USE X: \\computer\share", you can use the /PERSISTENT:NO switch, which will not restore the connection on logoff/logon.

Have you tried Geates code?
 
I just remembered that I had to do this another way because .RemoveNetworkDrive only disconnects the drive but the mapping still exists - thus, new mappings with the same drive letters can't be created. In that case, you'll need to remove them from the users hive.

Open regedit.exe and drill down to HKEY_CURRENT_USER\Network. This is the key where mapped drives are stored (on a user basis). Each mapping exists as it's own key with values that define the mapping. The script will need to delete these key entries to delete a mapping.

The drives are stored in the HKEY_CURRENT_USER\Network key which is not accessible thru conventional means (objShell) unless you know the users SID (HKEY_USERS + SID = HKEY_CURRENT_USER). Therefore, you need to use the standard registry provider (objReg) to access and remove the reg key.

1. Connent to the Standard Registry Provider
2. Enumberate Drive mappings
3. Delete all drives that <> Z:

Code:
CONST HKCU = &H80000001
strComputer = "." 'Local Computer
set objNetwork = CreateObject("WScript.Network") 'Create Network object

'1. Connent to the Standard Registry Provider
set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

'2. Enumberate Drive mappings
set colShares = objNetwork.EnumNetworkDrives()

'3. Delete all drives that <> Z:
for i = 0 To colShares.Count - 1 step 2
   if (colShares.item(i) <> "Z:") then
      'Because the item value is letter-colon, we take the first left character of the item
      objReg.DeleteKey HKCU, "Nework\" & left(colShares.item(i), 1)
   end if
next

Great registry managment code examples


-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Thank you all for helping with this script. I finally got it run out errors.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top