+ Post New Thread
Results 1 to 12 of 12
How do you do....it? Thread, Connecting to a folder in Technical; I have got this VBS script thats says WshNetwork.MapNetworkDrive "G:", "\servername\games\%username%\" But when the user logs on they are not ...
  1. #1

    Join Date
    Dec 2005
    Posts
    453
    Thank Post
    4
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Connecting to a folder

    I have got this VBS script thats says

    WshNetwork.MapNetworkDrive "G:", "\\servername\games\%username%\"

    But when the user logs on they are not getting the g:\. If i remove %username% it goes to the directory \\servername\games. There is a folder present within the games folder ie A user logs in as msmith there is a shared folder in there named msmith.

    Any advice would be great, I have tried permissions but still no joy.


    Faza

  2. #2

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,270
    Thank Post
    884
    Thanked 2,747 Times in 2,321 Posts
    Blog Entries
    11
    Rep Power
    785
    You can't use the environment variable like that in VBS you will want to replace it with:
    Code:
    fUser = WshNetwork.UserName
    WshNetwork.MapNetworkDrive "G:", "\\servername\games\" + fUser
    That environment variable will work fine in a batch file or a reg_expand registry key but VBS does not use the same syntax unfortunately. Here's the way to grab env variables in VBS:
    Code:
    fComputername = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
    Last edited by SYNACK; 28th May 2008 at 11:47 AM.

  3. #3

    Join Date
    Dec 2005
    Posts
    453
    Thank Post
    4
    Thanked 1 Time in 1 Post
    Rep Power
    0
    Thanks SYNACK

    Worked a treat !!!!

    Faza

  4. #4

    Join Date
    Dec 2005
    Posts
    453
    Thank Post
    4
    Thanked 1 Time in 1 Post
    Rep Power
    0
    Sorry bit more help required i have tried different things to get this working.

    I have the following script below

    Set objNetwork = CreateObject("WScript.Network")
    ' Forces script to skip errors (rem below line to see errors)
    on error resume next

    Dim WshNetwork
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    fUser = WshNetwork.UserName

    WshNetwork.RemoveNetworkDrive "G:"
    WshNetwork.RemoveNetworkDrive "W:"

    WshNetwork.MapNetworkDrive "W:", "\\servername\programs"
    WshNetwork.MapNetworkDrive "G:", "\\servername\games\" + fUser

    WScript.Quit


    The script works fine but for the g:\ mapping i only want that to work if the users are a member of a specific group. I know you can do it i have been looking everytime i try the whole script then fails to work.

    Any advice

    Faza

  5. #5

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,158
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    The quick and dirty way to do this is to set NTFS permissions on the top folder you're mapping to as drive G: (ie \\servername\games) to allow only the group access to the folder.

    When you try and connect the mapping will fail but because you have an on error resume next it will just go on to the next statement.

    Not sure if you have other stuff after that - the sort of thing I would do is:
    Code:
    err.clear
    WshNetwork.MapNetworkDrive "G:", "\\servername\games\" + fUser
    if err.number=0 then
      'we mapped the G drive do things with it
    else
      'we didn't map the G drive so do something else
    end if
    the not so quick and dirty way is to bind to the user object and look at the memberof property. This may or may not be easy. If every user is directly a member of the group then it's simple. If you have nested groups then it's much harder because you have to chase through them recursively.

  6. #6

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,323
    Thank Post
    903
    Thanked 1,801 Times in 1,550 Posts
    Blog Entries
    12
    Rep Power
    467
    This code will allow you to map drives based on what group the user is in

    On Error Resume Next

    Set objSysInfo = CreateObject("ADSystemInfo")
    Set objNetwork = CreateObject("Wscript.Network")

    strUserPath = "LDAP://" & objSysInfo.UserName
    Set objUser = GetObject(strUserPath)

    For Each strGroup in objUser.MemberOf
    strGroupPath = "LDAP://" & strGroup
    Set objGroup = GetObject(strGroupPath)
    strGroupName = objGroup.CN

    Select Case strGroupName
    Case "Domain Users"
    objNetwork.MapNetworkDrive "Z:", "\\Server\Share

    Case "Domain Admins"
    objNetwork.MapNetworkDrive "Y:", "\\Server\Share"

    End Select
    Next

  7. #7

    Join Date
    Dec 2005
    Posts
    453
    Thank Post
    4
    Thanked 1 Time in 1 Post
    Rep Power
    0
    FN-Greatermanchester

    Will it work with this command to, or will i need to add a line at the top?

    WshNetwork.MapNetworkDrive "G:", "\\servername\games\" + fUser

  8. #8
    eean's Avatar
    Join Date
    May 2006
    Location
    Seoul
    Posts
    572
    Thank Post
    71
    Thanked 57 Times in 41 Posts
    Rep Power
    31
    I think I understand you're question, so I'll butt in and reply.

    Yes, you can put that command in, but FN-Greatermanchester has called the "Wscript.Network" object objNetwork and you've called it WshNetwork.
    Also, you haven't defined fUser, so it won't know what the user name is.

    Try this:
    Code:
    On Error Resume Next
    
    Set objSysInfo = CreateObject("ADSystemInfo")
    Set objNetwork = CreateObject("Wscript.Network")
    
    
    ' I've just given the username a variable as you'll be using it later 
    '(You called it fuser, I've called it strUserName. 
    
    strUserName = objNetwork.UserName
    strUserPath = "LDAP://" & objSysInfo.UserName
    Set objUser = GetObject(strUserPath)
    
    For Each strGroup in objUser.MemberOf
    strGroupPath = "LDAP://" & strGroup
    Set objGroup = GetObject(strGroupPath)
    strGroupName = objGroup.CN
    
    Select Case strGroupName
    
    
    Case "Gaming" 'Edit your group name here
    
    objNetwork.MapNetworkDrive "G:", "\\ServerName\Games\" & strUserName
    
    'I think you won't need extra cases, but you can add them here if you want.
    Case "Domain Admins"
    objNetwork.MapNetworkDrive "Y:", "\\Server\Share"
    
    End Select
    Next
    Last edited by eean; 29th May 2008 at 11:32 AM. Reason: Fixed an error! (I hadn't appreciated the difference between the username given by ADSystemInfo and Wscript.network.

  9. #9
    eean's Avatar
    Join Date
    May 2006
    Location
    Seoul
    Posts
    572
    Thank Post
    71
    Thanked 57 Times in 41 Posts
    Rep Power
    31
    Hmm.. That's odd. When I try to correct my error, it shows it in the editing screen but only shows SOME of the correction on the page.

    Here is the right version, hopefully. I seem to have made a mountain out of a molehill

    Code:
    On Error Resume Next
    
    Set objSysInfo = CreateObject("ADSystemInfo")
    Set objNetwork = CreateObject("Wscript.Network")
    
    
    ' I've just given the username a variable as you'll be using it later 
    '(You called it fuser, I've called it strUserName. 
    
    strUserName = objNetwork.UserName
    strUserPath = "LDAP://" & objSysInfo.UserName
    Set objUser = GetObject(strUserPath)
    
    For Each strGroup in objUser.MemberOf
    strGroupPath = "LDAP://" & strGroup
    Set objGroup = GetObject(strGroupPath)
    strGroupName = objGroup.CN
    
    Select Case strGroupName
    
    
    Case "Gaming" 'Edit your group name here
    
    objNetwork.MapNetworkDrive "G:", "\\ServerName\Games\" & strUserName
    
    'I think you won't need extra cases, but you can add them here if you want.
    Case "Domain Admins"
    objNetwork.MapNetworkDrive "Y:", "\\Server\Share"
    
    End Select
    Next

  10. #10

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,270
    Thank Post
    884
    Thanked 2,747 Times in 2,321 Posts
    Blog Entries
    11
    Rep Power
    785
    I'll just repost my PM response to your PMed version of this question in case it can help anyone else out:

    Quote Originally Posted by SYNACK
    This function should do what you want with regard to the groups:

    Code:
    Usage:
    if ismember("All Teachers") then 
    
    Function:
    Function IsMember(sGroup)
    	Dim sAdsPath, oUser, oGroup
    
    	If IsEmpty(oGroupDict) Then
    		Set oGroupDict         = CreateObject("Scripting.Dictionary")
    		oGroupDict.CompareMode = vbTextCompare
    
    		sAdsPath  = WshNetwork.UserDomain & "/" & WshNetwork.UserName
    		Set oUser = GetObject("WinNT://" & sAdsPath & ",user")
    	
    		For Each oGroup In oUser.Groups
    			oGroupDict.Add oGroup.Name, "-"
    		Next
    		Set oUser = Nothing
    	End If
    	IsMember = CBool(oGroupDict.Exists(sGroup))
    End Function
    This is the one I use for drive mapping which includes error handeling:

    Code:
    Usage:
    DriveMapper "S:", "\\domain\dfs\share"
    
    Function:
    Sub DriveMapper(Drive, Share)
    	For i = 0 to oDrives.Count -1 Step 2
    	if LCase(Drive) = LCase(oDrives.Item(i)) then
    		if not LCase(Share) = LCase(oDrives.Item(i+1)) then
    			WshNetwork.RemoveNetworkDrive Drive, true, true
    		Else
    			Exit Sub
    		End if
    	End if
    	Next
    	on error resume next
    	WshNetwork.MapNetworkDrive Drive, Share
    	on error goto 0
    End Sub
    Can't take credit for these though as I goth them of the net from this guy:

    Code:
    '~~[author]~~
    'Johan Greefkes
    '~~[/author]~~
    
    '~~[emailAddress]~~
    'parcival@gmail.com
    '~~[/emailAddress]~~
    Hope these help.

  11. #11

    Join Date
    Dec 2005
    Posts
    453
    Thank Post
    4
    Thanked 1 Time in 1 Post
    Rep Power
    0
    Thanks SYNACK

    You know the latest script you sent (not the PM message), how and where would i put this line in. Sorry just learning the ropeas with script writing.

    WshNetwork.MapNetworkDrive "W:", "\\servername\programs"

    This line does not need any ifmember of etc... It is just a mapping for all users nopt dependant on group.


    Thanks for all your help and support

    Faza

  12. #12

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,270
    Thank Post
    884
    Thanked 2,747 Times in 2,321 Posts
    Blog Entries
    11
    Rep Power
    785
    Quote Originally Posted by faza View Post
    Thanks SYNACK

    You know the latest script you sent (not the PM message), how and where would i put this line in. Sorry just learning the ropeas with script writing.

    WshNetwork.MapNetworkDrive "W:", "\\servername\programs"

    This line does not need any ifmember of etc... It is just a mapping for all users nopt dependant on group.


    Thanks for all your help and support

    Faza
    If you are using my above post you would simply paste the two functions into your VBS at the end and then put all of your main code at the top so you would want:

    login.vbs:
    Code:
    'runs for all users
    DriveMapper "W:", "\\\servername\programs"
    'runs only if a member
    if ismember("All Teachers") then
       DriveMapper "S:", "\\domain\dfs\" + fUser
    end if
    
    Function IsMember(sGroup)
     ...stuff pasted from previous post...
    End Function 
    
    Sub DriveMapper(Drive, Share)
     ...stuff pasted from previous post...
    End Sub
    Disregard my PM as I thought you were talking about a different chunk of script.



SHARE:
+ Post New Thread

Similar Threads

  1. Connecting to SIMS tables from elsewhere
    By dhassen in forum MIS Systems
    Replies: 15
    Last Post: 19th September 2008, 12:16 PM
  2. Replies: 2
    Last Post: 17th April 2008, 12:25 PM
  3. Connecting Steps
    By speckytecky in forum Educational Software
    Replies: 1
    Last Post: 14th March 2008, 04:45 PM
  4. Replies: 15
    Last Post: 12th October 2006, 10:37 PM
  5. Connecting to SIMS.NET
    By u8dmtm in forum MIS Systems
    Replies: 7
    Last Post: 28th February 2006, 08:59 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •