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

Login scripts - mapping drives and printers depending on IP address 2

Status
Not open for further replies.

Tightpants

Technical User
Jan 22, 2004
238
GB
This is really directed to markdmac with reference to the FAQ faq329-5798.

In the same way you can map certain drives and printers using by detecting groups the user belongs to, I need to do the same depending on IP address.

eg. if the IP address is 192.168.1.x then map N drive to \\Server1\Data or if the IP address is 192.168.2.x then map N drive to \\Server2\Data.

I can then use the same VBS logon script at different sites without having to do anything special to the GPO defining the logon script for the users.
 
Tightpants,

You can do this however it is not very neat. The reason is you really have no way of knowing if the machine will have multiple network cards and therefore can't just read the IP address very easily. Do you have users or computers that travel between sites?

I'm thinking your best way to do this is to move those users from the remote location into their own OU and apply their own script at that OU.

Another idea would be to add a custom registry key that you could quickly look for.

If you know for sure that the PC uses DHCP then you could use this code that I wrote to find the DHCP address:

Code:
'==========================================================================
'
' NAME: GetDHCPIPaddress.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 5/4/2005
'
' COMMENT: Determines current DHCP IP Address of computer
'          Assumes that there is only ONE active NIC.  
'==========================================================================
On Error Resume Next

Const HKEY_LOCAL_MACHINE = &H80000002
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
".\root\default:StdRegProv")

Set WSHShell = CreateObject("Wscript.Shell")
strKeyPath = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

For Each subkey In arrSubKeys
	'WScript.Echo strKeyPath & subkey & "\DhcpIPAddress"
	DHCPAddress = WSHShell.RegRead("HKLM\" & strKeyPath & subkey & "\DhcpIPAddress")
	If DHCPAddress <> "0.0.0.0" And Left(DHCPAddress,3) <> "169" Then
		ActiveDHCPIPAddress = DHCPAddress	
	End If
Next
WScript.Echo "DHCP Address:" & ActiveDHCPIPAddress

Once you have the ActiveDHCPIPAddress value you could check the value of the third octet and do whatever you wanted in the script.

I hope you find this post helpful.

Regards,

Mark
 
This is the way I have done it for multiple sites and subnets.

Code:
' Create WMI object
Set objWMI = GetObject("WinMGMTS:root\cimv2")

' Get active IP address of user
strQuery = "SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'"
Set objNICCol =  objWMI.ExecQuery(strQuery)

For Each objAdapter IN objNICCol
	For Each strIP In objAdapter.IPAddress
		Select Case Left(strIP, InStrRev(strIP, ".", -1, 1))
			Case "192.168.1."	' Site A
				MapSiteA()
			Case "192.168.2."	' Site B
				MapSiteB()
			Case "192.168.3."	' Site C
				MapSiteC()
			Case "192.168.4."	' Site D
				MapSiteD()	
			Case Else
				' Do nothing
		End Select
	Next
Next



Paul

Work on Windows, play on Linux.
 
Excellent solution penauroth.

Like my code yours too has some limitations however. If a person has more than one NIC you can't guarantee results. For instance, if a laptop user has both an internal card and a PCMCIA or USB card. It is possible for them to travel from site to site with one card having a static address and the other DHCP. In this case the last detected NIC will win on the settings war. I'm sure this will work for 99% of situations however which may be acceptable.

I only raise this point because we get so many postings that start with "I have a problem with just ONE machine in my domain..."

I hope you find this post helpful.

Regards,

Mark
 
I was going to actually say that my code would need a few tweaks if you have special case systems, such as dual NICS. Just providing a solution based on the requirements.

Accordingly, the code can be modified to obtain the date time stamp of the IP lease - I think. Based on that info, you can easily select the appropriate IP address to map drives and connect printers.

Paul

Work on Windows, play on Linux.
 
I agree, but again we have the "what if" situation for static IPs too.

As I said before, I think yours is an excellent solution that will work 99% of the time.

Sounds like we have both thought hard about this one before. Shame there really isn't a 100% solution that either of us can think of.

I hope you find this post helpful.

Regards,

Mark
 
Thanks for your excellent suggestions. Will you update your FAQ markdmac?

The reason for the original question is that I have users who can logon at one of two sites. At the moment I have configured a custom environment variable on each machine called SITEID. This then calls the logon script %siteid%.bat defined in AD Users and Computers. It works surprisingly well however it doesnt work for notebook computers moving between sites and I also have to remember to change the variable if a desktop computer moves sites.

Ultimately I want to change my logon script to logon.vbs and configure it through GPO. I can't use the SITEID variable to do this. Similarly I can't put users into different OUs because they may logon at either site and call up the wrong script.

Maybe the IP address is not the best means of asking "where am I?" However your posts will work for me. Thank you.
 
Not sure if this will make your life any easier, but here si a script I wrote to allow laptop users to save and switch IP configurations. This would allow you to work with static addresses and not have the user have to know anything about network configurations.

I'm afraid this topic won't make it into my FAQ. As it won't work 100% I am not comfortable with adding it as an option until I find a better way of doing it.

This script will work on desktops too if you want to give it a try. It is rather cool I think.

Code:
'==========================================================================
'
' NAME: IPSwitcher.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 3/23/2004
'
' COMMENT: <comment>
'
'==========================================================================


Dim fso,WSHShell
Dim fso,WSHShell
 
 
 
Set fso = CreateObject("Scripting.FileSystemObject")
Set WSHSHell = CreateObject("Wscript.Shell")
Path = "C:\IPCONFIGS"
 
'Create Storage location if not already there
If Not fso.FolderExists(Path) Then
 fso.CreateFolder(Path)
End If
 
'Prompt to activate, create or delete a config
Action = InputBox("Do you want to ACTIVATE, SAVE or DELETE an IP Configuration","What should I do?")
Action = UCase(Action)
 
Call ConfigList
 
Select Case Action
 
Case "ACTIVATE"
     If Len(ConfigList) >0 Then
  What = InputBox("Existing configurations are:" & vbCrLf & vbCrLf & ConfigList & vbCrLf & "Enter config name to activate.","Activate Which Configuration")
 FileName = Path & "\" & What & ".txt"
 WSHShell.Run ("cmd /c netsh -f " & FileName)
 Else
  MsgBox "Sorry there are no configurations to activate"
 End If
 
Case "SAVE"
 If Len(ConfigList) >0 Then
  What = InputBox("Existing configurations are:" & vbCrLf & vbCrLf & ConfigList & vbCrLf & "Enter new config name to save, enter existing name to overwrite.","Save Configuration")
 Else
  What = InputBox("Enter new config name to save.","Save Configuration")
 End If 
 FileName = Path & "\" & What & ".txt"
 'MsgBox Filename
 WSHShell.Run ("cmd /c netsh -c interface dump >" & FileName)
 
Case "DELETE"
 If Len(ConfigList) >0 Then
  What = InputBox("Existing configurations are:" & vbCrLf & vbCrLf & ConfigList & vbCrLf & "Enter config name to delete.","Delete Configuration")
  FileName = Path & "\" & What & ".txt"
  fso.DeleteFile(FileName)
 Else
  MsgBox "No configurations saved to delete."
 End If
 
End Select
MsgBox "Done"
 
Function ConfigList
'Read the existing configs
Set oFolder = fso.GetFolder(Path)
 
ThisList = ""  
For Each oFile In oFolder.files
 ThisConfigLength = (Len(oFile.Name)-4)
 ThisConfig = Left(oFile.Name,ThisConfigLength)
 ThisList = ThisList & ThisConfig & vbCrLf
Next
ConfigList = UCase(ThisList)
 
End Function

I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top