Jump to content

Recommended Posts

Posted

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

Posted (edited)

You can't use the environment variable like that in VBS you will want to replace it with:

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:

fComputername = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

Edited by SYNACK
Posted

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

Posted

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:

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.

Posted

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

Posted

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

Posted (edited)

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:

On Error Resume Next

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

[color="Red"]
' I've just given the username a variable as you'll be using it later 
'(You called it fuser, I've called it strUserName. [/color]

[color="RoyalBlue"]strUserName = objNetwork.UserName[/color]
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 [color="Blue"]"Gaming"[/color] [color="Red"]'Edit your group name here[/color]

objNetwork.MapNetworkDrive "G:", [color="blue"]"\\ServerName\Games\" & strUserName[/color]

[color="Red"]'I think you won't need extra cases, but you can add them here if you want.[/color]
[color="Silver"]Case "Domain Admins"
objNetwork.MapNetworkDrive "Y:", "\\Server\Share"[/color]

End Select
Next

Edited by eean
Fixed an error! (I hadn't appreciated the difference between the username given by ADSystemInfo and Wscript.network.
Posted

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 :o

 

On Error Resume Next

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

[color="Red"]
' I've just given the username a variable as you'll be using it later 
'(You called it fuser, I've called it strUserName. [/color]

[color="RoyalBlue"]strUserName = objNetwork.UserName[/color]
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 [color="Blue"]"Gaming"[/color] [color="Red"]'Edit your group name here[/color]

objNetwork.MapNetworkDrive "G:", [color="blue"]"\\ServerName\Games\" & strUserName[/color]

[color="Red"]'I think you won't need extra cases, but you can add them here if you want.[/color]
[color="Silver"]Case "Domain Admins"
objNetwork.MapNetworkDrive "Y:", "\\Server\Share"[/color]

End Select
Next

Posted

I'll just repost my PM response to your PMed version of this question in case it can help anyone else out:

 

This function should do what you want with regard to the groups:

 

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:

 

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:

 

'~~[author]~~
'Johan Greefkes
'~~[/author]~~

'~~[emailAddress]~~
'[email protected]
'~~[/emailAddress]~~

 

Hope these help.

Posted

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

Posted
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:

'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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...