Solved

VBScript to map drives based on domain name

Posted on 2008-10-29
10
509 Views
Last Modified: 2012-06-22
I need a VBScript that will map 6 drives based on the domain name assigned by DHCP. I have 3 locations, 2 of them being child domains of the parent domain.
0
Comment
Question by:FIFBA
  • 4
  • 4
  • 2
10 Comments
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 100 total points
ID: 22835427
Hi, what about something like this...

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")
strDomain = UCase(objNetwork.UserDomain)
Select Case strDomain
	Case UCase("SUB1.DOMAIN.COM")
		objNetwork.MapNetworkDrive "G:", "\\server\gshare"
	Case UCase("SUB2.DOMAIN.COM")
		objNetwork.MapNetworkDrive "H:", "\\server\hshare"
	Case UCase("DOMAIN.COM")
		objNetwork.MapNetworkDrive "I:", "\\server\ishare"
End Select

Open in new window

0
 

Author Comment

by:FIFBA
ID: 22835562
How can I test this without mapping drives? i.e. display the result of the domain name query. Thanks.
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 22835891
For seeing the domain name, use this smaller script but remember you would need to run this once in each domain to see the actual results.
Set objNetwork = CreateObject("WScript.Network")
wscript.echo = UCase(objNetwork.UserDomain)

Open in new window

0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 14

Expert Comment

by:rejoinder
ID: 22835896
Opps, please take out the "=" in line 2... it should read;
wscript.echo UCase(objNetwork.UserDomain)
0
 

Author Comment

by:FIFBA
ID: 22845127
I'm not quite getting the results I expected. Maybe I need to be more clear about what I'm trying to do...

I need to determine what domain name is being handed out by the dhcp server. When a user goes to different locations, I need to map drives based on the dns suffix. So even though the PC may be a member of xyz.local, I need to map drives for the domain abc.xyz.local, which would be handed out as the dns suffix by the dhcp server at abc.xyz.local.

When I run the query, I get the domain that the PC is a member of for the results, regardless of the dns suffix handed out via DHCP.

Thanks for the help so far.
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 22845814
Here is a short sample that will tell you the DHCP server IP address and the DNSDomain name.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
For Each IPConfig in IPConfigSet
    if IPConfig.DHCPServer <> "" then
        wscript.echo "DHCP Server:" & vbTab & IPConfig.DHCPServer & vbCRLF &_
                     "DNS Domain:" & vbTab & IPConfig.DNSDomain
    end if
Next
 
wscript.echo "Done."

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22845940
Alternatively, does this give you the correct DNS Suffix?

Rob.
Set objShell = CreateObject("WScript.Shell")
strDNSDomain = UCase(objShell.ExpandEnvironmentStrings("%USERDNSDOMAIN%"))
WScript.Echo strDNSDomain

Open in new window

0
 

Author Comment

by:FIFBA
ID: 22846272
Getting closer...

When I run the first script, here's what I get...

DHCP SERVER: 192.168.200.1
DNS Domain: <empty>

then I click OK and I get

DHCP Server 192.168.1.1 (good)
DNS Domain: domain.local (good)

click OK again and get a 'done'.

So, really all I need to make this work is the DNS domain (above, domain.local is what I'm looking for).

0
 
LVL 14

Accepted Solution

by:
rejoinder earned 400 total points
ID: 22846535
This script uses the above but separates out the domain names so you can do your dive mapping etc based on the domain criteria.  Enter the various domain names as;
Case your.domain.name 
Follow this with the appropriate login script.
There might be some undesirable results with this technique.  If your client has multiple network cards (WiFi and wired), one of the network cards might retain the information from the other domain.  This would not confuse the script, it would simply loop through the cards and map accordingly where the last network card found would map drives taking precedence.
There might be other options worth trying such as adding login scripts through AD Sites and Services.  Each site you have set up will probably correspond to a particular domain.  Setting a GPO at this level to run login scripts might satisfy your needs.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
For Each IPConfig in IPConfigSet
    select case IPConfig.DNSDomain
        case "Domain1"
            wscript.echo "You are in Domain1 and this is your login script..."
        case "Domain2"
            wscript.echo "You are in Domain2 and this is your login script..."
        case "Domain3"
            wscript.echo "You are in Domain3 and this is your login script..."
    end select
Next
 
wscript.echo "Done."

Open in new window

0
 

Author Comment

by:FIFBA
ID: 22850660
Thanks for all the help. I think this will work. I've considered scripting based on site as you suggested but there are a couple things I would need to work out that I did not mention in my initial question. Your last script should get me most of the way there, though. Thanks again.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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