Link to home
Start Free TrialLog in
Avatar of miket71
miket71Flag for United States of America

asked on

Need to map drives based on subnet mask and IP

Hello,

We have two locations sharing one subnet, and two other locations sharing another subnet.  We are in need of a batch script that will allow us to map to specific server shares based on the IP detected by the script.

Thanks,
Mike
We are using the following shared subnet script, but it only works for one subnet, not two.  In other words, we cannot get this script to run two instances in the same batch file.  It only runs the first instance, and bombs out at the second instance.  Here is the script:
 
rem ***Login Script for Location1 and Location2***
@Echo off
set subnettocheck=10.60.26
ipconfig | find "%subnettocheck%" | find "IP Address" > %temp%\Ipaddr.txt
for /f "tokens=*" %%a in (%temp%\ipaddr.txt) do set ipaddr=%%a & goto :next
:next
 
Rem Get end of string.  replace . . . with nothing
set ipaddr=%ipaddr:. =%
 
REM Remove IP Address:
set ipaddr=%ipaddr:IP Address:=%
 
REM Remove any spaces to leave IP address x.x.x.x
set ipaddr=%ipaddr: =%
set lastoctet=%ipaddr:~-3,3%
set subnet=%ipaddr:~0,-4%
if "%lastoctet:~0,1%"=="." set lastoctet=%lastoctet:~1% & set subnet=%ipaddr:~0,-3%
if "%lastoctet:~1,1%"=="." set lastoctet=%lastoctet:~2% & set subnet=%ipaddr:~0,-2%
 
echo Your IP address is %ipaddr%
echo Your subnet is %subnet%
echo Last octet is %lastoctet%
 
if %lastoctet% LSS 63 echo Less than 63 & goto subnet63
if %lastoctet% LSS 127 echo Less than 127 & goto subnet127
 
:subnet63
rem ***Location1***
net use l: \\server1\APPS
net use m: \\server1\DATA
net use S: \\main\data
net use t: \\main\apps
goto next
 
:subnet127
rem ***Location2***
net use S: \\server2\data
goto next
 
:next

Open in new window

Avatar of Andrew Porter
Andrew Porter
Flag of United States of America image

Are they on different DC's? If so you are going to run into the issue of determining what user network share is on which DC...Say user A travels to site B but his user share is on the DC @ site A. It will find user A easily enough but will attempt to map his share based on IP, not location.

If not you can use a vbs to do this a lot easier than a batch file.


I would also recommend shifting to VbScript.

At which point, I would ask if you have Sites configured for each subnet (AD Sites and Services). Because if you do, you can use the script to check the site name as below.

By IP works of course, but names are just so much prettier ;)

Chris
Set objADSysInfo = CreateObject("ADSystemInfo")
 
Select Case objADSysInfo.SiteName
  Case "London"
    ' Do things for London
  Case "New York"
    ' Do things for New York
End Select

Open in new window

Avatar of miket71

ASKER

andeporter,

The shares are on different servers whether they are DCs or not.  We have at least one DC at each location.  I apologize for not being clear enough in my post.  We would want users to map to the server where they are logging in at, and not to a server where their home location is.

Chris-Dent,

We do use Sites and Services actually, so the VBS way is an option.  I have little or no experience with implementing VBS scripts though.  Based on the clarification to andeporter, what has to be done to setup the VBS option?  Would it be something like this in a VBS file?:

Set objADSysInfo = CreateObject("ADSystemInfo")
 
Select Case objADSysInfo.SiteName
  Case "Location1"
    ' net use l: \\server1\APPS
    ' net use m: \\server1\DATA
    ' net use S: \\main\data
    ' net use t: \\main\apps
  Case "Location2"
    ' net use S: \\server2\data
End Select

If you wanted to stick with Net Use it would be the first of the snippets below.

However, VbScript has WScript.Network which can also map drives for you. That's the second example.

Chris
' Example 1: Net Use
 
Set objADSysInfo = CreateObject("ADSystemInfo")
 
Set objShell = CreateObject("WScript.Shell")
 
Select Case objADSysInfo.SiteName
  Case "Location1"
    objShell.Run "net use l: \\server1\APPS"
    objShell.Run "net use m: \\server1\DATA"
    objShell.Run "net use S: \\main\data"
    objShell.Run "net use t: \\main\apps"
  Case "Location2"
    objShell.Run "net use S: \\server2\data"
End Select
 
' Example 2: WScript.Network
 
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("WScript.Network")
 
' Ignore error messages (will error if the drive is already mapped)
On Error Resume Next
 
Select Case objADSysInfo.SiteName
  Case "Location1"
    objNetwork.MapNetworkDrive "l:", "\\server1\APPS"
    objNetwork.MapNetworkDrive "m:", "\\server1\DATA"
    objNetwork.MapNetworkDrive "s:", "\\main\data"
    objNetwork.MapNetworkDrive "t:", "\\main\apps"
  Case "Location2"
    objNetwork.MapNetworkDrive "s:", "\\server2\data"
End Select

Open in new window

Avatar of miket71

ASKER

Okay, I put both in a VBS file after updating the site names, servernames, sharenames, etc, and tried running it manually, but no mappings.  Am I missing something else?  Or will the VBS file only work on a DC from the netlogon directory?  We will end up using it as a group policy script with a GPO.

It will work from anywhere, either it died, but we didn't see because of "On Error Resume Next" or it failed on the site name match.

Lets go with the site name match first. It's case sensitive, it must match exactly to what you have in AD Sites and Services. That is london isn't the same as London.

Delete the "On Error Resume Next" line while we're testing as well, it crying at us if far better than it say nothing at all.

Chris
Avatar of miket71

ASKER

Here's what I have now.  I checked the case of the site names.  I'm test running it from the root of C on a workstation and from the root of C on a DC which would fall under the "Main" site:

Set objADSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("WScript.Network")
 
' Ignore error messages (will error if the drive is already mapped)

 Select Case objADSysInfo.SiteName
  Case "Main"
    objNetwork.MapNetworkDrive "l:", "\\main\APPS"
    objNetwork.MapNetworkDrive "m:", "\\main\DATA"
    objNetwork.MapNetworkDrive "s:", "\\main\data"
    objNetwork.MapNetworkDrive "t:", "\\main\apps"
  Case "Delray"
    objNetwork.MapNetworkDrive "s:", "\\delray\data"
End Select

Well, we can do one more thing :)

Add:

WScript.Echo objADSysInfo.SiteName

That will tell us what it's seeing and may tell us why it's not doing anything.

Chris
Avatar of miket71

ASKER

Okay, I added the WScript.Echo objADSysInfo.SiteName to where I thought would work as seen below, and ran it again.  Now I get a window popup on the screen that says "Main" with no mappings which I forgot to mention in the previous post.  So this proves at least that the machine is seeing the site.

Set objADSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("WScript.Network")
 
' Ignore error messages (will error if the drive is already mapped)

 WScript.Echo objADSysInfo.SiteName  <--Added here if this is okay
 Select Case objADSysInfo.SiteName
  Case "Main"
    objNetwork.MapNetworkDrive "l:", "\\main\APPS"
    objNetwork.MapNetworkDrive "m:", "\\main\DATA"
    objNetwork.MapNetworkDrive "s:", "\\main\data"
    objNetwork.MapNetworkDrive "t:", "\\main\apps"
  Case "Delray"
    objNetwork.MapNetworkDrive "s:", "\\delray\data"
End Select

That's fine, pretty much ideal place to add it in.

Lets see if it's going into the Select statement then, everything you have there looks fine.

Chris
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("WScript.Network")
 
' Ignore error messages (will error if the drive is already mapped)
 
 Select Case objADSysInfo.SiteName
  Case "Main"
 
    WScript.Echo "Mapping drives for Main"
 
    objNetwork.MapNetworkDrive "l:", "\\main\APPS"
    objNetwork.MapNetworkDrive "m:", "\\main\DATA"
    objNetwork.MapNetworkDrive "s:", "\\main\data"
    objNetwork.MapNetworkDrive "t:", "\\main\apps"
 
  Case "Delray"
 
    WScript.Echo "Mapping drives for Delray"
 
    objNetwork.MapNetworkDrive "s:", "\\delray\data"
End Select

Open in new window

Avatar of miket71

ASKER

Okay, I pasted your last post to a vbs file and ran it but nothing happens this time.  Is there a way that I can run it in verbose or echo on mode so I can see what's going on as it's running?  Then I can send those results back to you.

It only does what we tell it, so no stepping through a bit limited in that respect. However, if it gets upset it'll pop up an error message, it's just failing to match up the names rather than going wrong. In essence, our fault rather than its.

I have another theory. Trailing spaces can create havoc when trying to match names. Lets see if there are any here. If you could run this snippet (short and simple). Just echoes the site name with quotes around it.

Chris
Set objADSysInfo = CreateObject("ADSystemInfo")
WScript.Echo "This is the site name in quotes: """ & objADSysInfo.SiteName & """"

Open in new window

Avatar of miket71

ASKER

After running the snippet, it says, This is the site name in quotes "Main".

Well that's.... annoying ;) It's supposed to do something that makes it obviously broken.

Lets try this version. No more case sensitivity, and a lot more flexible about the name, it just looks for our value within the string.

I've left the boxes that'll pop up telling us which it's running for now.

Chris
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("WScript.Network")
 
strSiteName = objADSysInfo.SiteName
 
If InStr(1, strSiteName, "Main", vbTextCompare) > 0 Then
 
  WScript.Echo "Mapping drives for Main"
 
  objNetwork.MapNetworkDrive "l:", "\\main\APPS"
  objNetwork.MapNetworkDrive "m:", "\\main\DATA"
  objNetwork.MapNetworkDrive "s:", "\\main\data"
  objNetwork.MapNetworkDrive "t:", "\\main\apps"
 
ElseIf InStr(1, strSiteName, "Delray", vbTextCompare) > 0 Then 
 
  WScript.Echo "Mapping drives for Delray"
 
  objNetwork.MapNetworkDrive "s:", "\\delray\data"
End If

Open in new window

Avatar of miket71

ASKER

Awsome, now it works for both the Main and Delray sites.  I just have one other question.  We sometimes like to use the con2prt.exe command to connect printers with a login script.  How would we fit that into the vbs script?

The syntax for con2prt would be like:  con2prt /c \\servername\printershare

So now that the mappings work, all we have to do is remove the   WScript.Echo "Mapping drives for..." lines so people don't have to click ok every time?

You would need to wrap it up with objShell.Run. As below, just move it to where you'd like it to run.

I've removed the WScript.Echo parts, and reinstated On Error Resume Next so it doesn't bother us if a drive is already mapped. On Error Goto 0 turns that option off again, useful if there's an error elsewhere in the script that needs capturing and fixing. A bit pointless in this case as it's the last thing in the script, but worth knowing about.

Chris
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
 
' Using Con2Ptr
objShell.Run "con2prt /c \\servername\printershare"
 
strSiteName = objADSysInfo.SiteName
 
On Error Resume Next
If InStr(1, strSiteName, "Main", vbTextCompare) > 0 Then
 
  objNetwork.MapNetworkDrive "l:", "\\main\APPS"
  objNetwork.MapNetworkDrive "m:", "\\main\DATA"
  objNetwork.MapNetworkDrive "s:", "\\main\data"
  objNetwork.MapNetworkDrive "t:", "\\main\apps"
 
ElseIf InStr(1, strSiteName, "Delray", vbTextCompare) > 0 Then 
 
  objNetwork.MapNetworkDrive "s:", "\\delray\data"
 
End If
On Error Goto 0

Open in new window

Avatar of miket71

ASKER

I forgot this one.  Sorry.  We also like to use the ifmember.exe command in the login script.  How would this one fit in?  Once location mappings, printers, and membership mappings can all be placed in the vbs script, we can abandon the batch version script altogether.  Here is an ifmember.exe syntax sample:

:"Knowledge Users"
ifmember "Knowledge Users"
if not errorlevel 1 goto next
echo Connecting to Group Name...Knowledge Users
net use v: \\g125rmt\kshare /persistent:no

:next
Avatar of miket71

ASKER

Chris, I also added these commands to clear out any remembered mappings in the vbs script:

objShell.Run "net use s: /delete"
objShell.Run "net use t: /delete"
objShell.Run "net use v: /delete"

I placed these after the first three commands if that is the correct spot:

Set objADSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of miket71

ASKER

Thanks Chris.  We will need to fill in the username with a domain ID below, correct?
Is there anything else we need to change below for authentication or identification purposes?

' Group based drive mappings
 
' Connect to the user account  (is LDAP is enabled by default in server 2003?)
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)  <--Domain ID?
' Get all of the users groups
strGroups = Join(objUser.GetEx("memberOf"), ";")

> Thanks Chris.  We will need to fill in the username with a domain ID below, correct?

No. ADSystemInfo knows about the current user, we just ask for it and connect based on that.

No authentication is required, the domain user is already able to read their own membership. It should all work as is :)

Chris
Avatar of miket71

ASKER

Thanks Chris for all your help and prompt resposes today.  I wish I could give you more than 500 points.  If there's a way, please let me know.  We will test and implement the final draft tomorrow and let you know either way if we have more questions.

There isn't, but don't worry about it, they're multiplied based on the grade you give anyway :) Besides, thank you is always much more appreciated.

Chris
Avatar of miket71

ASKER

Hi Chris,

Here are the points for you as promised.  We were still having trouble with membership mappings.  I'm not onsite there every day, but the next time I am, I'll grab a screen shot of the error and send it to you somehow.  We thought that the error would go away once DNS settled down from IP address/name changes on the machines, but it didn't, so we went ahead only with Site based mappings which work fine.  Thanks again for all oyur help.  Mike