VBS Script

Coso

OSNN Newbie
Joined
Nov 17, 2011
Messages
2
I am trying to write a VBS script that will map drives and printers depending on what location the employee is at. I have a mixed win 7 and xp environment with 08 servers. I need to use IP address instead of site name because there are users that will take their laptops between locations. So far i have the following but i can not get it to run via GPO:

Code:
Dim strComputer,strIPAddress,strIPRange
Dim objNetwork
Dim IPConfigSet, IPConfig
Dim arrIPAddress
strComputer = "."
Set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}!//" _ 
 & strComputer & "/root/cimv2").ExecQuery("select IPAddress from " _ 
 & "Win32_NetworkAdapterConfiguration where IPEnabled=True") 
 
Set objNetwork = WScript.CreateObject("WScript.Network")
For Each IPConfig In IPConfigSet
     If Not IsNull(IPConfig.IPAddress) Then
          arrIPAddress = split(IPConfig.IPAddress(0) ,".")
          strIPRange = arrIPAddress(0) & "." & arrIPAddress(1) & "." & arrIPAddress(2) & ".0" 
          select case strIPRange
               case "172.31.2.*"
   WshNetwork.RemoveNetworkDrive "G:"
   WshNetwork.MapNetworkDrive "G:", "[URL="file://\\Server\Folder"]\\Server\Folder[/URL]"
                    objNetwork.AddWindowsPrinterConnection "[URL="file://\\Server\Printer"(UPSTAIRS"]\\Server\Printer"(UPSTAIRS[/URL])"
               case "172.31.1.*"
   WshNetwork.RemoveNetworkDrive "G:"
   WshNetwork.MapNetworkDrive "G:", "[URL="file://\\Server\Folder"]\\Server\Folder[/URL]"
                    objNetwork.AddWindowsPrinterConnection "[URL="file://\\Server\Printer"]\\Server\Printer[/URL]"
          end select
     end if
next
set objNetwork = nothing
set IPConfigSet = nothing
The logon script was added here in GPO "User Configuration", "Windows Settings", "Scripts (Logon/Logoff)", "Logon"

Any help would be great, this is getting stressful because this is one of the first scripts i need to run via vbs rather than just batch and i never really learned vbs :crosseyed: Thanks!!!!!
 
Last edited by a moderator:

fitz

Woah.. I'm still here?
Staff member
Political User
Joined
Apr 26, 2004
Messages
4,084
Assuming it's an Active Directory environment and you've setup your sites correctly, it shouldn't matter if they have laptops as wherever the computer is, it will pick up the correct AD site and you should be able to map drives based on site.

That all being said, does the script work if you run it manually?
The code you wrote has you setting the strIPRange variable to xxx.xxx.xxx.0 but your case statement has you searching on xxx.xxx.xxx.* - it will never find a match that way.

Code:
strIPRange = arrIPAddress(0) & "." & arrIPAddress(1) & "." & arrIPAddress(2) & ".0" 

select case strIPRange
  case "172.31.2.*"
  case "172.31.1.*"
end select
 

Members online

No members online now.

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Hey ep!

All good with me, applying for microsoft mvp right now, should have done this a while ago.

Notifications don't work, I only found your response by comming back to hunt up some threads, if you want, give me your email address so we can keep in touch easier, mine is perriscalderon at gmail
Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...

Forum statistics

Threads
61,976
Messages
673,274
Members
89,050
Latest member
lennerttt