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

Can someone help me with the following? 5

Status
Not open for further replies.

Chambers

IS-IT--Management
Jan 19, 2001
257
0
0
US
Hi guys, I need help with a script to do the following;


If "IP=Whatever I Pick"
Then
con2prt /cd \\servername\printer
Else
con2prt /cd \\servername\otherprinter
End

con2prt.exe resides in the system32 directory and is made by MS. Basically the script would do the following. If the IP is this, then map this printer and make it default..else, map this other printer and make it default.

I only know basic logic, but that is what I would like a batch file to do. Can someone help me out? Thanks!
 
Hello Chambers,

Try this (partly pseudo).
[tt]
If "IP=Whatever I Pick" Then
createobject("wscript.shell").run "con2prt /cd \\servername\printer"
Else
createobject("wscript.shell").run "con2prt /cd \\servername\otherprinter"
End
[/tt]
regards - tsuji
 
Thanks for the reply, but I get an error on line 1

If IP=10.10.1.133 Then
createobject("wscript.shell").run "con2prt /cd \\shore1\LO"
Else
createobject("wscript.shell").run "con2prt /cd \\shore1\HuntingtonWoods"
End IF

and also tried

If "IP=10.10.1.133" Then
createobject("wscript.shell").run "con2prt /cd \\shore1\LO"
Else
createobject("wscript.shell").run "con2prt /cd \\shore1\HuntingtonWoods"
End IF

Type mismatch string IP=10.10.1.133

Basically I need this script to say if my IP is this, then map/set default this printer...else map/set default this other printer. con2prt takes care of the mapping/default printer portion of it. I just need it to distinguish based on my ip address. Thank you
 
where is the code to retrieve the ipaddress???

i use winsock

so youwould need to start your script with

Set WshSock = WScript.CreateObject("MSWinsock.Winsock")
IP = WshSock.LocalIP
Set WshSock = Nothing

then you can do

If IP = "10.10.1.133" Then
 
Ok, I got it somewhat working, here's my code;

If IP-Address="10.10.1.133" Then
' createobject("wscript.shell").run "con2prt /F"
createobject("wscript.shell").run "con2prt /cd \\shore1\LO"
Else
' createobject("wscript.shell").run "con2prt /F"
createobject("wscript.shell").run "con2prt /cd \\shore1\HuntingtonWoods"
End IF

However, my IP is set at 10.10.1.133, so it should pay the LO printer, however it doesn't and skips to HuntingtonWoods. What could be wrong? Maybe it's seeing my ip as 10.10.1.1?

Please help, thanks!!
 
Why do you have a minus sign in your variable name? VBScript will try to subtract the 2 values, which don't exist, and automatically perform the Else part.

Lee
 
Tried with the winsock, getting error cannot creat MSWinsock.Winsock
 
trollacious, I looked online and saw that is the way to get the IP address..with the syntax IP-Address I took the - out of it and then tried running my code, got an error saying it's wanting a ELSE
 
how are you generating your IPaddress value then?

trollacious is right about the "-" sign, not a recommended naming convention for variables

what happens if you put a

Msgbox IP

in your code, that way you can view the string representation of variable IP, after all this is what it will use when you do the comparison = "10.0.....
 
Ok, tried with just IPAddress = "10.10.1.133" Then

It ran fine with no errors but it still mapped the HuntingtonWoods printer even though my IP is 10.10.1.133
 
tried Msgbox IP

it's blank! well that could be a problem :)
but I have an ip...
 
Try this:
Code:
Option Explicit
Dim strComputer
Dim objWMIService
Dim i
Dim objAdapter
Dim IPAddress
Dim colAdapters

strComputer = "."
Set objWMIService = GetObject _
("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objAdapter in colAdapters
	If Not IsNull(objAdapter.IPAddress) Then
		For i = LBound(objAdapter.IPAddress) To UBound(objAdapter.IPAddress)
			IPAddress = objAdapter.IPAddress(i)
		Next
	End If
Next

' WScript.Echo IPAddress

If IPAddress="10.10.1.133" Then
    createobject("wscript.shell").run "con2prt /cd \\shore1\LO"
Else
    CreateObject("wscript.shell").run "con2prt /cd \\shore1\HuntingtonWoods"
End If

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
chambers,

when you write in code

IP

how do you think vbscript knows about the ipaddress???

where is your code which assigns the variable 'IP' a value

i.e. in its simplest form

IP = "1.1.1.1"
If IP = "2.2.2.2" Then
Msgbox "match"
Else
Msgbox "no match"
End If

you need to give IP a value somehow, if winsock doesnt work how about this code

strComputer = "."
Set objWMIService = GetObject _
("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objAdapter in colAdapters
If Not IsNull(objAdapter.IPAddress) Then
For i = LBound(objAdapter.IPAddress) To UBound(objAdapter.IPAddress)
IP = objAdapter.IPAddress(i)
Next
End If
Next

Msgbox IP
 
Tom..it worked!! Thank you so much!!!!!!
 
Notice that it is the same as what mrmovie was trying to get you to do. You should also be aware that if you do this on a machine with multiple NICs you may not get the result that you expect.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
mrmovie: i thought that vbscript would already know that IP..just thought it was built into it. I'm sorry for my ignorance :(

But I also want to thank each and everyone one of you for taking the time to help me out with this, I know how frustrating it can be to help someone that doesn't know anything. Thanks you again everyone!!!
 
Thanks for the star. I don't think I deserved it in this instance though. I just swooped in at the end with a snippet of code. The people that were trying to help you understand the fundamentals really deserve stars. I will give them some. :)


[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Eeek.... I just found out that it won't work with IP address, but it WILL work with client name.

Do any of you know to make the script get the client name? Basically the %clientname% from a batch file?
 
Look at the ExpandEnvironmentStrings method of the WshShell method.

Here is an example of its use:
set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "WinDir is " & WshShell.ExpandEnvironmentStrings("%WinDir%")

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Thanks again Tom! I actually was googling around an managed to use the following and all seems to work :D

Set Sh = CreateObject("WScript.Shell")
sys = Sh.ExpandEnvironmentStrings("%CLIENTNAME%")

' WScript.Echo IPAddress

If sys="dduck" Then
createobject("wscript.shell").run "con2prt /F"
createobject("wscript.shell").run "con2prt /cd \\shore1\Imaging"
Else
createobject("wscript.shell").run "con2prt /F"
CreateObject("wscript.shell").run "con2prt /cd \\shore1\HuntingtonWoods"
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top