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!

Login Script with Multiple NIC's

Status
Not open for further replies.

KevinBugg

Technical User
Feb 8, 2008
65
Hi,

We currently have 4 sites, all sepearted with different subnets. I am trying to create a single login script which defines which site the user is in by IP address then apply the relevant drive and printer mappings. At the moment it works well with pc's that have 1 network card. If a machine has 2 networks cards the script fails to run, even if the second card has no ip address. Can anyone see how I can tackle this problem? Here is my script.


On Error Resume Next
Dim objFSO,objFILE,objShell,objNetwork
strComputer = "."
set objFSO=CreateObject("Scripting.FileSystemObject")
set objShell=CreateObject("Wscript.Shell")
set objNetwork=CreateObject("Wscript.Network")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")
For Each objAdapter in colAdapters
For Each strAddress in objAdapter.IPAddress
arrOctets = Split(strAddress, ".")
If arrOctets(0) <> "" Then
strSubnet = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
x = 1
Exit For
End If
If x = 1 Then
Exit For
End If
Next
Next
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where DeviceID = 'Y:'")
If colItems.Count = 0 Then
Select Case strSubnet

'site 1
Case "192.168.0"

MapIt "I:","\\server-01\Upload"
AddPrinterConnection "\\server-01\Photocopier - Ricoh"

'site 2
Case "192.168.1"

MapIt "I:","\\server-01\Upload"
AddPrinterConnection "\\server-01\Photocopier - Ricoh"

'site 3
Case "192.168.2"

MapIt "I:","\\server-01\Upload"
AddPrinterConnection "\\server-01\Photocopier - Ricoh"

'site 4
Case "192.168.3"

MapIt "I:","\\server-01\Upload"
AddPrinterConnection "\\server-01\Photocopier - Ricoh"

End Select
End If

'End of main script

'//////////////////////////////////////////////////


Function IsAMemberOf(strDomain,strUser,strGroup)
On Error Resume Next
Set objUser=GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
Set objGrp=GetObject("WinNT://" & strDomain & "/" & strGroup & ",group")

If objGrp.IsMember(objUser.ADsPath) Then
IsAMemberOf=True
Else
IsAMemberOf=False
End If

End Function

Sub MapIt(strDrive,strMap)
On Error Resume Next
If objFSO.DriveExists(strDrive) Then objNetwork.RemoveNetworkDrive(strDrive)

objNetwork.MapNetworkDrive strDrive,strMap

If Err.Number<>0 And blnShowError Then
strMsg="There was a problem mapping drive " & UCase(strDrive) & " to " &_
strMap & VbCrLf & strHelpMsg & VbCrLf & "Error#:" & Hex(err.Number) &_
VbCrLf & Err.Description
objShell.Popup strMsg,iErrorTimeOut,"Error",vbOKOnly+vbExclamation
Err.Clear
End If

End Sub

Sub AddPrinterConnection(strPrinterUNC)
On Error Resume Next

objNetwork.AddWindowsPrinterConnection strPrinterUNC

If Err.Number<>0 And blnShowError Then
strMsg="There was a problem mapping " & UCase(strPrinterUNC) & ". " &_
vbcrlf & VbCrLf & strHelpMsg & VbCrLf & "Error#:" & Hex(err.Number) &_
VbCrLf & Err.Description

objShell.Popup strMsg,iErrorTimeOut,"Error",vbOKOnly+vbExclamation
Err.Clear
End If

end sub

Thanks
Kev
 
you not suggested where you script is failing? you have no logging so you cant tell where your script has failed.
i would start off by advising you to log to a log file the progress of your logonscript, that way you can debug issues. I would advise this to be a local log file as well as copying the log file to a central store, that way you can debug centrally, assess the speed o fyour logon script etc, identigy issues if your environment etc///

i can only guess, but this could cause a runtime therefore shoudl be addressed (it might be your exact issue...this time)

For Each strAddress in objAdapter.IPAddress
arrOctets = Split(strAddress, ".")
If arrOctets(0) <> "" Then
strSubnet = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)

you are not checking the UBound of your arrOctets before trying to access it


you would be safer using something like

For Each strAddress in objAdapter.IPAddress
If InStr(strAddress, ".") Then
arrOctets = Split(strAddress, ".")
objTS.WriteLine Now() & " UBound(arrOctets) = " & Cstr(UBound(arrOctets))
If UBound(arrOctets) = 3 Then
'do some other stuff?
Else
objTS.WriteLine Now() & " not enough ."
End If
Else
objTS.WriteLine Now() & " invalid strAddress " & CStr(strAddress)
End If
...

you may even need to consider IsNull() on that strAddress, but im not sure i havent tested
 
Thanks for your response, I agree I need to put some debugging in here, any tips?

As to my current problem, this script runs perfectly fine on a system that has 1 network card, if 2 network cards are present the script simply doesn't run. even if one of the cards is disabled.

Thanks
 
Hi Kevin,
you state that the script 'simply doesnt run' with two network cards. I do not believe this statement is true. You are suggesting that a logonscript will not start on a box if it has more than one network card, this is not true.

the simplest form of logging is:
Wscript.Echo "here there everywhere"
you can then see it on the screen.
if you logon script is defined as 'cscript.exe mylogonscript.vbs > %temp%\logonscript.log', or a batch file which calls the same thing, you may find that it writes the wscript.echos to %temp%\logonscript.log.
if you dont like the wscript.echos, then you want to open a text file for appending and start writing to it with
objTS.WriteLine Now() & vbTab & "my info...etc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top