Greetings,

I am writing a logon script for our domain users and have run into a
roadblock. I am trying to figure out how to use RemoveNetworkDrive in a Sub
rather than have it check every drive mapping. Here is what I have so far,
without a RemoveNetworkDrive entry:

Option Explicit

Dim objNetwork, objUser, CurrentUser

Dim strGroup, strDomain, strUser, strUNCPath

Dim bIsMember

Dim objGroup, objNet

'Map P: Drive (Public access share)

'Map H: Drive (Home Folder)

On Error Resume Next

If IsMemberOf("Domain Users") Then

MapDrive "P:","\\Test\Public"

End If

If IsMemberOf("Domain Users") Then

MapHomeDrive "H:","\\Test\Home"

End If

If Err <> 0 Then

MsgBox "Server not available; one or more drives were not mapped."

End If

On Error Goto 0



'------------------------------------------------------------------'

'FUNCTIONS '

'------------------------------------------------------------------'

Sub MapDrive(strLetter, strUNC)

Set objNet = WScript.CreateObject("WScript.Network")

objNet.MapNetworkDrive strLetter, strUNC

End Sub

Sub MapHomeDrive(strLetter, strUNCPath)

Set objNet = WScript.CreateObject("WScript.Network")

strUser = objNetwork.UserName

objNet.MapNetworkDrive strLetter, strUNCPath & "\" & strUser

End Sub

Function IsMemberOf(strGroupName)

Set objNetwork = CreateObject("WScript.Network")

strDomain = objNetwork.UserDomain

strUser = objNetwork.UserName

bIsMember = False

Set objUser = GetObject("WinNT://" & strDomain & "/" & _

strUser & ",user")

For Each objGroup In objUser.Groups

If objGroup.Name = strGroupName Then

bIsMember = True

Exit For

End If

Next

IsMemberOf = bIsMember

End Function

Re: If...then help please by Richard

Richard
Wed Apr 04 11:20:41 CDT 2007

You could write the functions to map drives similar to:
========
Sub MapDrive(strLetter, strUNC)
Set objNet = WScript.CreateObject("WScript.Network")
On Error Resume Next
objNet.MapNetworkDrive strLetter, strUNC
If (Err.Number <> 0) Then
On Error GoTo 0
objNet.RemoveNetworkDrive strLetter, True, True
objNet.MapNetworkDrive strLetter, strUNC
End If
On Error GoTo 0

End Sub
==========
I would advise against using "On Error Resume Next" for more than one
statement at a time. Especially in a logon script, if anything goes wrong,
you have no idea what happened. In your example, all errors are ignored. If
you are more cautious, you could code:
========
Sub MapDrive(strLetter, strUNC)
Set objNet = WScript.CreateObject("WScript.Network")
On Error Resume Next
objNet.MapNetworkDrive strLetter, strUNC
If (Err.Number <> 0) Then
On Error GoTo 0
objNet.RemoveNetworkDrive strLetter, True, True
On Error Resume Next
objNet.MapNetworkDrive strLetter, strUNC
If (Err.Number <> 0) Then
On Error GoTo 0
Call MsgBox("Drive cannot be mapped", "Logon Script")
End If
End If
On Error GoTo 0

End Sub
==========
Or, you could code a function instead of a Sub that returns True if
successful and False if not succesful in mapping the drive.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--

"Mac" <bcmchenry@nmrc.state.ms.us> wrote in message
news:uBh%23B$sdHHA.4616@TK2MSFTNGP03.phx.gbl...
> Greetings,
>
> I am writing a logon script for our domain users and have run into a
> roadblock. I am trying to figure out how to use RemoveNetworkDrive in a
> Sub rather than have it check every drive mapping. Here is what I have so
> far, without a RemoveNetworkDrive entry:
>
> Option Explicit
>
> Dim objNetwork, objUser, CurrentUser
>
> Dim strGroup, strDomain, strUser, strUNCPath
>
> Dim bIsMember
>
> Dim objGroup, objNet
>
> 'Map P: Drive (Public access share)
>
> 'Map H: Drive (Home Folder)
>
> On Error Resume Next
>
> If IsMemberOf("Domain Users") Then
>
> MapDrive "P:","\\Test\Public"
>
> End If
>
> If IsMemberOf("Domain Users") Then
>
> MapHomeDrive "H:","\\Test\Home"
>
> End If
>
> If Err <> 0 Then
>
> MsgBox "Server not available; one or more drives were not mapped."
>
> End If
>
> On Error Goto 0
>
>
>
> '------------------------------------------------------------------'
>
> 'FUNCTIONS '
>
> '------------------------------------------------------------------'
>
> Sub MapDrive(strLetter, strUNC)
>
> Set objNet = WScript.CreateObject("WScript.Network")
>
> objNet.MapNetworkDrive strLetter, strUNC
>
> End Sub
>
> Sub MapHomeDrive(strLetter, strUNCPath)
>
> Set objNet = WScript.CreateObject("WScript.Network")
>
> strUser = objNetwork.UserName
>
> objNet.MapNetworkDrive strLetter, strUNCPath & "\" & strUser
>
> End Sub
>
> Function IsMemberOf(strGroupName)
>
> Set objNetwork = CreateObject("WScript.Network")
>
> strDomain = objNetwork.UserDomain
>
> strUser = objNetwork.UserName
>
> bIsMember = False
>
> Set objUser = GetObject("WinNT://" & strDomain & "/" & _
>
> strUser & ",user")
>
> For Each objGroup In objUser.Groups
>
> If objGroup.Name = strGroupName Then
>
> bIsMember = True
>
> Exit For
>
> End If
>
> Next
>
> IsMemberOf = bIsMember
>
> End Function
>
>



Re: If...then help please by Mac

Mac
Wed Apr 04 14:01:41 CDT 2007


Ah I see, that defnitely makes more sense. Thanks for the reply and example!

"Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote in
message news:%23v6MTWtdHHA.5056@TK2MSFTNGP02.phx.gbl...
> You could write the functions to map drives similar to:
> ========
> Sub MapDrive(strLetter, strUNC)
> Set objNet = WScript.CreateObject("WScript.Network")
> On Error Resume Next
> objNet.MapNetworkDrive strLetter, strUNC
> If (Err.Number <> 0) Then
> On Error GoTo 0
> objNet.RemoveNetworkDrive strLetter, True, True
> objNet.MapNetworkDrive strLetter, strUNC
> End If
> On Error GoTo 0
>
> End Sub
> ==========
> I would advise against using "On Error Resume Next" for more than one
> statement at a time. Especially in a logon script, if anything goes wrong,
> you have no idea what happened. In your example, all errors are ignored.
> If you are more cautious, you could code:
> ========
> Sub MapDrive(strLetter, strUNC)
> Set objNet = WScript.CreateObject("WScript.Network")
> On Error Resume Next
> objNet.MapNetworkDrive strLetter, strUNC
> If (Err.Number <> 0) Then
> On Error GoTo 0
> objNet.RemoveNetworkDrive strLetter, True, True
> On Error Resume Next
> objNet.MapNetworkDrive strLetter, strUNC
> If (Err.Number <> 0) Then
> On Error GoTo 0
> Call MsgBox("Drive cannot be mapped", "Logon Script")
> End If
> End If
> On Error GoTo 0
>
> End Sub
> ==========
> Or, you could code a function instead of a Sub that returns True if
> successful and False if not succesful in mapping the drive.
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
> --