Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need to map drives based on subnet mask and IP

Posted on 2009-04-16
24
Medium Priority
?
1,153 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:miket71
  • 12
  • 11
24 Comments
 
LVL 6

Expert Comment

by:andeporter
ID: 24157869
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.

0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24157919

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

0
 

Author Comment

by:miket71
ID: 24158129
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
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 71

Expert Comment

by:Chris Dent
ID: 24158195

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

0
 

Author Comment

by:miket71
ID: 24158301
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.
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24158380

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
0
 

Author Comment

by:miket71
ID: 24158529
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
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24158545

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
0
 

Author Comment

by:miket71
ID: 24158773
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
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24158832

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

0
 

Author Comment

by:miket71
ID: 24158964
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.
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24158999

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

0
 

Author Comment

by:miket71
ID: 24159288
After running the snippet, it says, This is the site name in quotes "Main".
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24159337

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

0
 

Author Comment

by:miket71
ID: 24159476
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?
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24159525

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

0
 

Author Comment

by:miket71
ID: 24159563
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
0
 

Author Comment

by:miket71
ID: 24160039
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")
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 24162666

That's absolutely fine. I've popped the method that compliments "MapNetworkDrive" below as an alternative, but feel free to use yours if you find that easier to work with.

For "ifmember" there are a number of ways to approach that one. The easiest makes use of AD System Info again to get the user account, then we can simple ask Active Directory for the list of groups. That returns as an Array, so we use Join to make it into a single string, after which we can use "InStr" again to see if the list of groups includes a single group.

Because a user might be in more than one group, and might need more than one mapping, we would use the form below rather than the ElseIf we used when testing the site.

If "is in a group" Then
   Map a drive
End If
If "is in another group" Then
   Map another drive
End If

That's not real vbscript, so don't try to use it. Only intended to show the rough process, which would make it pseudo-code. The real method is below :)

Chris
' Stop errors being thrown and upsetting people
On Error Resume Next
 
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
 
' Using Con2Ptr
 
objShell.Run "con2prt /c \\servername\printershare"
 
' Clear drive mappings
 
objNetwork.RemoveNetworkDrive "s:"
objNetwork.RemoveNetworkDrive "t:"
objNetwork.RemoveNetworkDrive "v:"
 
' Site based drive mapping
 
strSiteName = objADSysInfo.SiteName
 
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
 
' Group based drive mappings
 
' Connect to the user account
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
' Get all of the users groups
strGroups = Join(objUser.GetEx("memberOf"), ";")
 
If InStr(1, strGroups, "Knowledge Users", vbTextCompare) > 0 Then
 
  objNetwork.MapNetworkDrive "v:", "\\g125rmt\kshare"
 
End If

Open in new window

0
 

Author Comment

by:miket71
ID: 24163078
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"), ";")
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24163163

> 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
0
 

Author Comment

by:miket71
ID: 24163205
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.
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24165638

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
0
 

Author Closing Comment

by:miket71
ID: 31570994
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
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question